Click here to Skip to main content
16,004,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir / Madam

I Made Project Which Contain Datagridview in form and also i use database as sql 2005

now i have problem to save data from datagridview rows to database in button click event

My code save only last row's data in database how to save all rows data from datagridview to database Here is my code

VB
RS.Open("select * from purchasebill where entryno like '" & TextEntryNo.Text & "'", CN, CursorTypeEnum.adOpenKeyset, LockTypeEnum.adLockPessimistic)

        If (RS.EOF And RS.BOF) Then

            RS.AddNew()
            MsgBox(TextEntryNo.Text & "   Is Saved")

        Else

            If RS("EntryNo").Value = TextEntryNo.Text Then

                MsgBox(TextEntryNo.Text & "    User Ia Allready Exist")
                   
                CmdNew.Focus()

               Exit Sub

            End If

        End If
       
        RS("EntryNo").Value = TextEntryNo.Text
        RS("Entrydate").Value = MaskEntryDate.Text
        RS("BillNo").Value = TextBillNo.Text
        RS("billDate").Value = MaskBillDate.Text
        RS("SupplierName").Value = TextSupplierName.Text
        RS("Billtype").Value = ComboBillType.Text
         
     For i As Integer = 0 To DGVRecord.RowCount - 1

            RS("productname").Value = Me.DGVRecord.Rows(i).Cells("Product").Value
            RS("batchno").Value = Me.DGVRecord.Rows(i).Cells("batchno").Value
            RS("expirydate").Value = Me.DGVRecord.Rows(i).Cells("Expiry").Value

        Next

        RS.Update()

        RS.Close()


Please Help me to solve this 
Thanking you
Jayeshkumar m Patel
Posted
Updated 21-Oct-17 5:41am

Try to include
RS.Update()
on your loop:

 For i As Integer = 0 To DGVRecord.RowCount - 1

           RS("productname").Value = Me.DGVRecord.Rows(i).Cells("Product").Value
           RS("batchno").Value = Me.DGVRecord.Rows(i).Cells("batchno").Value
           RS("expirydate").Value = Me.DGVRecord.Rows(i).Cells("Expiry").Value

       RS.Update()
Next



if doesn't work that code try the code below:

VB
''Declare this to your class
 Public Const CONNECTION_STRING As String = "Data Source=YOUR_HOSTNAME;Initial Catalog=DATABASE_NAME; Integrated Security=True"
    Dim dbConnection As SqlConnection = New SqlConnection(CONNECTION_STRING)
Dim ds As DataSet = New DataSet
Dim da As SqlDataAdapter
''//Declare this to your class

 Private Sub updateDGV() 
        Dim SQLText As String = "SELECT Field1,Field2,Field3,Field4,Field5 FROM TEST"

        Try
            If dbConnection.State = ConnectionState.Open Then dbConnection.Close()
            dbConnection.Open()
            da= New SqlDataAdapter(SQLText, dbConnection)
            da.Fill(ds, "TEST")
            dbConnection.Close()
 
        Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
        da.Update(ds, "TEST")
     
       Catch ex As Exception
            MessageBox.Show(ex.ToString)
       End Try
  
  End Sub



Call the updateDGV method to your Save button.
Hope this will help you.
 
Share this answer
 
v2
The issue you have is that you are over writting the values through each iteration. You could try to move the update call to inside the for loop. However, this will not be very effecient as you would make n number of update calls to the Db.

[Just consolidated both answers from the user into 1]
VB
For i As Integer = 0 To DGVRecord.RowCount - 1

            RS("productname").Value = Me.DGVRecord.Rows(i).Cells("Product").Value
            RS("batchno").Value = Me.DGVRecord.Rows(i).Cells("batchno").Value
            RS("expirydate").Value = Me.DGVRecord.Rows(i).Cells("Expiry").Value
'The update is moved into the loop creating multiple calls to the update
RS.Update()
        Next
        RS.Close()


A better way is to create a dynamic update string. I am not great with vb but not knowing what your RS object is coded like does not help.

Here is an implementation that uses a dataset and a data adapter to fill a datagridview (in c#)

C#
SqlDataAdapter da=new SqlDataAdapter("select * from product",strcon);
SqlCommandBuilder cb=new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource=ds.Tables[0];

//now u can save changes to back end with
da.Update(ds);


this is not my code but from:
http://forums.devshed.com/net-development-87/update-table-in-database-through-datagridview-in-winform-350445.html[^]
 
Share this answer
 
v2
Comments
Jayeshkumar M Patel 25-Jun-12 1:50am    
Thank you for your reply
i am very new to vb.net
however your reply is litte help full to me
can you give me exact code for save data from datagridview in vb.net

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