Monday, March 19, 2012

Check for Duplicate Records Before Insertion

Hi,

Does anyone have any sugestions as to the best way to achieve the following?

I want to display a list of products in a GridView. Against each product I want to place a button which will add the product to a "wish list" table.

I need to be able to check that the product has not already been inserted into the wish list.

Either suggested methods or links to tutorials will be appreciated.

Thanks in advance.

Just do a;

SELECT ProductID FROM WishListTable WHERE ProductID = <selected Product ID>

If you don't get an empty result set back you know the product is already there. Or if you are using SQL Server Stored Procedures;

IF NOT EXISTS (SELECT 1 FROM WishListTable WHERE ProductID = <selected Product ID>) INSERT INTO WishListTable ...

|||

Thanks for your answer.

I started searching for a tutorial on this and found one athttp://aspalliance.com/687

I've incorporated your suggestion and the tutorial into my system and it works a treat.

Thanks again

No comments:

Post a Comment