Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i m trying to this code but i think loop not working and datagridview data not inserted into DB please help

What I have tried:

Dim list As New List(Of SqlParameter)

For rw As Integer = 0 To DataGridView1.Rows.Count - 1

qry = "insert into ItemMaster(D_Id,DNo,DName,PCS,Weight) values(@1,@2,@3,@4,@5)"

list.Add(New SqlParameter("@1", TextBox3.Text))
list.Add(New SqlParameter("@2", DataGridView1.Rows(rw).Cells(1).Value))
list.Add(New SqlParameter("@3", DataGridView1.Rows(rw).Cells(2).Value))
list.Add(New SqlParameter("@4", DataGridView1.Rows(rw).Cells(3).Value))
list.Add(New SqlParameter("@5", DataGridView1.Rows(rw).Cells(4).Value))

Next

If dal.execute(qry, list) Then
MsgBox("Data inserted successfully")
Else
MsgBox("Error inserting data")
End If

End Sub
Where is the problem "Data inserted successfully" msg shown but data not inserted
Posted
Updated 31-Aug-21 0:36am
Comments
Richard MacCutchan 28-Aug-21 6:33am    
You are creating a list of all the rows of the DataGridView before you try the insert operation. You need to move the If block above the Next statement. Also, are you sure the execute command takes a List(T) rather than a SqlParameterCollection Class (System.Data.SqlClient) | Microsoft Docs[^]?
Joshi_Aryan 28-Aug-21 6:38am    
how let me explain with code example please
Richard MacCutchan 28-Aug-21 7:00am    
See Solution 2 below.

Your INSERT SQL instruction is only executed once, so it only expects a single set of parameters. You add them in the loop, but unless the DAL code "knows" that it's getting half a dozen parameter sets it will just send the SQL to the database and only the first set will be inserted.

Either move the list constructor and the Execute code inside the loop, or construct a DataTable instead and pass that as a set of result to a DAL method (which can use a DataAdapter to INSERT all rows in a single instruction).
 
Share this answer
 
Comments
Joshi_Aryan 28-Aug-21 6:58am    
please explain with the code please i m trying to inside loop of (list array) but not understand thanx
VB
For rw As Integer = 0 To DataGridView1.Rows.Count - 1

    qry = "insert into ItemMaster(D_Id,DNo,DName,PCS,Weight) values(@1,@2,@3,@4,@5)"
    
    ' Note also, move this command here
    Dim list As New List(Of SqlParameter)
    list.Add(New SqlParameter("@1", TextBox3.Text))
    list.Add(New SqlParameter("@2", DataGridView1.Rows(rw).Cells(1).Value))
    list.Add(New SqlParameter("@3", DataGridView1.Rows(rw).Cells(2).Value))
    list.Add(New SqlParameter("@4", DataGridView1.Rows(rw).Cells(3).Value))
    list.Add(New SqlParameter("@5", DataGridView1.Rows(rw).Cells(4).Value))
    

    If dal.execute(qry, list) Then
        MsgBox("Data inserted successfully")
    Else
        MsgBox("Error inserting data")
    End If

Next ' NB the end of the loop must be here, so the SQL command is executed for each row.
    
End Sub
 
Share this answer
 
v2
Comments
Joshi_Aryan 28-Aug-21 7:36am    
i m entered to 5 times then 5 times exicute to this code..? what should i do???

If dal.execute(qry, list) Then
MsgBox("Data inserted successfully")
Else
MsgBox("Error inserting data")
End If
Richard MacCutchan 28-Aug-21 7:38am    
What do you mean? If you have five rows in your DataGridView then (I assume) you will need to insert five rows into your database.
Joshi_Aryan 28-Aug-21 7:44am    
yes i m entered 5 rows then appear 5 times shown to this both msgbox
Richard MacCutchan 28-Aug-21 7:46am    
Yes, that is correct because that is what your code is doing. If you do not want the messagebox every time then change the code.
Joshi_Aryan 28-Aug-21 7:48am    
i want to only single time messagebox then what should i do..?
If you want to insert all rows in a single batch, try:
VB.NET
Dim sb As New StringBuilder("INSERT INTO ItemMaster (D_Id, DNo, DName, PCS, Weight) VALUES ")
Dim list As New List(Of SqlParameter)
list.Add(New SqlParameter("@D_Id", TextBox3.Text))

For rw As Integer = 0 To DataGridView1.Rows.Count - 1
    If rw <> 0 Then sb.Append(", ")
    sb.AppendFormat("(@D_Id, @DNo_{0}, @DName_{0}, @PCS_{0}, @Weight_{0})", rw)
    list.Add(New SqlParameter("@DNo_" & rw, DataGridView1.Rows(rw).Cells(1).Value))
    list.Add(New SqlParameter("@DName_" & rw, DataGridView1.Rows(rw).Cells(2).Value))
    list.Add(New SqlParameter("@PCS_" & rw, DataGridView1.Rows(rw).Cells(3).Value))
    list.Add(New SqlParameter("@Weight_" & rw, DataGridView1.Rows(rw).Cells(4).Value))
Next

If dal.execute(sb.ToString(), list) Then
MsgBox("Data inserted successfully")
Else
MsgBox("Error inserting data")
End If
 
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