0

I have the following code to loop through my datatable (dtItem) and insert each row into the database. However, i can only insert the last row of the datatable. How do i insert all rows? Here is my code.

Dim dtRow As DataRow

            For Each dtRow In dtItem.Rows

                dtRow.ToString.Split("|")

                Dim xBinCode As String = dtRow(0)
                Dim xLocationCode As String = dtRow(7)
                Dim xItemNo As String = dtRow(1)
                Dim xQuantity As String = dtRow(2)
                Dim xCountNo As String = dtRow(8)

                cmd.CommandText = "INSERT INTO tblItems (BinCode, LocationCode, ItemNo, Quantity, CountNo) values('" & xBinCode & "','" & xLocationCode & "','" & xItemNo & "','" & xQuantity & "','" & xCountNo & "')"

            Next

2 Answers 2

2

if you use data reader you need to open connection and use command

cmd.ExecuteNonQuery

after

cmd.CommandText = "INSERT INTO tblItems (BinCode, LocationCode, ItemNo, Quantity, CountNo) values('" & xBinCode & "','" & xLocationCode & "','" & xItemNo & "','" & xQuantity & "','" & xCountNo & "')"

statement

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you're missing something here.

You can do that concatening the SQL insert sentences for each row before sending to server.

If you want to send one row at time, configure and execute your command inside the for loop.

You can also use a DbDataAdapter. Loak at the following pages:

http://msdn.microsoft.com/es-es/library/at8a576f(v=vs.90).aspx

http://msdn.microsoft.com/es-es/library/system.data.sqlclient.sqldataadapter.insertcommand(v=vs.90).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.