nvnkmr
nvnkmr

Reputation: 11

Error on accessing Rows property of DataSet

I need to create a simple login page and getting an error on Rows...

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("server=AMR\\DEV1;UID=po;PWD=12W; database=POM;");
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from budget where id='" + TextBox1.Text + "' and amount='" + TextBox2.Text + "'", con);
    DataSet ds = new DataSet();
    ds.Clear();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);

    if (ds.Rows.Count > 0) {
        Session["user"] = TextBox1.Text;
        Response.Redirect("Default2.aspx");
    } else {
        TextBox1.Text = "";
        TextBox2.Text = "";
        Label1.Text = "Invalid Login Details!";
    }
}

Upvotes: 0

Views: 175

Answers (2)

syed mohsin
syed mohsin

Reputation: 2938

You have many problem in your code. I changed it where it is required

protected void Button1_Click(object sender, EventArgs e)
{
 SqlConnection con = new SqlConnection("server=AMR\\DEV1;UID=po;PWD=12W; database=POM;");
 con.Open();
 SqlCommand cmd = new SqlCommand("select * from budget where id=@id and amount=amount", con);
 cmd.Parameters.AddWithValue("@id",TextBox1.Text);
 cmd.Parameters.AddWithValue("@amount",TextBox2.Text);
 DataSet ds = new DataSet();
 ds.Clear();
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 da.Fill(ds);

 if (ds.Tables[0].Rows.Count > 0) 
 {
    Session["user"] = ds.Tables[0].Rows[0][0].ToString();
    Response.Redirect("Default2.aspx");
 }
  else {
    TextBox1.Text = "";
    TextBox2.Text = "";
    Label1.Text = "Invalid Login Details!";
 }
}

Learn DataSet , DataTable and Parameterized SqlComamnd as told by Andrey Gordeev.

Upvotes: 0

Andrey Gordeev
Andrey Gordeev

Reputation: 32559

DataSet doesn't have Rows property. DataTable does. Replace ds.Rows to ds.Tables[0].Rows in your code.

One more point: use Parameterized SqlComamnd. Your code is open to SQL Injection attacks

Upvotes: 1

Related Questions