Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I cant seem to save multiple data from my datagridview to SQL Server.
When i loop using messagebox, the loop from line 1 to 10 works. But when using the same code to insert data, it can only save 1 line then the code exits.

Any sensei willing to help?
:(

What I have tried:

Dim c As Integer = 0
Dim dgv As Integer = DataGridView1.Rows.Count - 1
While c < dgv
ExecuteSQLQuery("INSERT INTO G_Masterlist (serial_no) VALUES (" + DataGridView1.Item(3, c).Value.ToString + ");")
'MsgBox(DataGridView1.Item(3, c).Value.ToString)
c += 1
End While



"also tried this"



For Each band As DataGridViewBand In DataGridView1.Rows
Dim strItemID As String = CStr(DataGridView1.Rows(band.Index).Cells(3).Value)
'MsgBox(strItemID)
sqlSTR = "INSERT INTO G_Masterlist (serial_no) VALUES ('" + strItemID + "')"
ExecuteSQLQuery(sqlSTR)
Next
Posted
Updated 9-Mar-16 22:59pm
Comments
Richard Deeming 10-Mar-16 5:58am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

Use proper data binding and the framework will do most of the work for you; see Updating, Inserting, and Deleting Records in a Dataset[^].
 
Share this answer
 

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