Resolved sql fill not working

rwahdan2023

Member
Joined
Aug 28, 2023
Messages
10
Programming Experience
1-3
Hi,

I am trying to run this code but getting error.

code:
C#:
try
{
    SqlConnection conn =
    new SqlConnection("Data Source = .; " +
    "Initial Catalog = users; Persist Security Info = True; " +
    "User ID = sa; Password = ***********");

    SqlCommand cmd = new SqlCommand("select * from users");
    cmd.Connection = conn;
    cmd.Parameters.AddWithValue("@username", txtuser.Text);
    cmd.Parameters.AddWithValue("@password", txtpass.Text);

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

    if (dt.Rows.Count > 0)
    {
        MessageBox.Show("Success!");
    }
    else
    {
        MessageBox.Show("Not Success!");
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
the error:
I am always getting success even if the user or password is incorrect
 
Last edited:
I am always getting success even if the user or password is incorrect

Careful how you phrase your posts. At first I thought you were talking about the username and password in the sql server connection string

By the way, the world has moved on in terms of authing users. Having some table you maintain yourself and "select * from users" is decades old. Now we keep users in some system dedicated to auth and it is a well solved problem that third party and built in functionality has been doing for years. Look at Auth0, Active Directory, ASPNET identity management etc etc
 
Back
Top Bottom