Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
good morning
i am making a window form
and i am using error provider to validate controls.

when i click on save button the form is saved to database instead of being errors.
please give me some easy solution so that i can check the error provider on save button if it has errors or it is clear.
thank you.
Posted
Updated 19-Sep-21 3:23am

There are a couple of techniques you could use...

You could re-validate all of the forms controls under the save button and have those validation functions return a bool that you can check, or iterate through each control on the form and see if the errorProvider has an error for it. For example...
C#
errorProvider1.Clear();

//Revalidate the controls
bool success = ValidateX1();
if (success) success = ValidateX2();
if (success) success = ValidateX3();
if (success)
{
    // do your save here
}

or
C#
// examine the contents of the errorProvider for each control
success = true;
foreach (Control c in this.Controls)
{
    if (errorProvider1.GetError(c).Length > 0)
        success = false;
}

if (success)
{
    // do your save here
}


[EDIT]
because the OP had his controls in a table, my suggested code of foreach (Control c in this.Controls) only found the table. The OP used another, nested foreach loop to look through the controls in the table once the outer loop had located it.
 
Share this answer
 
v3
Comments
Member 10358986 13-Nov-13 5:06am    
Thank you Sir
but i am unable to solve my problem my code is :
private void buttonSave_Click(object sender, EventArgs e)
{
// examine the contents of the errorProvider for each control
bool success = true;
foreach (Control c in this.Controls)
{
if (errorProvider1.GetError(c).Length > 0)
success = false;
}
if (success)
{
// MessageBox.Show("All fields filled");
try
{
string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\nic-15\My Documents\ALRS.mdb");
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
string query = "insert into arms_records (thana,licence_number,sanction_date,licencee_name,father_name,dob,religion,caste,address,village,uid,mobile,email,arms_category,type_of_arm,no_of_arms,arms_number,valid_area,renewal_date,remark,[image],[arms_image]) values (@thana, @lno, @sdate, @lname, @fname, @dob, @religion, @caste, @address, @village, @uid, @mobile, @email, @armscat, @toarm, @narms, @ano, @varea, @rdate, @remark, @image, @armsImage)";
OleDbCommand cmd = new OleDbCommand(query, con);
cmd.Parameters.AddWithValue("@thana", comboBoxthana.SelectedItem);
cmd.Parameters.AddWithValue("@lno", textBoxLicence_number.Text);
cmd.Parameters.AddWithValue("@sdate", dateTimePicker1.Text);
cmd.Parameters.AddWithValue("@lname", textBoxLicencee_Name.Text);
cmd.Parameters.AddWithValue("@fname", textBoxFatherName.Text);
cmd.Parameters.AddWithValue("@dob", dateTimePickerDob.Text);
cmd.Parameters.AddWithValue("@religion", comboBoxReligion.SelectedItem);
cmd.Parameters.AddWithValue("@caste", comboBoxCaste.SelectedItem);
cmd.Parameters.AddWithValue("@address", textBoxAdress.Text);
cmd.Parameters.AddWithValue("@village", textBoxvillage.Text);
cmd.Parameters.AddWithValue("@uid", textBoxUid.Text);
cmd.Parameters.AddWithValue("@mobile", textBox1Mobile.Text);
cmd.Parameters.AddWithValue("@email", textBoxemail.Text);
cmd.Parameters.AddWithValue("@armscat", comboBoxArmsCategory.SelectedItem);
cmd.Parameters.AddWithValue("@toarm", comboBoxtypeofarm.SelectedItem);
cmd.Parameters.AddWithValue("@narms", textBoxNoOfArm.Text);
cmd.Parameters.AddWithValue("@ano", textBoxArmsNo.Text);
cmd.Parameters.AddWithValue("@varea", comboBoxValidArea.SelectedItem);
cmd.Parameters.AddWithValue("@rdate", dateTimePickerRenewDate.Text);
cmd.Parameters.AddWithValue("@remark", textBoxRemark.Text);


// below 10 lines convert image into binary data
byte[] FileByte = null;
//string filename = textBoxImage.Text.Substring(textBoxImage.Text.LastIndexOf("\\") + 1, textBoxImage.Text.Length - (textBoxImage.Text.LastIndexOf("\\") + 1));
string path = textBoxImage.Text;
FileStream fs = new FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long allbytes = new FileInfo(path).Length;
FileByte = br.ReadBytes((Int32)allbytes);
fs.Close();
fs.Dispose();
br.Close();
//below code convert ArmsImage into binary data
byte[] FileByte2 = null;
string path2 = textBoxArmsImage.Text;
FileStream fs2 = new FileStream(path
CHill60 13-Nov-13 5:46am    
Have a look at the code where you are updating the errorProvider ... i.e. the validation code for your controls. It looks as if there are no errors on the provider.
Member 10358986 14-Nov-13 0:19am    
Thank you sir
I have solved the problem.My controls were in a table.so i used nested foreach and it is working.
CHill60 14-Nov-13 9:24am    
Ah! I'm glad you worked out what the problem was. For the benefit of anyone coming by here with a similar problem, I'm updating my solution.
1. Create your personal ErrorProvider
Friend Class myErrorProvider
Inherits ErrorProvider
Friend ErrorList As New Hashtable

Friend Sub AddError(c As Control, value As String)
Me.SetError(c, value)
ErrorList.Add(c, value)
End Sub

Friend Sub ClearList()
MyBase.Clear()
ErrorList.Clear()
End Sub
End Class

2. Compile your project then find it in your toolbox (from your solution components)

3. Usage
With MyErrorProvider1
If .ErrorList.Count > 0 Then
MsgBox("There are errors")
DirectCast(.ErrorList.Keys(0), Control).Select()
.ClearList()
End If
End With
 
Share this answer
 

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