Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
SolvedPrivate

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

Posted on 2014-02-12
2
Medium Priority
?
43 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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:…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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