SolvedPrivate

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

Posted on 2014-02-12
2
40 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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