简体   繁体   中英

Login doesn't work

so, I'm trying to make a login form but I'm facing an error.. It doesn't continue, it shows only no data (the error message I've put if the username or password is incorrect, even tho I;ve set them correct)

this is my code

SqlConnection conn = new SqlConnection("Data Source=BRZAP\\MSSQLSERVERBTK; Initial Catalog=BrzAP; Integrated Security=True;");
try
{
    conn.Close();
    conn.Open();
    SqlCommand cmd = new SqlCommand(@"SELECT id,d_username,d_password FROM t_Login 
                            WHERE d_username=@usr AND 
                            d_password=@pw", conn);
    cmd.Parameters.AddWithValue("@usr", txtUID.ToString());
    cmd.Parameters.AddWithValue("@pw", txtP.ToString());
    SqlDataReader reader = cmd.ExecuteReader();
    if (reader.Read())
    {
       MessageBox.Show(reader["d_username"].ToString());
    }
    else
    {
        MessageBox.Show("no data");
    } 
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message.ToString());
    conn.Close();
}
finally
{
    conn.Close();
}

bellow is a pic of the data I have in sql. pic with data

Use txtUID.Text and txtP.Text instead of txtUID.ToString() and txtP.ToString() :

cmd.Parameters.AddWithValue("@usr", txtUID.Text);
cmd.Parameters.AddWithValue("@pw", txtP.Text);

The Text property gives you the actual value in the text box. But when you call .ToString() on a TextBox control, you will get something like:

System.Windows.Controls.Textbox

This means that the parameter will always be incorrect.

See the documentation on MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM