2016-07-22 62 views
0
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必須先關閉。「

+0

請使用參數化查詢添加MultipleActiveResultSets =真! ! https://msdn.microsoft.com/en-us/library/bb738521(v=vs.100).aspx – Aviatrix

回答

0

嘗試在你的連接 作爲

SqlConnection conn = 
new SqlConnection("Data Source=MCDU11; 
Initial Catalog=VisitorManagement;Integrated Security=True;MultipleActiveResultSets=True"); 

SqlCommand cmd1 = 
new SqlCommand("update SecurityUser set LoginOn ='" + DateTime.Now + "' 
WHERE Username ='" + txtUsername.Text.Trim() + "'",conn); 
+0

嗨感謝您的幫助,他們告訴我這個錯誤,「類型'System.Data.SqlClient異常。 SqlException'發生在System.Data.dll中,但未在用戶代碼中處理 附加信息:關鍵字'WHERE'附近的語法錯誤。「 –

+0

它是不同的錯誤相關的你在cmd1 sql語句,看到更新結果 – nazark

+0

非常感謝你!這行得通 –

0

「已經有一個用此命令 必須先關閉相關聯的打開的DataReader。」

該異常消息非常清晰,您不能爲連接(另一個DataReader,NonQuery ...)執行任何命令,直到原始DataReader關閉。 在你的情況下,代碼拋出異常是

cmd1.ExecuteNonQuery(); 

你應該提取你的代碼2個motheds,第一個將得到DB和第二個數據,如果步驟1成功將更新。

出於演示:

private static string ConnectionString = "Data Source=MCDU11;Initial Catalog=VisitorManagement;Integrated Security=True"; 

protected string GetLoggedUserId(string username, string password) { 
    var id = string.Empty; 
    using(var conn = new SqlConnection(ConnectionString) { 
     var cmd = new SqlCommand("SELECT * FROM SecurityUser WHERE Username = '" + username + "' AND " 
             + "Password='" + password) + "'" , conn); 
     conn.Open(); 
     using(var reader = cmd.ExecuteReader()){ 
      if(reader.Read()){ 
       id = reader["Id"].ToString(); 
      } 
     } 
    } 
    return id; 
} 

protected string UpdateLoggedUser(string username) { 
    using(var conn = new SqlConnection(ConnectionString) { 
     var cmd = new SqlCommand("update SecurityUser set LoginOn ='" + DateTime.Now + "' , " + "WHERE Username ='" + username + "'", conn); 
     conn.Open(); 
     cmd.ExecuteNonQuery(); 
    } 
} 

protected void btnLogin_Click(object sender, EventArgs e) { 

    var loggedId = GetLoggedUserId(txtUsername.Text.Trim(),Encrypt(txtPassword.Text.Trim())); 

    if(!string.IsNullOrWhiteSpace(loggedId)) 
    { 
     UpdateLoggedUser(txtUsername.Text.Trim()); 
     Response.Redirect("SecurityHome.aspx"); 
    } 
    else 
    { 
     lblError.Text = "Either username and/or password is wrong. Please try again!"; 
    } 
} 

根據MSDN:https://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.80).aspx

0

用戶DataAdapter的。它比DataReader的速度更快,這也已斷開導向架構

string connString = "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); 
    using (SqlConnection conn = new SqlConnection(connString)) 
{ 

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

     if (t.Rows.Count > 0) 
     { 

     Session["Username"] = txtUsername.Text; 
     Session["Id"] = t[0]["Id"].ToString(); 
     cmd1.ExecuteNonQuery(); 
     Response.Redirect("SecurityHome.aspx"); 

     } 
     else 
     { 
     lblError.Text = "Either username and/or password is wrong. Please try again!"; 
     } 

    } 

    }