C# Windows Validation by ErrorProvider
In ASP.Net 2.0 we use different Validation controls from Toolbox for validations (Just Drag & Drop, No need of writing code in C#). But in Case of C# windows application this feature is not available so we use Error Provider control to do any type of validation.

For example:-
If we take a Employee data…
- Name (text)
- Age (Integer)
- Salary (Double)
Form looks like

In ADD button click you need to write the following code.
private void ADD_Click(object sender, EventArgs e)
{
if (is_validate())
{
errorProvider1.Clear();
// Write Coding to add into database
MessageBox.Show(“Data Successfully Added”);
}
}
private bool is_validate()
{
bool no_error = true;
if (txtname.Text == string.Empty)
{
errorProvider1.SetError(txtname, “Text Missing”);
no_error = false;
}
else
{
// Clear all Error Messages
try
{
int i = Convert.ToInt32(txtage.Text);
}
catch
{
errorProvider1.Clear(); // Clear all Error Messages
errorProvider1.SetError(txtage, “Enter Valid Age”);
return false;
}
try
{
double j = Convert.ToDouble(txtsalary.Text);
}
catch
{
errorProvider1.Clear(); // Clear all Error Messages
errorProvider1.SetError(txtsalary, “Enter Valid Salary”);
return false;
}
}
return no_error;
}
Here one Error Provider Control can be used for multiple fields.
That’s it. It’s all done. Its Very Simple isn’t it??
it is very usefull…
thanks
Very useful article,keep it up Ganesh Mohan
Hi Ganesh where are u working?
Thanks for your Comments…
I am working in Heurion Consulting Pvt Ltd, Bangalore
Thank you Ganesh . It is very useful article.
thank you Ganesh Mohan ji,
thank for this article u have drag guide line for me at your side….
i am stating in software development field you meet me very early thank you so much & good luck && Happy and Prosperous New Year to you……………
Thank u,Thank u very much……..
This code helped me a lot for the form validation.
could you pls tell me about the other adresses on net where u publish ur code..
i’m sure this will help me a lot..
thank you
It was good Ganesh sir, but my code is not showing any error ,but still nt working….. nt any syntax error even,plz help!!!