protected void btnLogin_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=MCDU11;Initial Catalog=VisitorManagement;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT * FROM SecurityUser WHERE Username = '" + txtUsername.Text.Trim() + "' AND "
+ "Password='" + Encrypt(txtPassword.Text.Trim()) + "'" , conn);
SqlCommand cmd1 = new SqlCommand("update SecurityUser set LoginOn ='" + DateTime.Now + "' , " + "WHERE Username ='" + txtUsername.Text.Trim() + "'", conn);
conn.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["Username"] = txtUsername.Text;
Session["Id"] = dr["Id"].ToString();
cmd1.ExecuteNonQuery();
Response.Redirect("SecurityHome.aspx");
}
else
{
lblError.Text = "Either username and/or password is wrong. Please try again!";
}
dr.Close();
conn.Close();
}
這是我的錯誤ASP.NET無法選擇和更新
「‘System.InvalidOperationException’類型的異常出現在 System.Data.dll中,但在用戶代碼中沒有處理
附加信息:已經有一個開放的DataReader關聯 這個Command必須先關閉。「
請使用參數化查詢添加MultipleActiveResultSets =真! ! https://msdn.microsoft.com/en-us/library/bb738521(v=vs.100).aspx – Aviatrix