Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding datatable row index and deleting/removing

Posted on 2009-06-29
4
Medium Priority
?
1,117 Views
Last Modified: 2013-12-25
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.
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

Open in new window

0
Comment
Question by:John0214
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
prairiedog earned 2000 total points
ID: 24737290
This is how to get the row index  in your Button click event handler:

Dim lbtn As LinkButton = CType(sender, LinkButton)
Dim item As RepeaterItem = CType(lbtn.NamingContainer, RepeaterItem)
Dim rowIndex As Integer = item.ItemIndex
'Continue your code
 
0
 

Author Comment

by:John0214
ID: 24738172
Prairiedog, you have saved my life!!  You have no idea how stuck I was.

THANK YOU!!

John
0
 

Author Closing Comment

by:John0214
ID: 31597911
Thank you, prairiedog - as I said before, you saved my life on this. "Thanks" is not enough.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24739770
Glad I could help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

715 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