DataGrid View using Data Binding to amend changes to a file

  • Thread starter Thread starter Lucky Hyena
  • Start date Start date
L

Lucky Hyena

Guest
Hello. I am trying to use data binding for a datagrid object to create a save button.

Here is the relevant code

Private Sub PopulateDataGrids(ByRef fileDirectory As String, ByRef fileName As String, ByRef checkCounter As Integer) 'code to establish connections from all tables to all data grids
Dim testAdapter As New OleDbDataAdapter
Dim testDs1 As New DataSet
If checkCounter = 0 Then
'First Data Grid
Dim con As New OleDbConnection
con.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fileDirectory & fileName)
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
con.Open()
ds.Tables.Add(dt)
da = New OleDbDataAdapter("SELECT * FROM Patient", con)
da.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
testDs1 = ds
testAdapter = da
con.Close()
ElseIf checkCounter = 2 Then
testAdapter.Update(testDs1, "Patient")
End If

End Sub


The counter is used to check whether the save button has been clicked. When the button is clicked, it calls this function and passes the counter's value to it. When the counter value is 0, the procedure is used to populate the grids with tables after a new file has been created or an old one opened. The procedure will run the population process every time, as the user cannot click the Save button without this process having been run.

I am have set all the relevant variables from the first time the procedure is called to some placeholders outside the if statement so i can use those values the second time the procedure is called. However, when it's called the second time by clicking the save button I get an error: System.InvalidOperationException: 'Update unable to find TableMapping['Patient'] or DataTable 'Patient'.'

How do I fix this? Thanks :)

Continue reading...
 
Back
Top