Click here to Skip to main content
15,029,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the easiest way to validate textboxes that need to be checked before they go through. I have 2 tables I have to insert my data into as well and that's just for one group, in total I have 3 groups but just a helpful tip towards the one will be fine.

I have the main table which is 'Person' and then 3 sub tables; 'Student', 'Teacher', 'Administration'.

Not all the textboxes need to be validated. (These two don't need to be validated sAdd2.Text and StudentType.Text).

//Edited

I'm now getting an error message with the "var pid = personTableAdapter..." with the var pid. "Embedded statement cannot be a declaration or labeled statement". Is this a decent way of trying to validate before it goes through to the database?
My normal code is

C#
string errorMessage = "";

bool cancel = false;

if (string.IsNullOrEmpty(this.sFirstName.Text) || string.IsNullOrEmpty(this.sSurname.Text) || string.IsNullOrEmpty(this.sPhone.Text) || string.IsNullOrEmpty(this.sAdd1.Text) || string.IsNullOrEmpty(this.sSuburb.Text) || string.IsNullOrEmpty(this.sState.Text) || string.IsNullOrEmpty(this.sPostcode.Text));

//This control fails validation: Name cannot be empty.

if (cancel == true)

this.errorProvider1.SetError(sFirstName, "You must provide your first name");

errorMessage += "Enter your First name" + "/r/n";

this.errorProvider1.SetError(sSurname, "You must provide your surname");

errorMessage += "Enter your Surname" + "/r/n";

this.errorProvider1.SetError(sPhone, "You must provide your phone number");

errorMessage += "Enter your Phone number" + "/r/n";

this.errorProvider1.SetError(sAdd1, "You must provide your address");

errorMessage += "Enter your Address" + "/r/n";

this.errorProvider1.SetError(sSuburb, "You must provide your suburb");

errorMessage += "Enter your Suburb" + "/r/n";

this.errorProvider1.SetError(sState, "You must provide your state");

errorMessage += "Enter your State" + "/r/n";

this.errorProvider1.SetError(sPostcode, "You must provide your postcode");

errorMessage += "Enter your Postcode" + "/r/n";

if (cancel == false)

var pid = personTableAdapter.InsertPerson(sFirstName.Text, sSurname.Text, Convert.ToDateTime(sDoB.Text), sPhone.Text, sAdd1.Text, sAdd2.Text, sSuburb.Text, sState.Text, sPostcode.Text, StudentType.Text);

studentTableAdapter.Insert(Convert.ToInt32(pid), Convert.ToDateTime(sSDate.Text), sQCode.Text);

MessageBox.Show(sFirstName.Text + " " + sSurname.Text + " has been added. The Student ID is " + pid);

this.personTableAdapter.Fill(this._30002195DataSet.Person);

this.studentTableAdapter.Fill(this._30002195DataSet.Student);

this.Hide();

Admin f1 = new Admin();

f1.Show();


Thanks for your help! :)
Posted
Updated 15-Jul-15 2:04am
v4

You can user Required field Validator to do that.
Follow the below link for more details.

http://asp.net-tutorials.com/validation/required-field-validator/[^
   
Comments
Member 11525453 29-Jun-15 6:49am
   
Sorry, should've mentioned that it is a windows form not a web form

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900