我已將代碼更改爲此代碼,但現在我得到的另一個問題是未將對象引用設置爲對象的實例。錯誤是:我得到的對象引用未設置爲對象的實例
string Password = Pass.ExecuteScalar().ToString().Replace(" ", "");
protected void btn_login_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(
ConfigurationManager
.ConnectionStrings["RegistrationCTIConnectionString"].ConnectionString);
conn.Open();
string checkuser = "Select count(*) from [tblEmployee]
where UserID= '" + txt_userID + "'";
SqlCommand com = new SqlCommand(checkuser, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
conn.Close();
if (temp != 1)
{
conn.Open();
string checkPassword = "Select Password from [tblEmployee]
where UserID= '" + txt_userID + "'";
SqlCommand Pass = new SqlCommand(checkPassword, conn);
string Password = Pass.ExecuteScalar().ToString().Replace(" ", "");
if (Password == txt_password.Text)
{
Session["New"] = txt_userID.Text;
Response.Write("Password is correct.");
Response.Redirect("~/Homepage.aspx");
}
}
else
{
Response.Write("Login is incorrect.");
}
}
哪一行有錯誤? –
你甚至不知道問題出在哪裏,這是你所指的對象...... – Lynx
'ExecuteScalar'可以返回'null'。 'txt_userID'也是'TextBox'。我建議在查詢中使用參數並傳遞'txt_userID.Text'而不是'txt_userID'。 –