troubleshooting Question

How to Edit DataGridview1 in vb.Net Windows Form and save it to database.

Avatar of searchsanjaysharma
searchsanjaysharma asked on
Visual Basic.NET
12 Comments1 Solution335 ViewsLast Modified:
I have the button btnsave, and btnfind and txtid.Text.

The users enters the id in txtid, clicks on btnfind and the respective record is binded to the DataGridView1

The users clicks on any cell and modifies the existing values.

Now i have btnsave, when the user clicks on the this, the new values of that Datagridview1 s should be stored in database.

1. How to do this. The current code is as follows.


Imports System.Data
Imports System.Data.SqlClient
Public Class Form3
    Dim con As SqlConnection
    Dim dt As DataTable
    Dim da As New SqlDataAdapter
    Dim s As String

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        'call update procedure where id= txtid.Text
    End Sub

    Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click
        Try
            DataGridView1.Height = Me.Height
            DataGridView1.Width = Me.Width
            con = New SqlConnection("Data Source=172.16.3.17;Initial Catalog=testadms;User Id=sa;Password=admin#cgc;")
            If (con.State = ConnectionState.Closed) Then
                con.Open()
            End If
            s = "SELECT id,firstname,lastname from mstusers where id=" & Convert.ToInt32(txtid.Text.Trim())
            da = New SqlDataAdapter(s, con)
            dt = New DataTable()
            da.Fill(dt)
            DataGridView1.DataSource = dt
        Catch ex As Exception
            MsgBox("Error occured " & ex.ToString())
        Finally
            con.Close()
        End Try
    End Sub
End Class
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros