Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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

Hi Sariq,

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

C#
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:

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


Hope this helps!

Happy Coding :)
Sunny K
 
Share this answer
 
v2
Comments
sariqkhan 18-Nov-12 23:48pm    
that was a silly mistake
:laugh
thanks sir
+5
Sunny_Kumar_ 19-Nov-12 0:54am    
you are most welcome, always :)
shaikh-adil 19-Nov-12 1:14am    
nice shot.
Sunny_Kumar_ 19-Nov-12 1:20am    
thanks shaikh-adil :)
shaikh-adil 19-Nov-12 1:30am    
you are welcome
:)
C#
{
  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 !=  "");
 
Share this answer
 
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.In 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.In 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
:)

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