Tuesday, March 27, 2012

Duplicate records on database

hi all,

How do i avoid duplicate records on my database? i have 4 textboxes that collect user information and this information is saved in the database. when a user fills the textboxes and clicks the submit button, i want to check through the database if the exact records exist in the database before the data is saved. if the user is registered on the database, he wont be allowed to login. how can i acheive this?

i thought of using the comparevalidator but i'm not sure how to proceed.

thanks

You can check for the existence of the record before you insert to avoid duplicates.

IF NOT EXISTS ( SELECT * FROM YourTable WHER <Condition>)

BEGIN

INSERT INTO ...

END

|||

1declare @.login_namevarchar(50)2declare @.messagevarchar(100)34set @.login_name ='CS4Ever'56IFEXISTS (SELECT 1FROM RegisterationTableWHERE LoginName = @.login_name)7BEING-- user already registered89set @.message ='Sorry ' + @.login_name +', you are already registered!'1011END-- user already registered12ELSE-- user not registered yet13BEGIN-- user not registered yet1415insert into RegisterationTable (LoginName)values (@.login_name)1617if(@.@.rowcount = 1)18begin-- user added sucessfully19set @.message = @.login_name +', you have been registered sucessfully.'20end-- user added sucessfully21else-- faild in adding user22set @.message ='Registereation filed', please contact registeration office!23begin-- faild in adding user24end-- faild in adding user2526END-- user not registered yet
 
Good luck.
sql

No comments:

Post a Comment