I am having a problem and I'm pretty sure it is me missing something somewhere.
I have set up a project in VS2008 and for most of the application I am using the Windows controls and their default behaviour. However, I have started programming in some functionality for the combo boxes that link to other tables. I set this up using the VS ComboBox Tasks.
What I am trying to do is let people type a client name into a client combobox. If that entry is not in the combobox, then a message box will ask the user whether this should be saved to the database. This uses the Comboboxes Leave event. However, when I save the record, close the program and open it up again, I get an error message saying System.ArgumentException: DataGridViewComboBoxCell value is not valid.
On further examination of the table with the foreign key, I notice that this has been stored as a -1. The code for this functionality is as follows:
If anyone could get me over this stumbling block, then I would be eternally grateful!!!
I have set up a project in VS2008 and for most of the application I am using the Windows controls and their default behaviour. However, I have started programming in some functionality for the combo boxes that link to other tables. I set this up using the VS ComboBox Tasks.
What I am trying to do is let people type a client name into a client combobox. If that entry is not in the combobox, then a message box will ask the user whether this should be saved to the database. This uses the Comboboxes Leave event. However, when I save the record, close the program and open it up again, I get an error message saying System.ArgumentException: DataGridViewComboBoxCell value is not valid.
On further examination of the table with the foreign key, I notice that this has been stored as a -1. The code for this functionality is as follows:
VB.NET:
Private Sub ClientComboBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClientComboBox.Leave
Dim con As New OleDb.OleDbConnection
Dim dsClients As New DataSet
Dim cmdClients As New OleDb.OleDbDataAdapter
Dim strQryClients As String = "SELECT ID, ClientName FROM Clients ORDER BY ClientName ASC"
Dim strDataSource As String = "Provider=Microsoft.Ace.OLEDB.12.0;" & _
"Data Source=C:\Documents and Settings\bowringj\Desktop\Bid Status.accdb"
Dim strClientSelection As String
Dim intClientRecordCounter As Integer
Dim boolClientFound As Boolean
'Dim cbClients As New OleDb.OleDbCommandBuilder(cmdClients)
Dim dsNewRow As DataRow
strClientSelection = ClientComboBox.Text
boolClientFound = False
con.ConnectionString = strDataSource
Try
con.Open()
Catch ex As Exception
MsgBox("An error occurred while trying to connect to the database." & vbCrLf & _
"You will not be able to use the update functionality from the Client Combo Box.", _
MsgBoxStyle.Critical, "Database Connection Error")
End Try
Try
cmdClients = New OleDb.OleDbDataAdapter(strQryClients, con)
cmdClients.Fill(ClientsDataSet, "Clients")
Dim cbClients As New OleDb.OleDbCommandBuilder(cmdClients)
Catch ex As Exception
MsgBox("An error occurred while trying to retrieve data from the database." & vbCrLf & _
"You will not be able to use the update functionality from the Client Combo Box.", _
MsgBoxStyle.Critical, "Data Retrieval Error")
Finally
con.Close()
End Try
For intClientRecordCounter = 0 To ClientsDataSet.Tables("Clients").Rows.Count - 1
If ClientsDataSet.Tables("Clients").Rows(intClientRecordCounter).Item(1) = strClientSelection Then
boolClientFound = True
Exit For
End If
Next intClientRecordCounter
If Not boolClientFound Then
If MessageBox.Show("The client entered does not appear in the list." & vbCrLf & _
"Do you want to add this client?", _
"Add Client", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Try
dsNewRow = ClientsDataSet.Tables("Clients").NewRow()
dsNewRow.Item("ClientName") = strClientSelection
ClientsDataSet.Tables("Clients").Rows.Add(dsNewRow)
cmdClients.Update(ClientsDataSet, "Clients")
ClientsBindingSource.ResetBindings(False)
ClientComboBox.Refresh()
MsgBox("The Client has been added to the database.", MsgBoxStyle.Information, "Client Added")
Catch ex As Exception
MsgBox("There was a problem adding the new client to the database.", MsgBoxStyle.Exclamation, "Client Adding Error")
End Try
End If
End If
End Sub
If anyone could get me over this stumbling block, then I would be eternally grateful!!!