Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net/SQL Prevent Duplicates

Hi

In my ASP.net web app I am writing code that loops through a GridView and inserts data into a SQL table.
I want to prevent duplicates based o two columns [Username] and [Item ID].

What I the best way to do this?
Avatar of Misha
Misha
Flag of Russian Federation image

Usually read data from SQL table to GridView, but to insert all data from GridView in DataBase. And not check data from GridView  for existence duplicates. Are you sure, that this approch is correct and need?

If you have data from your GridView, you can use this code to get data without duplicates.
var result = YourList.Select(o => new { x.Username, x.ItemID })
                                       .Distinct();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Thanks.