Click here to Skip to main content
15,894,195 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i have written simple logic code for my winform
when i click on submit without filling the form the error message that is seen is updation failed.i want error message when i click on submit without filling the form, but my code also considers blank as passwordword has enteredd
here is my code
private void button1_Click(object sender, EventArgs e)
        {
            bool vcom = doublepasword();
            if (!vcom)
            {
                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");
            }
        }
        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;
                label13.Visible = true;
                return true;
            }
            else
            {
                pictureBox1.Visible = true;
                label13.Visible = false;
                errorProvider1.SetError(textBox2, string.Empty);
                return false;
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            validation2();
        }


[Edit]Changing the "C" tag into "C#"[/Edit]
Posted
Updated 18-Nov-12 5:16am
v3
Comments
Sergey Alexandrovich Kryukov 18-Nov-12 11:19am    
Please ask a question.
--SA
sariqkhan 18-Nov-12 11:25am    
i want error message
my code assumes blank text as password entered so it does not shows error message
because i have only used textbox1.text != textbox2.text
it also checks for blanktext. what to do? to display error message when i click on submit button without filling both the textbox field

Create a IsEmpty method:
C#
private bool IsEmpty()
{
      return (textBox1.Text == "") || (textBox2.Text == "");
}

And change the bool vcom = doublepasword(); line in the button1_Click method into this:
C#
bool vcom = !doublepasword() && !IsEmpty();

Hope this helps.
 
Share this answer
 
v5
Comments
sariqkhan 18-Nov-12 11:26am    
empty method will return that??? true or false???
Thomas Daniels 18-Nov-12 11:30am    
If the textBox1.Text string is empty OR the textBox2.Text string is empty, then IsEmpty() returns false.
sariqkhan 18-Nov-12 11:54am    
bool vcom2 = IsEmpty();
if (!vcom && vcom2)
{
//code
}
i have done this. then it is working sir
Thomas Daniels 18-Nov-12 11:56am    
Yes, you're right.
I'll update my answer.
sariqkhan 18-Nov-12 11:58am    
and what is this means
return (textBox1.Text != "") || (textBox2.Text != "");
Two things:
1) You need to specifically check for a blank password.
Before you open the connection add the lines:
C#
bool bFailed = true;
if (!string.IsNullOrWhitespace(textBox1.Text))
   {
After your ExecuteNonQuery, change your code to:
C#
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();
    bFailed = false;
}
if (bFailed)
{
    MessageBox.Show("updated failed");
}


That will solve the problem you are describing.

2) Don't do it like that! Never, ever store passwords in clear text, it is a major security risk! See here: Password Storage: How to do it.[^]
 
Share this answer
 
Comments
sariqkhan 18-Nov-12 11:45am    
bro it was awsome.. but you have done for one textbox. if i click on submit without filling both the form there is no error message!
OriginalGriff 18-Nov-12 11:54am    
:laugh: - so extend it! You have the code as a template, so it should be pretty easy for you.
sariqkhan 18-Nov-12 12:24pm    
sir can you help me out. can you tell me what does this means ad what will this will return?
private bool IsEmpty()//return false
{
return (textBox1.Text == "") || (textBox2.Text == "");
}
OriginalGriff 19-Nov-12 3:12am    
It exits the current method, and returns a bool value, which is true is either or both of the two textboxes contain an empty string, and false otherwise. (It is a much better idea to use the string.IsNullOrWhitespace method, as this copes with strings that are just spaces as well).
sariqkhan 19-Nov-12 12:40pm    
i got it.
thanks for explanation bro

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