Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey,

I have created an application in vb.net 08, and database is sql services based database,

Now i have Code:
VB
Public Class Form1
    Dim upd As New Boolean
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MyDataBaseDataSet.tblUsers' table. You can move, or remove it, as needed.
        Me.TblUsersTableAdapter.Fill(Me.MyDataBaseDataSet.tblUsers)
    End Sub

    Private Sub TblUsersBindingSource_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles TblUsersBindingSource.ListChanged
        If MyDataBaseDataSet.HasChanges Then
            upd = True
        End If
    End Sub

    Private Sub DataGridView1_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowValidated
        If upd = True Then
            TblUsersTableAdapter.Update(MyDataBaseDataSet.tblUsers)
        End If
    End Sub
End Class


Now this is the code of my project, when im editing in the datagridview, and press enter at that time its update successful, and then im closing that debugging and again run that application then its some time show the updated data or some time not, but only in datagridview, in the database its never update,
plz help me to update both datagridview and database table,

Thanks,,,
@Nilesh,,,
Posted

Hi,

Before calling the update method of data adapter, you need to set the InsertCommand, UpdateCommand, DeleteCommand properties.
C#
SqlCommandBuilder cb;
 cb = new SqlCommandBuilder(TblUsersTableAdapter);


TblUsersTableAdapter.DeleteCommand = cb.GetDeleteCommand(true);
TblUsersTableAdapter.UpdateCommand = cb.GetUpdateCommand(true);
TblUsersTableAdapter.InsertCommand = cb.GetInsertCommand(true);


call update()
C#
TblUsersTableAdapter.Update(MyDataBaseDataSet.tblUsers)



Sample program
-------------

VB
Imports System.Data.SqlClient

Public Class Form1

    Dim upd As New Boolean
    Dim cb As New SqlCommandBuilder
    Dim cn As New SqlConnection("server=.\sqlexpress;database=testdb;uid=sa;pwd=sa123")
    Dim dt As New DataTable
    Dim da As New SqlDataAdapter

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim squery As String
        squery = "Select * from dtl"
        da.SelectCommand = New SqlCommand(squery, cn)
        da.Fill(dt)
        MessageBox.Show(dt.Rows.Count.ToString())
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dr As DataRow = dt.NewRow
        dr(0) = 4
        dr(1) = "Shasini"
        dr(2) = 5

        dt.Rows.Add(dr)

        MessageBox.Show(dt.Rows.Count.ToString())

        Dim cb As New SqlCommandBuilder(da)

        da.DeleteCommand = cb.GetDeleteCommand(True)
        da.UpdateCommand = cb.GetUpdateCommand(True)
        da.InsertCommand = cb.GetInsertCommand(True)

        da.Update(dt)
    End Sub
End Class


The above sample works fine and it is inserting the record in the db. Please make sure you are having a Primary Key field in the table.

Best Regards
Muthuraja
 
Share this answer
 
v2
Comments
Muthuraja Irullandi 2-Feb-13 13:27pm    
Hi,
Please remove the semicolon in the below line,
TblUsersTableAdapter.DeleteCommand = cb.GetDeleteCommand(true);
TblUsersTableAdapter.UpdateCommand = cb.GetUpdateCommand(true);
TblUsersTableAdapter.InsertCommand = cb.GetInsertCommand(true);

Please see the updated solution for sample program
Nilesh Varchand 3-Feb-13 2:37am    
yeah, thanks alot
Thanking a lot dude,
Finally i got the Perfect Answer,
Code For Update DataGridView at Run Time,,,

VB
Imports System.Data.SqlClient
Public Class Form1
    Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Project\Practice Projects\DataGridViewFinal\DataGridViewFinal\MyDataBase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
    Dim upd As New Boolean
    Dim ada As New SqlClient.SqlDataAdapter("Select * from tblusers", con)
    Dim tab As New DataSet
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MyDataBaseDataSet.tblUsers' table. You can move, or remove it, as needed.
        Me.TblUsersTableAdapter.Fill(Me.MyDataBaseDataSet.tblUsers)
    End Sub
 
    Private Sub TblUsersBindingSource_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles TblUsersBindingSource.ListChanged
        If MyDataBaseDataSet.HasChanges Then
            upd = True
        End If
    End Sub
 
    Private Sub DataGridView1_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowValidated
        If upd = True Then
            Dim cb As New SqlCommandBuilder(ada)
            ada.DeleteCommand = cb.GetDeleteCommand(True)
            ada.UpdateCommand = cb.GetUpdateCommand(True)
            ada.InsertCommand = cb.GetInsertCommand(True)
            ada.Update(MyDataBaseDataSet.tblUsers)
        End If
    End Sub
End Class


Thanks Again,,,
@Nilesh,,,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900