• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9197
  • 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
        End If

        da.Fill(dsLine_Items, "dtLine_Items")

        Me.DataGridView1.DataSource = dsLine_Items.Tables("dtLine_Items")
  • 3
1 Solution
Jorge PaulinoIT Pro/DeveloperCommented:
With the update command:

Jorge PaulinoIT Pro/DeveloperCommented:
In your case

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?
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.

Jorge PaulinoIT Pro/DeveloperCommented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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