Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying vb.net sales module that module 4 textbox value add in 1 datagridview 4 column value , another 4 textbox value insert in sql database single data table 8 column value.but value not insert how to solve the problam

What I have tried:

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(
Posted
Updated 2-Apr-18 5:39am
Comments
manirangasamy78 4-Apr-18 9:44am    
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 ',')

1 solution

I answered the exact same question earlier here: Vb.net 4 textbox 1 datagridview 1 save button how inert value in sqldatabase[^] .

Based on the post you have not tried the suggested actions. So have a try with something like the following. I don't have a compiler at hand so sorry for any typos

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)
com.ExecuteNonQuery()

com.CommandText = "insert into three (pno,pname,qty,price) values (@pno,@pname,@qty,@price)"
com.Parameters.Add("@pno", SqlDbType.VarChar, 100)
com.Parameters.Add("@pname", SqlDbType.VarChar, 100)
com.Parameters.Add("@qty", SqlDbType.VarChar, 100)
com.Parameters.Add("@price", SqlDbType.VarChar, 100)
For i As Integer = 0 To DataGridView1.Rows.Count - 1
   com.Parameters("@pno").Value = DataGridView1.Rows(i).Cells(0).Value
   com.Parameters("@pname".Value = DataGridView1.Rows(i).Cells(1).Value
   com.Parameters("@qty".Value = DataGridView1.Rows(i).Cells(2).Value
   com.Parameters("@price".Value = DataGridView1.Rows(i).Cells(3).Value
   com.ExecuteNonQuery()
Next
connection.Close(


All the questions in the previous answer still apply and check the data type and lengths prior using this code. For example I do not know if the type of PNO is varchar or something else.
 
Share this answer
 
v2
Comments
manirangasamy78 23-Apr-18 10:22am    
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 ',')
Wendelius 23-Apr-18 12:22pm    
Seems that there was another extra parenthesis in the first SQL statement. Fixed in the answer now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900