Solved

If Primary Key ID already exists in datatable, how to not show error?

Posted on 2009-07-01
5
763 Views
Last Modified: 2012-05-07
I have a datatable that contains three columns: ID, firstName, lastName. This table is populated by the user clicking a linkbutton within a datalist. The datatable contents are displayed in a different region on the same page via a repeater control. The user can remove items from the datatable by clicking a linkbutton within the repeater control. This all works great.

Since I don't want the user to add the same record (person) to the datatable, I've made the ID column of my datatable the PRIMARY KEY to prevent the same ID from being submitted. However, if a user attempts to do this anyway, as you might imagine, they get an ASP.NET server error "Column 'ID' is constrained to be unique".

Is there a way, when the user clicks the linkbutton within the datalist, to check the ID column for the ID of the selected person and not take any action if the ID already exists? I don't want any error or messages to appear.

I've provided code below in case it helps. THANK YOU in advance for any insight you might provide.
Protected Sub AddToClinicianListFromOffices(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
 

        Dim ID As Integer = DataListOfficesTherapists.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
 

AddClinicianTable(ID, firstNameAddText, lastNameAddText, CType(Session("CliniciansTableSession"), DataTable))
 

 Me.RepeaterClinicians.DataSource = CType(Session("CliniciansTableSession"), DataTable).DefaultView
 

        Me.RepeaterClinicians.DataBind()

        Me.DataListOfficesTherapists.DataBind()

        ID = CInt(ID)

        firstNameAddText = ""

        lastNameAddText = ""
 

    End Sub
 
 

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

        Dim ID As Integer = DataListIssuesTherapists.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
 

        AddClinicianTable(ID, firstNameAddText, lastNameAddText, CType(Session("CliniciansTableSession"), DataTable))
 

        Me.RepeaterClinicians.DataSource = CType(Session("CliniciansTableSession"), DataTable).DefaultView
 

        Me.RepeaterClinicians.DataBind()

        Me.DataListOfficesTherapists.DataBind()

        ID = CInt(ID)

        firstNameAddText = ""

        lastNameAddText = ""
 

    End Sub

Open in new window

0
Comment
Question by:John0214
  • 3
5 Comments
 
LVL 12

Expert Comment

by:Cyber-spy
ID: 24754042
Not sure what type of database you are using, but most (including Access and SQL Server) can automatically generate the ID for you, so you don't have to add it yourself. This way it is guaranteed to be unique without you having to do anything!

In SQL2005/2008, right click on the table and select 'Design'
Select the ID column, then look at the Column Properties at the bottom of the dialog.
Scroll down until you find 'Identity Specification'. Click on the + sign to expand
Double click on the (Is Identity) row to set it to 'Yes'

In Access 2007, select the table and Click on 'Design View'. Make sure your ID column is of type 'AutoNumber'.
It will be similar in older versions of Access to, but I don't have any installed.

MySQL and other databases will also have some means of doing this.

Now, modify your AddClinicianTable() method so it doesn't take an ID property, nor send one to the database.
0
 

Author Comment

by:John0214
ID: 24754104
Cyber-spy, thank you for your speedy and thorough response. I really appreciate it!

Actually, I'm using a datatable that is stored in a session variable. This datatable is created when the user visits the website (created in the global.asax on session start). I just want to store data temporarily (much like a shopping cart)  during the user's visit.
0
 
LVL 2

Accepted Solution

by:
kbac earned 250 total points
ID: 24754454
Use simply try catch else for catching the errors, and act as you like on any error.

For this particular ID situation, you can do a select first and then decide if you want to insert or not. But the try catch resolves this issue as well. In debug mode, catch the duplicate ID error code, and manage it in your code in the catch area. Telling the user that the ID already exists is better than acting nonresponsive.
0
 

Author Comment

by:John0214
ID: 24755177
kbac, great suggestion! I will do this. Thank you!
0
 

Author Closing Comment

by:John0214
ID: 31598800
Thank you, kbac - it works!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

27 Experts available now in Live!

Get 1:1 Help Now