i want to insert records from datagridview to mysql database table ,
but i dont know how to insert datagridview to database table.
How to do it? :(
4 Answers
This is jus my suggestion if you have you code then can work out more better
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''create connection
Dim conn As SqlConnection = New SqlConnection()
conn.Open()
Dim comm As SqlCommand = New SqlCommand()
comm.Connection = conn
''insert data to sql database row by row
Dim name, ageAs String
For i As Integer = 0 To Me.DataGridView1.Rows.Count
name = Me.DataGridView1.Rows(i).Cells(0).ToString()
age= Me.DataGridView1.Rows(i).Cells(1).ToString()
comm.CommandText = "insert into mytable(name,age) values('" & name & "','" & age& "')"
comm.ExecuteNonQuery()
Next
conn.Close()
End Sub
This is just a way to insert it is best it you use stored procedure to insert the value because if you use SQL it might happen SQL Injection. So stored procedure is the best way and save to use.
4 Comments
instead of inserting data I was trying to update the record and for me
Me.AddPurchases.Rows(i).Cells(0).Value.ToString()
this statement did not solve the purpose this statement will return the row count and column count like { row=0,col=1} next iteration { row=0,col=1} and so on but
Dim roid As String
For i As Integer = 0 To Me.edit_role_grid.RowCount - 1
roid = Me.edit_role_grid.Rows(i).Cells(0).FormattedValue
MsgBox(roid)
this worked fine for me, this is returning the text of the cell and now this value can be used anywhere