I have made a Form named form1.vb.
I have 4 TextBoxes named supid, supname, supmobile, suploc.
I have a DataGridView named datagridview1.
I am transferring these 4 TextBoxes data in the DataGridView but when I try to send a DataGridView row in SQL database, it sends only 1 row.
I want multiple rows inserted in SQL server at once when I click the insert button.
There are many tutorials about C language but no tutorial about VB.Net.
Please help how to do this in VB.Net.
My code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Else
connection.Open()
Dim thequery As String = "select*from supplierdata where supmobile = @supmobile"
Dim cmd1 As SqlCommand = New SqlCommand(thequery, connection)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
Dim reader As SqlDataReader = cmd1.ExecuteReader()
If reader.HasRows Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Else
reader.Close()
command = New SqlCommand("INSERT INTO supplierdata VALUES (@supid,@supname,@supmobile,@suploc)", connection)
Dim i As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
command.Parameters.Add("supid", SqlDbType.Int).Value = DataGridView1.Rows(i).Cells(0).value
command.Parameters.Add("supname", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(1).Value
command.Parameters.Add("supmobile", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(2).Value
command.Parameters.Add("suploc", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(3).Value
i = command.ExecuteNonQuery()
Next
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
connection.Close()
command.Dispose()
End If
End If
End Sub
DataTable. CallFillon the data adapter to populate theDataTableand bind it to the grid. Saving the changes is then a matter of callingUpdateon the data adapter. Of course, you'll need to configure it first, so you should read up on how to do that. I provided this link last time where you could learn how to do that.