Click here to Skip to main content
12,821,084 members (43,368 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 18-Nov-12 5:51am
sariqkhan1.4K
Updated 18-Nov-12 6: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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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

Hope this helps.
  Permalink  
v5
Comments
sariqkhan 18-Nov-12 11:26am
   
empty method will return that??? true or false???
ProgramFOX 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
ProgramFOX 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 != "");
ProgramFOX 18-Nov-12 12:00pm
   
(textBox1.Text != "") || (textBox2.Text != "") is a bool.
The first part is (textBox1.Text != ""), then there's a OR operator, and then the second part of the bool: (textBox2.Text != "")
sariqkhan 18-Nov-12 12:04pm
   
means first part or the second part will not equal to string.empty
then bool is true??
ProgramFOX 18-Nov-12 12:06pm
   
I was wrong.
I updated my answer.
Now, IsEmpty() returns true of textBox1.Text is empty or textBox2.Text is empty.
sariqkhan 18-Nov-12 12:33pm
   
bool vcom2 = IsEmpty();
if (!vcom && !vcom2)
{ //code
}
now it is working with this
sariqkhan 18-Nov-12 23:42pm
   
+5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Two things:
1) You need to specifically check for a blank password.
Before you open the connection add the lines:
bool bFailed = true;
if (!string.IsNullOrWhitespace(textBox1.Text))
   {
After your ExecuteNonQuery, change your code to:
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.[^]
  Permalink  
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:00pm
   
okay and thank you sir
:)
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
OriginalGriff 19-Nov-12 13:58pm
   
You're welcome!

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.170308.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2017
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