Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using following command to update sql database from datagridview but the database is not updated. and their is no error. pls help.. thanks in advance

Dim update As New SqlCommand("Update FAGR SET FAGRN=@FAGRN,FAGRU=@FAGRU WHERE FAGRC=@FAGRC", connection)

update.Parameters.Add(New SqlParameter("@FAGRC", SqlDbType.NChar, 3))
update.Parameters.Add(New SqlParameter("@FAGRN", SqlDbType.NVarChar, 50))
update.Parameters.Add(New SqlParameter("@FAGRU", SqlDbType.NChar, 3))

Dim i As Integer
i = 0
Do While i < DataGridView1.Rows.Count
Try
update.Parameters(0).Value = DataGridView1.Rows(i).Cells(0).ToString
update.Parameters(1).Value = DataGridView1.Rows(i).Cells(1).ToString
update.Parameters(2).Value = DataGridView1.Rows(i).Cells(2).ToString
vartemp1 = update.ExecuteNonQuery()

Catch ex As Exception
MsgBox("Exception:" & vbCrLf & ex.Message)
Finally
Me.Close()
End Try
Loop
Posted
Updated 31-Jan-14 5:04am
v2

1 solution

Try changing the lines where you set the parameter values from:
VB
update.Parameters(0).Value = DataGridView1.Rows(i).Cells(0).ToString

to
VB
update.Parameters(0).Value = DataGridView1.Item(i, 0).Value.ToString

or
VB
update.Parameters(0).Value = DataGridView1.Rows(i).Cells(0).Value.ToString


Regards,
Johan
 
Share this answer
 
Comments
atul sharma 5126 6-Feb-14 6:31am    
thanks johan. I have already got it working with For each row ' command

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

  Print Answers RSS


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