SolvedPrivate

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

Posted on 2014-02-12
2
39 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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