I am trying to create a form to edit the data in a SQL Server table using a DataAdapter (in code) rather than a TableAdapter (drag and drop). I define the connection, dataset, dataapapter, and datatable when the form loads and populate the datagridview, but then I can't trigger the Update with a button because the DataAdapter expires after the load event is done. With the TableAdapter, it persisted somehow so that I could refer to it in the code for the button. How can I do this?
Imports System.Data.SqlClient
Public Class frmGroceries2
Private Sub frmGroceries2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim oCon As New SqlConnection
oCon.ConnectionString = "Data Source=IPS-03042013\sqlexpress;Initial Catalog=SqlToVbExamples;Integrated Security=True"
Dim dsSupplies As New DataSet
Dim daLocalGroceries As New SqlDataAdapter("SELECT * FROM GROCERIES", oCon)
Dim tblLocalGroceries As DataTable
Try
daLocalGroceries.FillSchema(dsSupplies, SchemaType.Source, "LocalGroceries")
daLocalGroceries.Fill(dsSupplies, "LocalGroceries")
tblLocalGroceries = dsSupplies.Tables("LocalGroceries")
dgvLocalGroceries.DataSource = tblLocalGroceries
Catch ex As Exception
MsgBox("Something has gone wrong..." & vbNewLine & ex.Message)
End Try
End Sub
Private Sub cmdSaveChanges_Click(sender As Object, e As EventArgs) Handles cmdSaveChanges.Click
'I want to put the update method in here, but can't
End Sub
End Class