SolvedPrivate

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

Posted on 2014-02-12
2
38 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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

679 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