Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

22 Experts available now in Live!

Get 1:1 Help Now