John0214
asked on
Finding datatable row index and deleting/removing
Hello - thank you for taking a moment to look at the ASP.NET issue I am having.
I have created a Datatable that has three columns: ID, firstName, lastName. The ID column is the primary key. Not to get too mired in the details, but this datatable acts like a shopping cart (but isn't) where users can select people from a list (provided by a DataList) and those selections are stored in the datatable and displayed in a different area on the same page by a Repeater control. This part seems to be working fine.
A requirement of this project is to be able to REMOVE those people from the datatable. I am doing this by placing an LinkButton within the Repeater that displays the text "REMOVE" - and this is where I am having problems. I have included the code I am trying to use to find the ID of the particular person (row), and then remove/delete it.
When I click the LinkButton in the Repeater control, I am receiving the following ASP.NET error:
System.FormatException: Input string was not in a correct format.
A side-note: I am not an expert programmer by any means, so I appreciate any and all suggestions.
I have created a Datatable that has three columns: ID, firstName, lastName. The ID column is the primary key. Not to get too mired in the details, but this datatable acts like a shopping cart (but isn't) where users can select people from a list (provided by a DataList) and those selections are stored in the datatable and displayed in a different area on the same page by a Repeater control. This part seems to be working fine.
A requirement of this project is to be able to REMOVE those people from the datatable. I am doing this by placing an LinkButton within the Repeater that displays the text "REMOVE" - and this is where I am having problems. I have included the code I am trying to use to find the ID of the particular person (row), and then remove/delete it.
When I click the LinkButton in the Repeater control, I am receiving the following ASP.NET error:
System.FormatException: Input string was not in a correct format.
A side-note: I am not an expert programmer by any means, so I appreciate any and all suggestions.
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dt As New DataTable
dt = CType(Session("JohnTable"), DataTable)
' Find the ID of the person (row)
Dim rowIndex As Integer
rowIndex = ViewState(CType(Session("JohnTable"), DataTable).Rows.IndexOf(CType(Session("JohnTable"), DataTable).Rows.Find(ID)))
'Delete the row
Dim ival As Integer
For ival = 0 To dt.Rows.Count - 1
If dt.Rows(ival).Item("ID") = rowIndex Then
dt.Rows(ival).Delete()
Exit For
End If
Next
dt.AcceptChanges()
Me.Repeater1.DataSource = dt
Me.Repeater1.DataBind()
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, prairiedog - as I said before, you saved my life on this. "Thanks" is not enough.
Glad I could help.
ASKER
THANK YOU!!
John