You must Sign In to post a response.
  • Category: SQL Server

    Vb.net 4 textbox 1 datagridview 1 save button how inert value in sql databas

    con.Open()
    com.CommandText = "insert into three(sid,supid,supname,supperson) values((@sid,@supid,@supname,@supperson)"
    com.Parameters.AddWithValue("@sid", TextBox1.Text)
    com.Parameters.AddWithValue("@supid", TextBox2.Text)
    com.Parameters.AddWithValue("@supname", TextBox3.Text)
    com.Parameters.AddWithValue("@supperson", TextBox4.Text)
    For i As Integer = 0 To DataGridView1.Rows.Count - 1
    com.CommandText = "insert into three(pno,pname,qty,price) values((@pno,@pname,@qty,@price)"
    com.Parameters.AddWithValue("@pno", DataGridView1.Rows(i).Cells(0).Value)
    com.Parameters.AddWithValue("@pname", DataGridView1.Rows(i).Cells(1).Value)
    com.Parameters.AddWithValue("@qty", DataGridView1.Rows(i).Cells(2).Value)
    com.Parameters.AddWithValue("@price", DataGridView1.Rows(i).Cells(3).Value)
    com.ExecuteNonQuery()
    Next
    connection.Close()

    [I am trying this code but i am get this error

    (An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

    Additional information: Incorrect syntax near ',')]

    how to solve
  • #769627
    Hi Manikandan,

    I suggest your code should be like this.

    If connection.State = ConnectionState.Closed Then
    connection.Open()
    End If
    For i As Integer = 0 To DataGridView1.Rows.Count - 1
    com.CommandText = "insert into three(sid,supid,supname,supperson,pno,pname,qty,price) values(@sid,@supid,@supname,@supperson,@pno,@pname,@qty,@price)"
    com.Parameters.AddWithValue("@sid", TextBox1.Text)
    com.Parameters.AddWithValue("@supid", TextBox2.Text)
    com.Parameters.AddWithValue("@supname", TextBox3.Text)
    com.Parameters.AddWithValue("@supperson", TextBox4.Text)
    com.Parameters.AddWithValue("@pno", DataGridView1.Rows(i).Cells(0).Value)
    com.Parameters.AddWithValue("@pname", DataGridView1.Rows(i).Cells(1).Value)
    com.Parameters.AddWithValue("@qty", DataGridView1.Rows(i).Cells(2).Value)
    com.Parameters.AddWithValue("@price", DataGridView1.Rows(i).Cells(3).Value)
    com.ExecuteNonQuery()
    Next
    connection.Close()

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


  • Sign In to post your comments