8 Replies - 649 Views - Last Post: 26 March 2010 - 06:49 AM Rate Topic: -----

#1 jokertin92  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 26-March 09

Problem with login form

Posted 25 March 2010 - 04:47 AM

Hi, I've created a login form where I've some problem,.. Can any one help me please...

My username password is (Location = Hyderabad, Password=Hydd)


private void btnlogin_Click(object sender, EventArgs e)
        {
            using ( con = new SqlConnection(Helper.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "Select Location, Password FROM UserAccounts WHERE Location=" + "@location AND Password = @password";
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@location",cmbLocation.Text.ToString());
                cmd.Parameters.AddWithValue("@password",txtPassword.Text.ToString());
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {

====> This statement is not executed...
---------------------------------------------------------------------------
                    if(cmbLocation.Text==null||txtPassword.Text==null)
---------------------------------------------------------------------------

                    {
                        MessageBox.Show("Location / Password must not be Empty");
                    }

===> This statement is created for checking case sensitive, it shows error if the password is "hydd", but other than this if i give any thing like "hyd" it doesn't show any error like password wrong etc.
---------------------------------------------------------------------------
                    else if (dr["Location"].ToString() == cmbLocation.Text && dr["Password"].ToString()==txtPassword.Text)
---------------------------------------------------------------------------
                    {
                        MessageBox.Show("Logged in Successfully");
                        //this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("!!! Please Check the Password");
                        txtPassword.Clear();
                        cmbLocation.Focus();
                    }
                }                                          
            } //con.Dispose() method would be called automatically
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with login form

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Problem with login form

Posted 25 March 2010 - 05:40 AM

you aren't doing your login form very efficiently.

try this instead...

private void btnlogin_Click(object sender, EventArgs e)
{
    // checks the combobox and textbox.  This should
    //    be done before you even do the database query.
    if (string.IsNullOrEmpty(cmbLocation.Text) || string.IsNullOrEmpty(txtPassword.Text))
    {
         MessageBox.Show("Location / Password must not be Empty");
         return;
    }

    using (SqlConnection con = new SqlConnection(Helper.ConnectionString))
    {
        using (SqlCommand cmd = con.CreateCommand())
        {
             // The query will return the number of record that match the location 
             //    and password.  If it's 0, then the password was incorrect.
             cmd.CommandText = "Select COUNT(*) FROM UserAccounts WHERE Location = @location AND Password = @password";
 
             // No need to use .ToString() since the Text properties are
             //    already strings.
             cmd.Parameters.AddWithValue("@location",cmbLocation.Text);
             cmd.Parameters.AddWithValue("@password",txtPassword.Text);

             con.Open();

             // ExecuteScalar returns only the first column of the first row
             //    from the query.  We are only returning one column and one row
             //    so ExecuteScalar fits our needs.
             int count = (int)cmd.ExecuteScalar();

             if (count > 0)
                MessageBox.Show("Logged in Successfully");
             else
               MessageBox.Show("!!! Please Check the Password");
        }
    }
}


This post has been edited by eclipsed4utoo: 25 March 2010 - 05:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 jokertin92  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 26-March 09

Re: Problem with login form

Posted 25 March 2010 - 06:14 AM

View Posteclipsed4utoo, on 25 March 2010 - 04:40 AM, said:

you aren't doing your login form very efficiently.

try this instead...

private void btnlogin_Click(object sender, EventArgs e)
{
    // checks the combobox and textbox.  This should
    //    be done before you even do the database query.
    if (string.IsNullOrEmpty(cmbLocation.Text) || string.IsNullOrEmpty(txtPassword.Text))
    {
         MessageBox.Show("Location / Password must not be Empty");
         return;
    }

    using (SqlConnection con = new SqlConnection(Helper.ConnectionString))
    {
        using (SqlCommand cmd = con.CreateCommand())
        {
             // The query will return the number of record that match the location 
             //    and password.  If it's 0, then the password was incorrect.
             cmd.CommandText = "Select COUNT(*) FROM UserAccounts WHERE Location = @location AND Password = @password";
 
             // No need to use .ToString() since the Text properties are
             //    already strings.
             cmd.Parameters.AddWithValue("@location",cmbLocation.Text);
             cmd.Parameters.AddWithValue("@password",txtPassword.Text);

             con.Open();

             // ExecuteScalar returns only the first column of the first row
             //    from the query.  We are only returning one column and one row
             //    so ExecuteScalar fits our needs.
             int count = (int)cmd.ExecuteScalar();

             if (count > 0)
                MessageBox.Show("Logged in Successfully");
             else
               MessageBox.Show("!!! Please Check the Password");
        }
    }
}



but, it doesn't check the case sensitive...
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Problem with login form

Posted 25 March 2010 - 06:36 AM

this should work for you

private void btnlogin_Click(object sender, EventArgs e)
{
    // checks the combobox and textbox.  This should
    //    be done before you even do the database query.
    if (string.IsNullOrEmpty(cmbLocation.Text) || string.IsNullOrEmpty(txtPassword.Text))
    {
         MessageBox.Show("Location / Password must not be Empty");
         return;
    }

    using (SqlConnection con = new SqlConnection(Helper.ConnectionString))
    {
        using (SqlCommand cmd = con.CreateCommand())
        {
             // The query will return the password of the location chosen
             cmd.CommandText = "Select Password FROM UserAccounts WHERE Location = @location";
 
             // No need to use .ToString() since the Text properties are
             //    already strings.
             cmd.Parameters.AddWithValue("@location",cmbLocation.Text);

             con.Open();

             // ExecuteScalar returns only the first column of the first row
             //    from the query.  We are only returning one column and one row
             //    so ExecuteScalar fits our needs.
             string dbPassword = Convert.ToString(cmd.ExecuteScalar());

             if (dbPassword == txtPassword.Text)
                MessageBox.Show("Logged in Successfully");
             else
                MessageBox.Show("!!! Please Check the Password");
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#5 jokertin92  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 26-March 09

Re: Problem with login form

Posted 25 March 2010 - 06:48 AM

View Posteclipsed4utoo, on 25 March 2010 - 05:36 AM, said:

this should work for you

private void btnlogin_Click(object sender, EventArgs e)
{
    // checks the combobox and textbox.  This should
    //    be done before you even do the database query.
    if (string.IsNullOrEmpty(cmbLocation.Text) || string.IsNullOrEmpty(txtPassword.Text))
    {
         MessageBox.Show("Location / Password must not be Empty");
         return;
    }

    using (SqlConnection con = new SqlConnection(Helper.ConnectionString))
    {
        using (SqlCommand cmd = con.CreateCommand())
        {
             // The query will return the password of the location chosen
             cmd.CommandText = "Select Password FROM UserAccounts WHERE Location = @location";
 
             // No need to use .ToString() since the Text properties are
             //    already strings.
             cmd.Parameters.AddWithValue("@location",cmbLocation.Text);

             con.Open();

             // ExecuteScalar returns only the first column of the first row
             //    from the query.  We are only returning one column and one row
             //    so ExecuteScalar fits our needs.
             string dbPassword = Convert.ToString(cmd.ExecuteScalar());

             if (dbPassword == txtPassword.Text)
                MessageBox.Show("Logged in Successfully");
             else
                MessageBox.Show("!!! Please Check the Password");
        }
    }
}


That's fine, but I don't get answer for doubt..

Anybody clear why does the if state is not working in Application?..
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Problem with login form

Posted 25 March 2010 - 07:14 AM

can you explain what your problem is? Are you getting an error message? If so, what error? If not, what is the application doing vs. what you expect it to do. Have you tried breakpointing and debugging to see what the values of the variables are?
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Guest*


Reputation:

Re: Problem with login form

Posted 25 March 2010 - 09:48 PM

View Posteclipsed4utoo, on 25 March 2010 - 06:14 AM, said:

can you explain what your problem is? Are you getting an error message? If so, what error? If not, what is the application doing vs. what you expect it to do. Have you tried breakpointing and debugging to see what the values of the variables are?


Ya, I tied to debugg, if i give the password other than (Hydd or hydd), it simply jumps that if statement.
When i give my username (location - Hyderabad) and password as hyd, it doesn't show any error message, and the application is alive, even i keep on press the login butto.
My actual insight is to check case sensitive.
Was This Post Helpful? 0

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Problem with login form

Posted 26 March 2010 - 05:10 AM

are you still using your original code, or my code?
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Guest*


Reputation:

Re: Problem with login form

Posted 26 March 2010 - 06:49 AM

View Posteclipsed4utoo, on 26 March 2010 - 04:10 AM, said:

are you still using your original code, or my code?

It's cleared...
Was This Post Helpful? 0

Page 1 of 1