2014-10-01 73 views
0

好吧,所以即時嘗試使此以下錯誤:在System.Data.dll中發生類型'System.Data.SqlClient.SqlException'的異常,但未在用戶代碼中處理創建ASP.net databse註冊連接時出錯

附加信息:關鍵字「表」附近的語法不正確。

protected void Button_Login_Click(object sender, EventArgs e) 
    { 
     SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisterConnectionString"].ConnectionString); 
     conn.Open(); 
     string checkuser = "select count(*) from [Table] where Användarnamn='" + TextBoxAnvändarelogin.Text + "'"; 
     SqlCommand com = new SqlCommand(checkuser, conn); 
     int temp = Convert.ToInt32(com.ExecuteScalar().ToString()); 
     conn.Close(); 
     if (temp == 1) 
     { 
      conn.Open(); 
      string checkPasswordQuery = "select password from Table where Användarnamn='" + TextBoxAnvändarelogin.Text + "'"; 
      SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn); 
      string password = passComm.ExecuteScalar().ToString().Replace(" " , ""); // ERROR HERE! 
      conn.Close(); 
      if (password==TextBoxLösenordlogin.Text) 
      { 
       Session["New"] = TextBoxAnvändarelogin.Text; 
       Response.Write("Lösenord är rätt!"); 
       Response.Redirect("Admin.aspx"); 
      } 
      else 
      { 
       Response.Write("Lösenord är fel!"); 
      } 
     } 
     else 
     { 
      Response.Write("Användarnamn är inte rätt!"); 
     } 

    } 
} 

回答

0

string checkPasswordQuery = "select password from Table where Användarnamn
應該
string checkPasswordQuery = "select password from [Table] where Användarnamn

附註:這是從來沒有使用字符串追加建立動態SQL一個很好的做法。有關詳細信息,請參閱Sql Injection。並不應該使用「表」作爲表名

+0

好吧,所以我嘗試了這一點,但現在出現了一個新問題:在System.Data.dll中發生類型'System.Data.SqlClient.SqlException'的異常,但沒有在用戶代碼中處理 附加信息:無效的列名稱'密碼'。 – Wharruphomie 2014-10-01 20:52:18

+0

@Wharruphomie你的表中有一列名爲password的列嗎?請注意其區分大小寫 – Steve 2014-10-01 20:53:15

+0

aaaaaa好吧我改變它爲columname,因爲我在我的表中,它的作品謝謝兄弟:) – Wharruphomie 2014-10-01 20:56:17