System.IndexOutOfRange Exception Sending Gridview Values to DataTable

Posted by SidC on Stack Overflow See other posts from Stack Overflow or by SidC
Published on 2010-04-11T22:47:05Z Indexed on 2010/04/11 22:53 UTC
Read the original article Hit count: 302

Filed under:
|
|

Hello,

I am writing an ASP.NET 3.5 application and need to send gridview values to a datatable for use in a listbox control as part of a quote process. I have written the following VB code in my Page_Load:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim dtSelParts As DataTable = New DataTable("dtSelParts")
    Dim column As DataColumn = New DataColumn
    column.ColumnName = "PartName"
    column.ColumnName = "PartNumber"
    column.ColumnName = "Quantity"
    dtSelParts.Columns.Add(column)
    For Each row As GridViewRow In MySearch.Rows
        Dim drSelParts As DataRow
        drSelParts = dtSelParts.NewRow()
        For i As Integer = 0 To row.Cells.Count - 1
            drSelParts(i) = row.Cells(i).Text
        Next

    Next


End Sub

When I run the partsearch.aspx page, I enter values in the row textbox for parts I want included in the listbox (to be included in quote). However, I receive the error message System.Index.OutOfRangeException: Cannot find column 1 which occurs on the line drSelParts(i) = row.Cells(i).Text.

How might I correct the code and resolve the error?

Thanks, Sid

© Stack Overflow or respective owner

Related posts about asp.net-3.5

Related posts about datatable