Hey rony,This is the concept to insert into database for 2 entries . We have to keep one column as primary key and using for loop we can achive this. Using this concept you can do n number of columns insertion.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim str3 Try Dim str2 As String Dim count As Integer If (DataGridView1.Rows.Count > 0) Then For i As Integer = 0 To DataGridView1.Rows.Count - 2 str2 = "select count (*) from userinfo where userid = '" + DataGridView1.Rows(i).Cells(0).Value + "'" str3 = New SqlCommand(str2, con) con.Open() count = str3.ExecuteScalar con.Close() If (count = 0) Then Dim sqlcmd = New SqlCommand("insert into userinfo values('" + DataGridView1.Rows(i).Cells(0).Value + "','" + DataGridView1.Rows(i).Cells(1).Value + "')", con) sqlcmd.CommandType = CommandType.Text con.Open() sqlcmd.ExecuteNonQuery() con.Close() End If Next MsgBox("no records found to be inserted") End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub
Inserting multiple rows from datagridview to database table
in .NET
Posted · Edited by jassticky
Hey rony,This is the concept to insert into database for 2 entries . We have to keep one column as primary key and using for loop we can achive this. Using this concept you can do n number of columns insertion.