Question How to Prevent Duplicate entry error

vb_newb

Member
Joined
Jan 31, 2013
Messages
7
Programming Experience
Beginner
can anyone help me on how to prevent duplicate entry in my project....

i just want to how to prevent this in happening before clicking the save button...

ex:

i set the username as the primary key

in my username textbox i want that when i type something, if it already exists there should be a warning that that username is not available so that there is no error after i try to save it..
 
Use the text changed event to fire off the validation

Sent from my GT-I9100 using Tapatalk 2

Absolutely not. The TextChanged event is raised every time the Text property changes. If you enter a user name that is 10 characters long, you're not going to want to validate 10 times.

You should either validate when the user finishes typing in the TextBox or when they try to save. It's up to you to decide which is more appropriate based on your desired user experience. If you choose the former then handle the Validating event of the TextBox. If you choose the latter then use the Click event of the Save Button.

Regardless of when you do the validation, it's simply a case of querying the database to see if there's a record with that value, which means a SELECT with a WHERE clause. You don't care about the data itself, only whether it exists, so the logical option is to use a COUNT and get back either zero or non-zero value or else is EXISTS to get back true or false.
 
To expand my answer.

Text changed fires every time a character is entered on a winform, so handling focus is maybe better. Text changed fires after loss of focus on web form. So depends.

Sent from my GT-I9100 using Tapatalk 2
 
To expand my answer.

Text changed fires every time a character is entered on a winform, so handling focus is maybe better. Text changed fires after loss of focus on web form. So depends.

Sent from my GT-I9100 using Tapatalk 2
I guess we're both showing our focus: mine being WinForms and yours being WebForms. Re-reading the original post, there's nothing to specifically suggest one over the other. the fact that we each interpreted something different is perfect example of why people need to provide a proper explanation of their problem and not just assume that we'll know things that we can't possibly know.
 
Back
Top