Solved

Distinct Selection Using Datatable and Repeater Control

Posted on 2009-06-29
5
710 Views
Last Modified: 2013-12-25
Hello - thanks for taking a look at my ASP.NET question.

I have a datatable that contains three columns: ID, firstName, and lastName. I have a repeater control on my aspx page that is databound to this datatable and displays the firstName and lastName on the page.

The ID is not an auto-incremental primary key, rather the ID is pulled from another datasource related to the person's firstName and lastName.

Is there a way to prevent someone from adding the same record to the datatable? And, if not, a way to apply a Distinct Selection to the repeater?

For example, if I have a listing of names (showing the ID in this example)...

ID    firstName    lastName
1    John            Smith
2    Jim              Conner
3    Lisa             Stanton

...is there a way to not allow the user to enter "John Smith" twice?

Would it be better for me to make the ID column a Primary Key? And, if so, how would I avoid the ASP.NET error page regarding unique constraints?

Thank you for any insight you might provide - I know I am asking a rather broad-based question.
0
Comment
Question by:John0214
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24739495
create a UNIQUE KEY Constraint on FirstName,LastName
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24739514
Or else before doing the insert perform a check

INSERT INTO urTable (FirstName, LastName)
SELECT 'John', 'Smith'
WHERE NOT EXISTS (SELECT 1 FROM  urTable WHERE FirstName = 'John' and LastName = 'Smith' )
0
 

Author Comment

by:John0214
ID: 24739697
Thank you for the quick reply, aneeshattingal.

Since this is a Datatable residing in a Session variable, I'm not exactly sure how I would perform this. I am going to include the code so you can see what it looks like now.

The Sub "AddToList" and Function "AddJohnTable" are included. Hopefully it makes sense.

Basically I have a DataList displaying people's names on the aspx page. A user can 'select' a person from the list by clicking on a LinkButton and their selection appears on a different area of the same page in a Repeater control.

I'm hoping to avoid the insertion of duplicate records in the Datatable because I will need to include the information in the Datatable into an e-mail later in the process, but I'm not that far yet.
Private Function AddJohnTable(ByVal ID As Integer, ByVal firstname As String, ByVal lastname As String, ByVal myTable As DataTable)

        

Dim row As DataRow

row = myTable.NewRow()

row("ID") = ID

row("First Name") = firstname.ToString

row("Last Name") = lastname.ToString

myTable.Rows.Add(row)
 

End Function
 
 

Protected Sub AddToList(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
 

Dim ID As Integer = DataList1.DataKeys(e.Item.ItemIndex)

Dim firstNameAdd As Label = CType(e.Item.FindControl("firstNameLabel"), Label)

Dim lastNameAdd As Label = CType(e.Item.FindControl("lastNameLabel"), Label)

Dim firstNameAddText As String = firstNameAdd.Text

Dim lastNameAddText As String = lastNameAdd.Text
 

AddJohnTable(ID, firstNameAddText, lastNameAddText, CType(Session("JohnTable"), DataTable))
 

Me.Repeater1.DataSource = CType(Session("JohnTable"), DataTable).DefaultView
 

Me.Repeater1.DataBind()

Me.DataList1.DataBind()

ID = CInt(ID)

firstNameAddText = ""

lastNameAddText = ""
 

End Sub

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24739943
i am really not a .net programmer, but i think you need to change the definition of this
AddJohnTable(ID, firstNameAddText, lastNameAddText, CType(Session("JohnTable"), DataTable))
 
0
 

Accepted Solution

by:
John0214 earned 0 total points
ID: 24758081
I decided to go with the ID as the PRIMARY KEY, then do TRY CATCH error handling to prevent the insertion of the duplicate ID. It works!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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.
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…

932 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

12 Experts available now in Live!

Get 1:1 Help Now