Click here to Skip to main content
15,888,816 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 30-Mar-18 4:38am
v3

1 solution

One problem is that you seem to have an extra opening parenthesis after VALUES keyword

The commmand should be
com.CommandText = "insert into three(sid,supid,supname,supperson) values(@sid,@supid,@supname,@supperson)"

Another thing is that you don't execute the first command at all, instead you go into the loop and modify the statement.

The same problem is with the second statement, it should be
com.CommandText = "insert into three(pno,pname,qty,price) values(@pno,@pname,@qty,@price)"

Then since you run the statement inside a loop you add parameters on every iteration. The first time the amount of parameters is 4 the second time 8 and so on. This means that the execution will fail on second iteration. You should move the parameter addition outside the loop and just set the åarameter values inside the loop.

Also it looks a bit odd that you're using completely different values and columns but the table name in both statements is three

I believe you would benefit going through the following article: Properly executing database operations[^]
 
Share this answer
 
Comments
manirangasamy78 30-Mar-18 7:34am    
i need solution for above code in single data table (but 4 textbox value and datagridview value ) how it possible
Wendelius 30-Mar-18 8:08am    
I'm sorry but I don't quite understand what you're asking.

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