There is no row at position 1

Posted on 2005-05-05
Last Modified: 2012-05-05
Hello Experts,
I am trying to loop through a datatable and run a stored proc for each record to insert the values into a database.
The first row writes fine then I get the "There is no row at position 1" error.  I know there is more then one record in the datatable becase I can respone.write the row.count property.  
Here is my code:

        Dim i As Integer

        For i = 0 To Cart.Rows.Count - 1
            order_key = insSL.Parameters.Add(New SqlParameter("@order_key", SqlDbType.Int))
            order_key.Direction = ParameterDirection.Input
            order_key.Value = OrderKey

            document_no = insSL.Parameters.Add(New SqlParameter("@document_no", SqlDbType.VarChar, 20))
            document_no.Direction = ParameterDirection.Input
            document_no.Value = OrderKey

            line_no = insSL.Parameters.Add(New SqlParameter("@line_no", SqlDbType.Int))
            line_no.Direction = ParameterDirection.Input
            line_no.Value = Cart.Rows.Count 'Cart.Rows(i).Item("AID")

            line_type = insSL.Parameters.Add(New SqlParameter("@line_type", SqlDbType.VarChar, 50))
            line_type.Direction = ParameterDirection.Input
            line_type.Value = "Item"

            sell_to_cust_no = insSL.Parameters.Add(New SqlParameter("@sell_to_cust_no", SqlDbType.VarChar, 20))
            sell_to_cust_no.Direction = ParameterDirection.Input
            sell_to_cust_no.Value = Company.Rows(i).Item("co_key")

Thanks for any comments
Question by:ChadMarsh
    LVL 15

    Accepted Solution

    well propably there is row in cart.rows.count

    but not company.rows     sell_to_cust_no.Value = Company.Rows(i).Item("co_key")
    LVL 23

    Expert Comment

    there is absolutely no need to do it the way you have done. 100% not done.
    LVL 2

    Author Comment

    You are exactly right.  Can't believe I missed that.
    LVL 23

    Expert Comment

    Dim connection As New SqlConnection(..)
    Dim adapter As New SqlDataAdapter()
    Dim insertCommand As SqlCommand = connection.CreateCommand()
    insertCommand.CommandType = CommandType.StoredProcedure
    'all stored procedure types have a return parameter
    With insertCommand.Parameters
          insertCommand.Parameters(0).Direction = ParameterDirection.Return
          'the rest
          'the last parameter is the name of the column in the datatable
          .Add("@line_type",SqlDbType.VarChar,50).Value = "Item"
    End With
    adapter.InsertCommand = insertCommand

    you create a hybrid table that holds the added rows with the columns "document_no","line_no" and sell_to_cust_no"
    and use the Update method. That will do call the InsertCommand for each row.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now