Check with the below code.
If con.State = ConnectionState.Open Then
con.Close()
End If
Dim i As Integer
Dim rs1 As String
Try
For i = 0 To DataGridView1.Rows.Count - 1
rs1 = "INSERT INTO tblProductsve(srno,ItemCode, ProDes, Price,Qty,amt,subtot) VALUES(@srno,@ItemCode, @ProDes, @Price,@Qty,@amt,@subtot)"
Dim cmd As New SqlClient.SqlCommand(rs1, con)
If Not String.IsNullOrEmpty(DataGridView1.Rows(i).Cells(0).Value) Then
cmd.Parameters.AddWithValue("@srno", DataGridView1.Rows(i).Cells(0).Value)
Else
cmd.Parameters.AddWithValue("@srno", DBNull.Value)
End If
If Not String.IsNullOrEmpty(DataGridView1.Rows(i).Cells(1).Value) Then
cmd.Parameters.AddWithValue("@ItemCode", DataGridView1.Rows(i).Cells(1).Value)
Else
cmd.Parameters.AddWithValue("@ItemCode", DBNull.Value)
End If
If Not String.IsNullOrEmpty(DataGridView1.Rows(i).Cells(2).Value) Then
cmd.Parameters.AddWithValue("@ProDes", DataGridView1.Rows(i).Cells(2).Value)
Else
cmd.Parameters.AddWithValue("@ProDes", DBNull.Value)
End If
If Not String.IsNullOrEmpty(DataGridView1.Rows(i).Cells(3).Value) Then
cmd.Parameters.AddWithValue("@Price", DataGridView1.Rows(i).Cells(3).Value)
Else
cmd.Parameters.AddWithValue("@Price", DBNull.Value)
End If
If Not String.IsNullOrEmpty(DataGridView1.Rows(i).Cells(4).Value) Then
cmd.Parameters.AddWithValue("@Qty", DataGridView1.Rows(i).Cells(4).Value)
Else
cmd.Parameters.AddWithValue("@Qty", DBNull.Value)
End If
If Not String.IsNullOrEmpty(DataGridView1.Rows(i).Cells(5).Value) Then
cmd.Parameters.AddWithValue("@amt", DataGridView1.Rows(i).Cells(5).Value)
Else
cmd.Parameters.AddWithValue("@amt", DBNull.Value)
End If
If Not String.IsNullOrEmpty(TextBox1.Text) Then
cmd.Parameters.AddWithValue("@subtot", TextBox1.Text)
Else
cmd.Parameters.AddWithValue("@subtot", DBNull.Value)
End If
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Next
MessageBox.Show("Save Successully ")
Catch ex As Exception
MsgBox(ex.Message)
End Try