• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9116
  • Last Modified:

Save changes to datagridview to database using vb.net

I am working on a windows froms project which i am using a datagridview control on.   I am populating the grid using a datatable which all works find.   I want to be able to save any changes or new rows that the user adds or makes to the rows in the gridview.   How to i save the changes back to the database.   I am using the following code to populate the grid:

Dim sSQL As String = "SELECT * FROM tblLine_Items WHERE Rec_ID = '1' ORDER BY RecordNumber"

        Dim cn As New SqlConnection(sconnstring)
        Dim da As New SqlDataAdapter(sSQL, cn)

        If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If

        da.Fill(dsLine_Items, "dtLine_Items")

        Me.DataGridView1.DataSource = dsLine_Items.Tables("dtLine_Items")
0
Mike_Stevens
Asked:
Mike_Stevens
  • 3
1 Solution
 
jpaulinoCommented:
With the update command:

mydatatable.Update(mydataset)
0
 
jpaulinoCommented:
In your case

da.update(dsLine_Items)
0
 
Mike_StevensAuthor Commented:
I want to be able to allow the user to click a button to save the changes back.   If I use you example in  the click event of a button i get a "da not declared" message.   Where do i declare the dataadapter?
0
 
Bob LearnedCommented:
If you defined the SqlDataAdapter as the module-level, then you could use the Fill method to get the data, and the Update method to write the changes back to the database.

Bob
0
 
jpaulinoCommented:
You have to declare the dataadapter outside the sub/function that you have like

Private da As SqlDataAdapter

Then in your sub/function you do

da =  New SqlDataAdapter(sSQL, cn)

Now on the click event you use the update. You have to do the same for the dataset


0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now