Click here to Skip to main content
12,405,853 members (72,958 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
in this code. when user clicks on the submit button without filling both the text fields error message is delivered. but i used a picturebox when the password matches then a pitcturebox is shown to user which contains a tick sign. but the problem is when user clicks the submit button without filling both the fields the picturebox is visible. so i have written method
newval();
which checks for blank textfields of both the text fields and set the piturebox to false. but when i implement the code m form hangs up!1
private void button1_Click(object sender, EventArgs e)
       {
           bool vcom = doublepasword();
           bool vnames = ValidateNames();
           bool vnames2 = ValidateNames2();
           if (!vcom && !vnames&& !vnames2)
           {
               SqlConnection conn = new SqlConnection();
               conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\project\sample project\prject xample2 perfect\login\Database1.mdf;Integrated Security=True;User Instance=True";
               try
               {
 
                       conn.Open();
                       string qry2 = "UPDATE Table1 SET Password =@Password WHERE username=@username";
                       SqlCommand com = new SqlCommand(qry2, conn);
                       com.Parameters.AddWithValue("@username", this.label1.Text);
                       com.Parameters.AddWithValue("@Password", this.textBox1.Text);
                       int result = com.ExecuteNonQuery();
                       if (result > 0)
                       {
                           MessageBox.Show("updated sucesfull \n" + "your new password is: " + textBox1.Text + " thanks for changing your password", "success");
                           this.Hide();
                           login ls = new login();
                           ls.Show();
 
                       }
                       else
                       {
                           MessageBox.Show("updated failed");
 
                       }
 
                   }
               catch (Exception)
               {
                   MessageBox.Show("Error with the databse connection");
               }
           }
           else
           {
               MessageBox.Show("error");
               validation2();
              neval(); //when i comment this my form runs smoothly but when i uncomment this my form hangs up?? whats the problem in methods??
           }
       }
       public bool ValidateNames()
       {
           if (textBox1.Text == string.Empty)
           {
               return true;
           }
           else
           {
               return false;
           }
       }
 
       public bool ValidateNames2()
       {
           if (textBox2.Text == string.Empty)
           {
               return true;
           }
           else
           {
               return false;
           }
       }
       public void neval()// is there any problem in this method?
       {
           while (textBox2.Text == "")
           {
               if (textBox1.Text ==  "")
               {
                   pictureBox1.Visible = false;
               }
           }
       }
       private bool doublepasword()
       {
           if (textBox1.Text != textBox2.Text)
           {
               return true;
           }
           else
           {
               return false;
           }
       }
       public bool validation2()
       {
           bool stat1 = doublepasword();
           if (stat1 == true)
           {
               errorProvider1.SetError(textBox2, "password doesnt match");
               pictureBox1.Visible = false;
 
               return true;
           }
           else
           {
               pictureBox1.Visible = true;
 
               errorProvider1.SetError(textBox2, string.Empty);
               return false;
           }
       }
 
       private void textBox2_TextChanged(object sender, EventArgs e)
       {
           validation2();
       }
Posted 18-Nov-12 17:18pm
sariqkhan1.4K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi Sariq,

The problem lies within your code inside the method you specified: "neval()":

public void neval()// is there any problem in this method?
        {
            while (textBox2.Text == "")
            {
                if (textBox1.Text ==  "")
                {
                    pictureBox1.Visible = false;
                }
            }
        }

You've used a while which is causing the form hangup. While takes the condition in a loop and keep evaluating until the condition results in a false. In your case if you leave the Textbox 2 empty it always results true causing the form to hangup.

Just replace "while" with "If" as below and it should work fine:

public void neval()// is there any problem in this method?
        {
            If (textBox2.Text == "")
            {
                if (textBox1.Text ==  "")
                {
                    pictureBox1.Visible = false;
                }
            }
        }

Hope this helps!

Happy Coding Smile | :)
Sunny K
  Permalink  
v2
Comments
sariqkhan 18-Nov-12 23:48pm
   
that was a silly mistake
:laugh
thanks sir
+5
Sunnykumar08 19-Nov-12 0:54am
   
you are most welcome, always :)
shaikh-adil 19-Nov-12 1:14am
   
nice shot.
Sunnykumar08 19-Nov-12 1:20am
   
thanks shaikh-adil :)
shaikh-adil 19-Nov-12 1:30am
   
you are welcome
:)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

{
  while (textBox2.Text == "")
  {
       if (textBox1.Text ==  "")
       {
           pictureBox1.Visible = false;
       }
  }
}


Why you use while (textBox2.Text == "") It is falling in continuous loop so your form hangs.

Just try it:
pictureBox1.Visible = (textBox1.Text !=  "");
  Permalink  
Comments
sariqkhan 19-Nov-12 0:01am
   
what does this means?
pictureBox1.Visible = (textBox1.Text != "");
picturebox will visible to true if textbox.text will not be empty string??
right sir?
Subrata_Kolkata 19-Nov-12 0:19am
   
You are right.
It will be better if you do this:
pictureBox1.Visible = (textBox1.Text.Trim() != "");//For avoiding space
sariqkhan 19-Nov-12 1:08am
   
textbox.text.trim???
What it will do?
Subrata_Kolkata 19-Nov-12 1:20am
   
It will avoid space from both end of string. Else your picturebox may visible if user provide space.
sariqkhan 19-Nov-12 1:48am
   
oh. Thanks sir.
It was awsome shortest code
:)
shaikh-adil 19-Nov-12 1:13am
   
good answer
:)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2016
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