Solved

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

Posted on 2009-07-01
5
765 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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

832 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