SolvedPrivate

how to avoid inserting duplicate records C# with LINQ to sql

Posted on 2014-02-12
2
33 Views
Last Modified: 2016-02-10
I have this snippet of LINQ code to add a record to my table
protected void cmd_Click(object sender, EventArgs e)
{
   AddressTbl AddressTbl = new AddressTbl();
  AddressTbl.Bsc_CTL_NO = Convert.ToInt32(txtBSC_NO_Text);
  AddressTbl.EmpTableType = intEmpTableType;
  AddressTbl.Address_Desc = txt Address_Desc.Text;




MyTelephoneDataContext.AddressTbls.InsertOnSubmit(AddressTbl);
MyTelephoneDataContext.SubmitChanges();

viewDataOnGrid();
}

I need to prevent from adding a duplicate records.
The combination of two columns (AddressTbl.Bsc_CTL_NO and AddressTbl.EmpTableType ) should be unique.
How could I do such a thing ?
Please help.
0
Comment
Question by:Mohammad Alsolaiman
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39855518
You can simply add that as a constraint in the DB ( a unique key constraint / or a composite primary key).

Then the insert to the DB fails with PK error, you can capture that and throw the same to the user.
0
 

Author Closing Comment

by:Mohammad Alsolaiman
ID: 39937223
thanks a lot
0

Featured Post

Free Trending Threat Insights Every Day

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

18 Experts available now in Live!

Get 1:1 Help Now