Using update with a table adapter

dougancil

Well-known member
Joined
Jun 29, 2010
Messages
50
Programming Experience
Beginner
I have a form that has a datagridview and a table adapter that displays data from a SQL database. What I'd like to do is that when edits are made to the data in the datagrid, I'd like to post all of the data back to the table. I know that you can do that with a dataset and a data adapter, but I don't know how to do that with a table adapter. Here is the code for my page:

VB.NET:
Public Class ExceptionEdit
    Private Sub ExceptionEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MDRDataSet.scratchpad3' table. You can move, or remove it, as needed.
        Me.Scratchpad3TableAdapter.Fill(Me.MDRDataSet.scratchpad3)
    End Sub
    Private Sub SaveExceptionButton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveexceptionsButton.Click
        Dim oCmd2 As System.Data.SqlClient.SqlCommand
        Dim oDr2 As System.Data.SqlClient.SqlDataReader
        oCmd2 = New System.Data.SqlClient.SqlCommand
        Try
            With oCmd2
                .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
                .Connection.Open()
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@payperiodstartdate", payperiodstartdate)
                .Parameters.AddWithValue("@payperiodenddate", payperiodenddate)
                .CommandText = "sp_exceptioncopy"
                oDr2 = .ExecuteReader()
                oCmd2.Connection.Close()
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            oCmd2.Connection.Close()
        End Try
        ExceptionsFinal.Show()
    End Sub

When people click on the Save Exception button, I'd like to save back to the update the database with the new data.

Thank you
 
Given that you call Fill on a DataAdapter to populate a DataTable and you call Fill on a TableAdapter to populate a DataTable, if you call Update on a DataAdapter to save data from a DataTable then how do you suppose you save data from a DataTable using a TableAdapter?

You shouldn't need any of that code to save the data. The whole point of a typed DataSet is that it's all create in the designer. If the wizard doesn't create exactly what you want then you should be making the appropriate changes in the DataSet designer.
 
Back
Top