View Full Version : stop duplicate records saving on database
mercea
09-03-2007, 06:20 PM
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 mean Registration-Login ?
Simply check username against ones existing table.
If exist, reject with 'Username already taken' message.
nikkiH
09-04-2007, 08:00 PM
Personally, I'd create a unique constraint and let it throw the error, catch it, and display the message. If you need unique, best to do that at the database level so it is not UI dependent.
Just my 2 cents.
ad5qa
09-04-2007, 10:18 PM
Would SELECT COUNT WHERE USER IS XXXX be a good idea?
If 0 then there is no user otherwise it would be 1 or more hopefully just 1.
nikkiH
09-04-2007, 10:32 PM
You could.
Or you can add
WHERE NOT EXISTS
clause to your insert to just prevent the duplicate, and check the result of the update for the number of rows affected (1 for insert, 0 for none).
But you open your back end to someone using something other than your user interface to register duplicates. Even if you do check things at the UI level, you should still pop on a unique constraint/index to CYA. It's pretty easy to do.
mercea
09-06-2007, 03:10 PM
thats what i originally intended to do but i dont know how to go about it and i thought of using the compare validator. what suggestions do u have?
SouthwaterDave
09-08-2007, 07:57 PM
You could use a CustomValidator with these properties, amongst others:
EnableClientScript="false" OnServerValidate="CustomValidator1_ServerValidate"
and server side code something like this:
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Do your database test here
If all is well Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.