Last post Jan 05, 2009 07:22 AM by Lee Dumond
Oct 29, 2007 12:21 PM|Hornwood509|LINK
Having a spot of bother with Validation. I have a Details view All setup to a DataSource with Insert. Works wonderfully. I've got required field validators where they need to be and they work. My slight issue is, I want to be able to validate if an integer
entered into a textbox EXISTS in the Database, and if it does, ALLOW the transaction to take place. Otherwise if the integer DOES NOT exist in the DataBase, a validator must pop up telling them the number does not exist. to make matters slightly more complicated...
[:)] I need another condition to be true as well...
So I wrote an SQL to sum up the validation.
"Select JobNumber from Job Where Flag = false"
This will return a range of numbers (int) from my table. I want to validate my textbox value against them and make sure that whatever is typed into my textbox EXISTS in the database.
I found this link
http://msdn2.microsoft.com/en-us/library/s5z00s5e(vs.80).aspx Which explaines the use of a custom validator, but I'm unable to modify it for my needs.
Can anybody help me?
Oct 29, 2007 04:03 PM|pixelsyndicate|LINK
The greatness of the custom validator is that it can do serverside or clientside validation. In your case, you probably need to do serverside since you want to look at database values.
Here's something quick and dirty that I used to validate a textbox value against the ID's in a database table containing US STATES. The ID's in the states table range from 1 to 56.
In my tests, if the value returned in the SQL was equal to the value searched on, then TRUE was returned, else FALSE was returned. That boolean value was then passed to the serverside args.IsValid property which is exposed in the eventhandler for the customvalidator
Here is the textbox, required field validator, custom validator and button which causes validation: note I have placed them all in their own validation group as to not cause validation on any other controls.
ID Doesn't Exist!
args.IsValid = ValidateIDExists();
Here is the code for the SQLHelper method getRowValue()
value = objMSSQLCmd.ExecuteScalar() +
HOpe this helps. It's not pretty, but it should show the process in a step-by-step situation.
Oct 31, 2007 09:00 AM|Hornwood509|LINK
Hey Pixel. Thanx eversomuch for the reply.I have worked through the example you gave me and managed to bend it to my whims. I had a slight bit of bother using the SQLHelper class you had stated in your code, but decieded against it in the end. if anybody
who reads this thread is interested in the SQLHelper, but does not know about it,
http://aspnet.4guysfromrolla.com/articles/070203-1.aspx this link should help them out. My custom Validator (inside of a databound DetailsView defaulted to insert) code is below. I'm not
sure i would have made it this far without your help. Thanx again :)
// this is the event handler for the customvalidation control
value = comm.ExecuteScalar() +
Dec 15, 2008 04:29 PM|nkair19|LINK
I am trying to accomplish a similar thing...i have a text box where the user needs to enter a serial number...I have this text in a custom create new wizard template....what i want to do is i have an sql database with the serial numbers...when the user enters
a username, password, serial number, etc and hits the button to create the user account, i want to be able to check whether the serial number is valid against the database and if so create the user, otherwise throw an error. I follow the code until here:
i dont have a gridview of the database or anything, i just want the custom validator to check the sql database for the serial number.
Jan 05, 2009 07:22 AM|Lee Dumond|LINK