Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi All,
 
How to use validations for windows application with c#. I have kept "error provider" for two text box validation. It is showing error, But at the same time record also saving in to database.Once all the fields is field then only record will be save in to database. And how to add required validation for combo box?
 
Thanks and regards,
Murali.
 
[Updated Code]
 
Submit Button click -------
 private void btnFamilysaveandnext_Click(object sender, EventArgs e)
        {            
                txtAddress_Validated(this, e);
                txtcity_Validated(this, e);
                txtPincode_Validated(this, e);
 

                SqlConnection con = new SqlConnection(strConn);
                con.Open();
                SqlCommand cmd = new SqlCommand("sp_rm_insert_basicfamily", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@FamilyID", txtFamilyid.Text);
                cmd.Parameters.AddWithValue("@HHID", txtHeadofhousehold.Text);
                cmd.Parameters.AddWithValue("@Religion", cbxReligion.SelectedValue);
                cmd.Parameters.AddWithValue("@Caste", cbxCaste.SelectedValue);
                cmd.Parameters.AddWithValue("@HouseNo", txtHouseno.Text);
                cmd.Parameters.AddWithValue("@Address1", txtAddress.Text);
                cmd.Parameters.AddWithValue("@Village", txtcity.Text);
                cmd.Parameters.AddWithValue("@State", cbxState.SelectedValue);
                cmd.Parameters.AddWithValue("@District", cbxDistrict.SelectedValue);
                cmd.Parameters.AddWithValue("@Mandal", cbxMandal.SelectedValue);
                cmd.Parameters.AddWithValue("@Pincode", txtPincode.Text);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Successfully Saved");
}
 
private void txtAddress_Validated(object sender, EventArgs e)
        {
            bool bTest = txtAddressValidate();
            if (bTest == true)
            {
                this.errorProvider1.SetError(txtAddress, "This field must contain text");
            }
            else
            {
                this.errorProvider1.SetError(txtAddress, "");
            }
        }
        private bool txtAddressValidate()
        {
            if (txtAddress.Text == string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        private void txtcity_Validated(object sender, EventArgs e)
        {
            bool bTest = txtcityStringIsValid();
            if (bTest == true)
            {
                this.errorProvider1.SetError(txtcity, "Enter your City");
            }
            else
            {
                this.errorProvider1.SetError(txtcity, "");
            }
        }
 
        private bool txtcityStringIsValid()
        {
            if (txtcity.Text == string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        private void txtPincode_Validated(object sender, EventArgs e)
        {
            bool bTest = txtPincodeStringIsValid();
            if (bTest == true)
            {
                this.errorProvider1.SetError(txtPincode, "Please enter 6 Digits");
            }
            else
            {
                this.errorProvider1.SetError(txtPincode, "");
            }
        }
 
        private bool txtPincodeStringIsValid()
        {
            char[] testArr = txtPincode.Text.ToCharArray();
            bool testBool = false;
            if (testArr.Length < 6)
            {
                testBool = true;
            }
            return testBool;
        }
 
I have use the "validated" event for that.
Posted 9-Dec-12 18:34pm
Edited 9-Dec-12 19:55pm
v3
Comments
Krunal Rohit at 9-Dec-12 23:35pm
   
provide your code.. it does need some change..
choudhary.sumit at 10-Dec-12 0:49am
   
what if i suggest you to use http://www.codeproject.com/Articles/10093/Validators-for-Windows-Forms-ValidationProvider-Co
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I tried like this , it's working fine.
 

if (txtAddress.Text != "")
{
if (txtPincode.Text != "")
{
 
if (txtcity.Text != "")
{
 
SqlConnection con = new SqlConnection(strConn);
con.Open();
SqlCommand cmd = new SqlCommand("sp_rm_insert_basicfamily", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FamilyID", txtFamilyid.Text);
cmd.Parameters.AddWithValue("@HHID", txtHeadofhousehold.Text);
cmd.Parameters.AddWithValue("@Religion", cbxReligion.SelectedValue);
cmd.Parameters.AddWithValue("@Caste", cbxCaste.SelectedValue);
cmd.Parameters.AddWithValue("@HouseNo", txtHouseno.Text);
cmd.Parameters.AddWithValue("@Address1", txtAddress.Text);
cmd.Parameters.AddWithValue("@Village", txtcity.Text);
cmd.Parameters.AddWithValue("@State", cbxState.SelectedValue);
cmd.Parameters.AddWithValue("@District", cbxDistrict.SelectedValue);
cmd.Parameters.AddWithValue("@Mandal", cbxMandal.SelectedValue);
cmd.Parameters.AddWithValue("@Pincode", txtPincode.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Successfully Saved");
}
 
else
{
MessageBox.Show("Please enter city");
}
}
 
else
{
MessageBox.Show("Please enter pin code");
}
}
 
else
 
{
 
MessageBox.Show("Please enter address");
 
}
 

Thanks to all..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Instead of handling it through script you can handle it by using validator (Required feild validator). In property window you need to fill the properties.
1. error message
2. Text
3.Tooltip
4.Validation group.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 215
1 George Jonsson 175
2 Kornfeld Eliyahu Peter 159
3 PIEBALDconsult 110
4 Richard MacCutchan 85
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100