2013-02-20 62 views
0

的行屬性我需要創建一個簡單的登錄頁面,並得到一個錯誤的行...錯誤上訪問數據集

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!"; 
    } 
} 
+0

請問你能格式化你的代碼嗎?它不能被理解,併發布編譯器給了什麼錯誤能夠更好地幫助你.. – igarcia 2013-02-20 12:12:53

+0

你得到什麼錯誤?在哪一行?你有什麼嘗試? – 2013-02-20 12:14:30

回答

0

您的代碼中存在很多問題。我改變了它在那裏需要

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 [email protected] 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!"; 
} 
} 

瞭解DataSetDataTableParameterized SqlComamnd由安德烈Gordeev所講述。