validations for windows forms


first desigh windows form as follows

write the following code in Register buttonclick event

 

  1. private void button1_Click(object sender, EventArgs e)
  2.     {
  3.         if (isvalid())
  4.         {
  5.             errorProvider1.Clear();
  6.             SqlConnection con = new SqlConnection(strcon);
  7.             SqlCommand cmd = new SqlCommand("sp_registration", con);
  8.             cmd.CommandType = CommandType.StoredProcedure;
  9.             cmd.Parameters.Add("@username", textBox1.Text);
  10.             cmd.Parameters.Add("@password", textBox2.Text);
  11.             cmd.Parameters.Add("@firstname", textBox3.Text);
  12.             cmd.Parameters.Add("@lastname", textBox4.Text);
  13.             cmd.Parameters.Add("@email", textBox5.Text);
  14.             cmd.Parameters.Add("@mobile", textBox6.Text);
  15.             con.Open();
  16.             int i = cmd.ExecuteNonQuery();
  17.             if (i > 0)
  18.             {
  19.                 label7.Text = "successfully Registered…";
  20.             }
  21.             else
  22.                 label7.Text = "Failed to register or details already exist…";
  23.             con.Close();
  24.         }
  25.     }
  26.     private bool isvalid()
  27.     {
  28.         bool error = true;
  29.         if (textBox1.Text.Trim() == string.Empty)
  30.         {
  31.             errorProvider1.SetError(textBox1, "Enter UserName");
  32.             error = false;
  33.         }
  34.         else if (textBox2.Text.Trim() == string.Empty)
  35.         {
  36.             errorProvider1.Clear();
  37.             errorProvider1.SetError(textBox2, "Enter Password");
  38.             error = false;
  39.         }
  40.         else if (textBox3.Text.Trim() == string.Empty)
  41.         {
  42.             errorProvider1.Clear();
  43.             errorProvider1.SetError(textBox3, "Enter FirstName");
  44.             error = false;
  45.         }
  46.         else if (textBox4.Text.Trim() == string.Empty)
  47.         {
  48.             errorProvider1.Clear();
  49.             errorProvider1.SetError(textBox4, "Enter LastName");
  50.             error = false;
  51.         }
  52.         else if (isvalidemail())
  53.         {
  54.             errorProvider1.Clear();
  55.             errorProvider1.SetError(textBox5, "Enter Valid Email");
  56.             error = false;
  57.         }
  58.         else if (isvalidmobile())
  59.         {
  60.             errorProvider1.Clear();
  61.             errorProvider1.SetError(textBox6, "Enter Valid Mobile Number");
  62.             error = false;
  63.         }
  64.         return error;
  65.  
  66.     }
  67.     private bool isvalidemail()
  68.     {
  69.         bool emailr = true;
  70.         if (textBox5.Text.Trim() != string.Empty)
  71.         {
  72.             if (textBox5.Text.Trim().Contains("@") && textBox5.Text.Trim().Contains("."))
  73.                 emailr = false;
  74.         }
  75.         return emailr;
  76.     }
  77.     private bool isvalidmobile()
  78.     {
  79.         bool mobile = false;
  80.         if (textBox6.Text.Trim() == string.Empty)
  81.             mobile = true;
  82.         else if (textBox6.Text.Trim().Length < 10)
  83.             mobile = true;
  84.         else if (textBox6.Text.Trim().Length > 10)
  85.             mobile = true;
  86.         else
  87.         {
  88.             try
  89.             {
  90.                 int.Parse(textBox6.Text);
  91.                 mobile = true;
  92.             }
  93.             catch
  94.             {
  95.                 mobile = false;
  96.                 return mobile;
  97.             }
  98.         }
  99.         return mobile;
  100.     }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s