Я пытаюсь добавить и сотрудника, и свою работу одновременно. У меня есть таблица идентификаторов, в которую я пытаюсь вставить обе записи. У меня есть кнопка НАЙТИ, которая открывает поле со списком в другой форме, заполненной заданиями. Как только выбор сделан, он заполняет текстовое поле txtJobName в первой форме. Это заполняется именем задания, как и должно, но я не могу вытащить SelectedIndex для вставки в таблицу TJobEmployees.
'Variables
Dim strSelect As String
Dim strInsert As String
Dim strFirstName As String = ""
Dim strLastName As String = ""
Dim strJob As String = frmSelectJobName.strSelectedJob
Dim intJobID As Integer
Dim cmdSelect As OleDb.OleDbCommand
Dim cmdInsert As OleDb.OleDbCommand
Dim dr As OleDb.OleDbDataReader
Dim intNextHighestRecordID As Integer
Dim intRowsAffected As Integer
Dim result As DialogResult
strFirstName = txtFirstName.Text
strLastName = txtLastName.Text
strJob = txtJobName.Text
intJobID = CInt(frmSelectJobName.lstJobs.SelectedValue)
If Validation() = True Then
If OpenDatabaseConnectionSQLServer() = False Then
' No, warn the user ...
MessageBox.Show(Me, "Database connection error." & vbNewLine &
"The application will now close.",
Me.Text + " Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
' and close the form/application
Me.Close()
End If
' always ask before adding!!!!
result = MessageBox.Show("Are you sure you want to Add Employee: Job-" & txtJobName.Text & "?", "Confirm Submission", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
' this will figure out which button was selected. Cancel and No does nothing, Yes will allow insert
Select Case result
Case DialogResult.Cancel
MessageBox.Show("Action Canceled")
Case DialogResult.No
MessageBox.Show("Action Canceled")
Case DialogResult.Yes
strSelect = "SELECT MAX(intEmployeeID) + 1 AS intNextHighestRecordID " &
" FROM TEmployees"
' Execute command
cmdSelect = New OleDb.OleDbCommand(strSelect, m_conAdministrator)
dr = cmdSelect.ExecuteReader
' Read result( highest ID )
dr.Read()
' Null? (empty table)
If dr.IsDBNull(0) = True Then
' Yes, start numbering at 1
intNextHighestRecordID = 1
Else
' No, get the next highest ID
intNextHighestRecordID = CInt(dr.Item(0))
End If
' add the child record
strInsert = "Insert into TEmployees (intEmployeeID, strFirstName, strLastName)" &
" Values (" & intNextHighestRecordID & ",'" & strFirstName & "'," & "'" & strLastName & "')"
cmdInsert = New OleDb.OleDbCommand(strInsert, m_conAdministrator)
intRowsAffected = cmdInsert.ExecuteNonQuery()
'add the parent record
strInsert = "Insert into TJobEmployees (intJobID, intEmployeeID)" &
" Values (" & intJobID & ",'" & intNextHighestRecordID & "')"
' Insert the record(s)
cmdInsert = New OleDb.OleDbCommand(strInsert, m_conAdministrator)
intRowsAffected = cmdInsert.ExecuteNonQuery()
If intRowsAffected > 0 Then
MessageBox.Show("Job has been added")
Me.Close()
End If
End Select
CloseDatabaseConnection()
Form1_Load(sender, e)
End If