Trouble adding rows to DataTable : Error = System.IndexOutOfRangeException: Cannot find column 2 
Author Message
 Trouble adding rows to DataTable : Error = System.IndexOutOfRangeException: Cannot find column 2

Hello everyone, I have a strange problem.  It's hard to explain so
please bare with me.  I have 2 generic datatables produced in a
different page.  I only need to two columns from these datatables so I
remove the other 12 columns from each datatable.  So the result is I
have 2 independant datatables that bind to two independant listboxes

Now upon a button click I try to add the selected row or item (from
the listbox) into the other.  Now I need this sorted so I try to add
the row to the other datatable, put it into a dataview, sort, and then
rebind.  This should be easy except I get the following error:

System.IndexOutOfRangeException: Cannot find column 2.

Now here is some code to clarify (hopefully):

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
             PopGrid()
        End If
End Sub
Public Sub PopGrid()
Dim primarykey(1) As DataColumn
Dim dt_ins As DataTable
Dim dt_rem As DataTable
Try
    dt_ins = New DataTable("INS_ENROLL")
    dt_ins = Session("dtInsured_Filtered_addremove_ins")
    dt_ins.Constraints.Clear()
    primarykey(0) = dt_ins.Columns("EMPL_ID_PLAN")
    dt_ins.PrimaryKey = primarykey
' here is the list of original columns:
' FULLNAME, LDAP_REG_ID, EMPL_ID, NAU_ID, NATIONAL_ID, INS_PLAN,
PLAN_DURATION
' UNITS, DEGREE_SEEKING, APPLIED_AMT, NAME_ID, EMPL_ID_PLAN, EMAIL,
SELECTED
    dt_ins.Columns.Remove("LDAP_REG_ID")
    dt_ins.Columns.Remove("NAU_ID")
    dt_ins.Columns.Remove("NATIONAL_ID")
    dt_ins.Columns.Remove("INS_PLAN")
    dt_ins.Columns.Remove("PLAN_DURATION")-
    dt_ins.Columns.Remove("UNITS")
    dt_ins.Columns.Remove("DEGREE_SEEKING")
    dt_ins.Columns.Remove("APPLIED_AMT")-
    dt_ins.Columns.Remove("EMAIL")
    dt_ins.Columns.Remove("SELECTED")
    dt_ins.Columns.Remove("FULLNAME")
    dt_ins.Columns.Remove("EMPL_ID")

    dvIns = New DataView(dt_ins)
    Session.Contents.Remove("dtInsured_Filtered_addremove_ins")

    With clbInsuredList
        .DataSource = dvIns
        .DataTextField = "NAME_ID"
        .DataValueField = "EMPL_ID_PLAN"
        .DataBind()
    End With
    Session("dt_local_ins") = dt_ins

    dt_rem = New DataTable("INS_REMOVED")
    dt_rem = Session("dtInsured_Filtered_addremove_removed")
    dt_rem.Constraints.Clear()
    primarykey(0) = dt_rem.Columns("EMPL_ID_PLAN")
    dt_rem.PrimaryKey = primarykey
    dt_rem.Columns.Remove("LDAP_REG_ID")
    dt_rem.Columns.Remove("NAU_ID")
    dt_rem.Columns.Remove("NATIONAL_ID")
    dt_rem.Columns.Remove("INS_PLAN")
    dt_rem.Columns.Remove("PLAN_DURATION")
    dt_rem.Columns.Remove("UNITS")
    dt_rem.Columns.Remove("DEGREE_SEEKING")
    dt_rem.Columns.Remove("APPLIED_AMT")
    dt_rem.Columns.Remove("EMAIL")
    dt_rem.Columns.Remove("SELECTED")
    dt_rem.Columns.Remove("FULLNAME")
    dt_rem.Columns.Remove("EMPL_ID")

    dvRem = New DataView(dt_rem)
    Session.Contents.Remove("dtInsured_Filtered_addremove_removed")

    With clbRemovededList
        .DataSource = dvRem
        .DataTextField = "NAME_ID"
        .DataValueField = "EMPL_ID_PLAN"
        .DataBind()
    End With
    Session("dt_local_rem") = dt_rem
Catch e As Exception
    Response.Write(e)
End Try
End Sub

Private Sub btnPut_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPut.Click
Dim count As Int16 = 0
Dim dr As DataRow
Dim dc As DataColumn
Try
    ' loop through items in list
    Do While count < clbInsuredList.Items.Count
        If clbInsuredList.Items(count).Selected Then
            ' makes new row
            dr = Session("dt_local_rem").NewRow()
            ' next two lines populate row
' **********************************
' here I am writing the column names to the screen
' output =
'-->NAME_ID<--
'-->EMPL_ID_PLAN<--
' so there are only 2 columns . . .
' **********************************

            For Each dc In Session("dt_local_rem").Columns
                Response.Write("-->" & dc.ColumnName & "<--<br>")
            Next
            dr("NAME_ID") = clbInsuredList.Items(count).Text
            dr("EMPL_ID_PLAN") = clbInsuredList.Items(count).Value
            ' actually adds row to dataset
            Session("dt_local_rem").Rows.Add(dr)  <-- error here with message
(see above)
            ' finds record we want to delete
            dr = Session("dt_local_ins").Rows.find(clbInsuredList.Items(count).Value)
            ' deletes row
            dr.Delete()
            ' removes item from list, this is so
            ' we don't have to rebind, probably faster
            clbInsuredList.Items.Remove(clbInsuredList.Items(count))
        Else
            count += 1
        End If
    Loop
    ' rebind removed list
    dvRem = New DataView(Session("dt_local_rem").Tables("FHC_ADM.INS_REMOVED"))
    dvRem.Sort() = "NAME_ID"
    With clbRemovededList
        .DataSource = dvRem
        .DataTextField = "NAME_ID"
        .DataValueField = "REG_ID_PLAN"
        .DataBind()
    End With
Catch ex As Exception
    Response.Write(ex)
End Try
End Sub

I really hope this is an easy problem to fix because my computer is
getting tired of my head slamming into it :)

I even response.write'd Session("dt_local_rem").Columns.Count
and it printed 2

If I am just being dumb, please be gentle :)

Any help would be greatly appreciated!

Ryan



Mon, 19 Sep 2005 07:18:21 GMT  
 Trouble adding rows to DataTable : Error = System.IndexOutOfRangeException: Cannot find column 2
Perhaps your column collection is zero based.  Therefore you'd want to
reference columns zero and one (instead of one and two.)

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net


Quote:
> Hello everyone, I have a strange problem.  It's hard to explain so
> please bare with me.  I have 2 generic datatables produced in a
> different page.  I only need to two columns from these datatables so I
> remove the other 12 columns from each datatable.  So the result is I
> have 2 independant datatables that bind to two independant listboxes

> Now upon a button click I try to add the selected row or item (from
> the listbox) into the other.  Now I need this sorted so I try to add
> the row to the other datatable, put it into a dataview, sort, and then
> rebind.  This should be easy except I get the following error:

> System.IndexOutOfRangeException: Cannot find column 2.

> Now here is some code to clarify (hopefully):

> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>         If Not Page.IsPostBack Then
>              PopGrid()
>         End If
> End Sub
> Public Sub PopGrid()
> Dim primarykey(1) As DataColumn
> Dim dt_ins As DataTable
> Dim dt_rem As DataTable
> Try
>     dt_ins = New DataTable("INS_ENROLL")
>     dt_ins = Session("dtInsured_Filtered_addremove_ins")
>     dt_ins.Constraints.Clear()
>     primarykey(0) = dt_ins.Columns("EMPL_ID_PLAN")
>     dt_ins.PrimaryKey = primarykey
> ' here is the list of original columns:
> ' FULLNAME, LDAP_REG_ID, EMPL_ID, NAU_ID, NATIONAL_ID, INS_PLAN,
> PLAN_DURATION
> ' UNITS, DEGREE_SEEKING, APPLIED_AMT, NAME_ID, EMPL_ID_PLAN, EMAIL,
> SELECTED
>     dt_ins.Columns.Remove("LDAP_REG_ID")
>     dt_ins.Columns.Remove("NAU_ID")
>     dt_ins.Columns.Remove("NATIONAL_ID")
>     dt_ins.Columns.Remove("INS_PLAN")
>     dt_ins.Columns.Remove("PLAN_DURATION")-
>     dt_ins.Columns.Remove("UNITS")
>     dt_ins.Columns.Remove("DEGREE_SEEKING")
>     dt_ins.Columns.Remove("APPLIED_AMT")-
>     dt_ins.Columns.Remove("EMAIL")
>     dt_ins.Columns.Remove("SELECTED")
>     dt_ins.Columns.Remove("FULLNAME")
>     dt_ins.Columns.Remove("EMPL_ID")

>     dvIns = New DataView(dt_ins)
>     Session.Contents.Remove("dtInsured_Filtered_addremove_ins")

>     With clbInsuredList
> .DataSource = dvIns
> .DataTextField = "NAME_ID"
> .DataValueField = "EMPL_ID_PLAN"
> .DataBind()
>     End With
>     Session("dt_local_ins") = dt_ins

>     dt_rem = New DataTable("INS_REMOVED")
>     dt_rem = Session("dtInsured_Filtered_addremove_removed")
>     dt_rem.Constraints.Clear()
>     primarykey(0) = dt_rem.Columns("EMPL_ID_PLAN")
>     dt_rem.PrimaryKey = primarykey
>     dt_rem.Columns.Remove("LDAP_REG_ID")
>     dt_rem.Columns.Remove("NAU_ID")
>     dt_rem.Columns.Remove("NATIONAL_ID")
>     dt_rem.Columns.Remove("INS_PLAN")
>     dt_rem.Columns.Remove("PLAN_DURATION")
>     dt_rem.Columns.Remove("UNITS")
>     dt_rem.Columns.Remove("DEGREE_SEEKING")
>     dt_rem.Columns.Remove("APPLIED_AMT")
>     dt_rem.Columns.Remove("EMAIL")
>     dt_rem.Columns.Remove("SELECTED")
>     dt_rem.Columns.Remove("FULLNAME")
>     dt_rem.Columns.Remove("EMPL_ID")

>     dvRem = New DataView(dt_rem)
>     Session.Contents.Remove("dtInsured_Filtered_addremove_removed")

>     With clbRemovededList
> .DataSource = dvRem
> .DataTextField = "NAME_ID"
> .DataValueField = "EMPL_ID_PLAN"
> .DataBind()
>     End With
>     Session("dt_local_rem") = dt_rem
> Catch e As Exception
>     Response.Write(e)
> End Try
> End Sub

> Private Sub btnPut_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnPut.Click
> Dim count As Int16 = 0
> Dim dr As DataRow
> Dim dc As DataColumn
> Try
>     ' loop through items in list
>     Do While count < clbInsuredList.Items.Count
> If clbInsuredList.Items(count).Selected Then
>     ' makes new row
>     dr = Session("dt_local_rem").NewRow()
>     ' next two lines populate row
> ' **********************************
> ' here I am writing the column names to the screen
> ' output =
> '-->NAME_ID<--
> '-->EMPL_ID_PLAN<--
> ' so there are only 2 columns . . .
> ' **********************************

>     For Each dc In Session("dt_local_rem").Columns
> Response.Write("-->" & dc.ColumnName & "<--<br>")
>     Next
>     dr("NAME_ID") = clbInsuredList.Items(count).Text
>     dr("EMPL_ID_PLAN") = clbInsuredList.Items(count).Value
>     ' actually adds row to dataset
>     Session("dt_local_rem").Rows.Add(dr)  <-- error here with message
> (see above)
>     ' finds record we want to delete
>     dr =

Session("dt_local_ins").Rows.find(clbInsuredList.Items(count).Value)
Quote:
>     ' deletes row
>     dr.Delete()
>     ' removes item from list, this is so
>     ' we don't have to rebind, probably faster
>     clbInsuredList.Items.Remove(clbInsuredList.Items(count))
> Else
>     count += 1
> End If
>     Loop
>     ' rebind removed list
>     dvRem = New

DataView(Session("dt_local_rem").Tables("FHC_ADM.INS_REMOVED"))

- Show quoted text -

Quote:
>     dvRem.Sort() = "NAME_ID"
>     With clbRemovededList
> .DataSource = dvRem
> .DataTextField = "NAME_ID"
> .DataValueField = "REG_ID_PLAN"
> .DataBind()
>     End With
> Catch ex As Exception
>     Response.Write(ex)
> End Try
> End Sub

> I really hope this is an easy problem to fix because my computer is
> getting tired of my head slamming into it :)

> I even response.write'd Session("dt_local_rem").Columns.Count
> and it printed 2

> If I am just being dumb, please be gentle :)

> Any help would be greatly appreciated!

> Ryan



Mon, 19 Sep 2005 07:21:56 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. problems adding datatable row in code - overrideable error

2. Add row to datatable from datagrid without leaving current row with cursor

3. Event called on adding row in dataTable

4. AutoIncrement and method DataTable.Rows.Add(Object())

5. Trouble with adding row to datagrid

6. Trouble Adding a row to a table - runtime

7. Trouble transferring data from 1 DBGrid to another and adding new rows

8. ADO.NET DataSet.TableName.AddTableNameRow() inserts an extra row

9. System.Runtime.InteropServices.VTableCallsNotSupportedException

10. Error creating Windowhandle - System.ComponentModel.Win32Exception

11. Error in System.Windows.Forms.Form.UpdateLayered()

12. System.TypeInitializationException Error (- Urgent!)

 

 
Powered by phpBB® Forum Software