views:

61

answers:

5

I have a username db table that I'm trying to connect with to compare the username/pass.

Here is my code, it's not working, what am I doing wrong?

DataTable dt = null;

protected void btn_Click_Login(object sender, EventArgs e)
{
    string query = string.Format("SELECT * FROM Users WHERE Username='{0}' AND Password='{1}'", txtUsername.Text, txtPassword.Text);

    using (SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["RBConnectionString"].ConnectionString))
    {
        c.Open();

        using (SqlDataAdapter a = new SqlDataAdapter(query, c))
        {
            DataTable t = new DataTable();
            a.Fill(t);
        }
    }
    if (dt.Rows.Count > 0)
    {
        Session["Username"] = txtUsername.Text;
        Session["Password"] = txtPassword.Text;
        Response.Redirect("main.aspx");
        lblError.Text = "success";
    }
    else
    {
        lblError.Text = "Wrong Username/Password combination";
    }
} 

}

+1  A: 

Try creating a SqlCommand to hold your query.

SqlCommand cmd = new SqlCommand(query, c);

using (SqlDataAdapter a = new SqlDataAdapter(cmd))
{
    DataTable t = new DataTable();
    a.Fill(t);
}

I'm not 100% sure that's your issue, but back in the days when i used to use ADO.NET (before L2SQL/EF, dark days indeed), i seem to remember an issue with DataTable's and SqlDataAdapter.

From what i remember - you can't fill a DataTable with a SqlDataAdapter based on a raw query string - you need to use SqlCommand. But i believe this can be accomplished with DataSet.

So either change to SqlCommand, or change to DataSet.

RPM1984
+1  A: 

most probably you are using wrong datatable to check no of rows returned.

Check for t and dt instances of datatable.

saurabh
+1  A: 

You fill t:

DataTable t = new DataTable();
a.Fill(t);

but read dt:

if (dt.Rows.Count > 0)
abatishchev
haha, didnt even notice that! you're probably right. let me guess, you were really good at those "where's wally" books?
RPM1984
I'm getting a null reference exception when trying to count the rows in the data table. The debugger says: Object reference not set to an instance of an object.
nick
@nick - did you make the change @abatishchev mentioned?
RPM1984
I did make the change, but the datatable kept coming up null. I decided to go with a datareader instead. Got it working that way.
nick
A: 

What error you are getting is not clear. But i feel your connection is open and is never closed. Try

c.Close();

Anuja Pawar
A: 

I decided to try the data reader and got it working:

protected void btn_Click_Login(object sender, EventArgs e)
{

   SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RbConnectionString"].ConnectionString);
    conn.Open();
    string queryString = "SELECT * FROM [Users] WHERE Username=@username AND Password= @password";
   SqlCommand command = new SqlCommand(queryString, conn);
   command.Parameters.AddWithValue("@username", txtUsername.Text);
   command.Parameters.AddWithValue("@password", txtPassword.Text);

   SqlDataReader reader = null;
   reader = command.ExecuteReader();

   if (reader.Read())
   {
       Session["Username"] = txtUsername.Text;
       Session["Password"] = txtPassword.Text;
       Response.Redirect("main.aspx");
   }
   else
   {
       lblError.Visible = true;
       lblError.Text = "Incorrect Username/Password Combination";
   }
    conn.Close();

}
nick
Is that the correct way to protect against sql injection?
nick
Yes, that's the proper way to avoid SQL injection.
Dillie-O