dodgerfan
asked on
Check for duplicate values before letting user insert
I have an asp.net page that allows users to insert new records into a table. When they enter the data, I want to do a search of the empnum field to ensure that value does not exist already in the system. So before inserting, the page searches the users table for the empnum entered by the user. If it exits, show a message stating it already exists. If not, insert the record. What is the best way to do this? I'm using C# code behind.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
YZlat,
I'm grabbing the empum from a value entered into a textbox. The value can contain letters, so it's a string. Your code is erroring out on the line: int count = (int)cmd.ExecuteScalar(). It does not know what to do, I guess. Can I fix that?
I'm grabbing the empum from a value entered into a textbox. The value can contain letters, so it's a string. Your code is erroring out on the line: int count = (int)cmd.ExecuteScalar(). It does not know what to do, I guess. Can I fix that?
can you post your sql statement?
your sql should be something like
sql="SELECT COUNT(*) FROM Table1 WHERE empnum='" + TextBox1.Text + "'";
sql="SELECT COUNT(*) FROM Table1 WHERE empnum='" + TextBox1.Text + "'";
if the field in your where clause is of type text, you will need single quotes around the value
WHERE empnum = 'Test'
WHERE empnum = 'Test'
ASKER
Got it, thanks for the help. I just needed to get the syntax with those single and double quotes correct.
Open in new window