2 Replies - 301 Views - Last Post: 23 July 2013 - 06:12 AM Rate Topic: -----

#1 AirlynKhey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-July 13

failed to login

Posted 22 July 2013 - 08:31 PM

so far i got this code but I cannot load the main form even though I've inputted the right username and password.Its always says that invalid username and password.
private void btn_log_Click(object sender, EventArgs e)
        {

SqlCommand cmd;
            DataSet ds = new DataSet();

            try{
                if (txtuser.Text=="" || txtpass.Text=="")
                {
                    MessageBox.Show("Enter Username and password");
                    return;
                }
                cmd =new SqlCommand("select * from login where user='"+txtuser.Text+"'and pass='"+txtpass.Text+"'",con);
                SqlDataAdapter da=new SqlDataAdapter (cmd);
                da.Fill (ds);
                int i = ds.Tables[0].Rows.Count;
                if(i !=0)
                {
                  
                    this.Hide();
                    mainform objmain=new mainform();
                    objmain.Show();
                    ds.Clear();
                    

                    }
                    else
                    {
                    MessageBox.Show("Not Registered User or Invalid Name/Password");
                    txtpass.Text ="";
    }
}
catch(Exception ex)
{
                MessageBox.Show(ex.Message);
}

}


*** EDIT ***
Please use code tags when posting code
:code:

This post has been edited by GunnerInc: 22 July 2013 - 08:51 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: failed to login

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: failed to login

Posted 23 July 2013 - 12:09 AM

Might not be your problem, but there is no space between the last ' for your user name and the SQL keyword and.

And why go through the complicated method you use, just use SqlCommand.ExecuteScalar and count the number of returned rows.
And use Parameters
Was This Post Helpful? 2
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: failed to login

Posted 23 July 2013 - 06:12 AM

I've made it a point now to not help fix code that uses raw string building for SQL queries until they fix their code to use parameters. Here's a tutorial:

http://www.dreaminco...ery-a-database/

Once you get that fixed, then we can talk about any errors that come up.

Then we can talk about you storing passwords in plain text in a database and how bad of an idea that is.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1