密码不通过ASP.Net应用程序重置数据库

时间:2016-10-13 08:19:18

标签: c# asp.net database

我正在尝试通过ASP.Net应用程序将密码重置为数据库上的员工编号。问题是,即使我收到密码已被重置的确认消息,它实际上并没有在DB上重置密码。

这是我目前的代码

SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlCmd = new SqlCommand();
string empNo = "";

empNo = txtEmpNo.Text;
empReset = txtEmpNo.Text;
// sql statement to check if employee number exists in DB
SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM tbl_Login WHERE (Emp_ID = @user)", sqlcon);
check_User_Name.Parameters.AddWithValue("@user", empNo);
int UserExist = (int)check_User_Name.ExecuteScalar();

if (UserExist > 0)
{
    //EmpNo exists
    sqlCmd = new SqlCommand("update tbl_Login set Emp_Pass=@Pass where Emp_ID=@EmpID", sqlcon);
    sqlCmd.Parameters.AddWithValue("@EmpID", empNo);
    sqlCmd.Parameters.AddWithValue("@Pass", "1234");
    lblExists.Text = "Password reset!";
}// end if
else
{
    //EmpNo doesnt exist
    lblExists.Text = "Employee doesnt exists";
}//end else

2 个答案:

答案 0 :(得分:1)

你错过了

sqlCmd.ExecuteNonQuery();

答案 1 :(得分:1)

您需要在if:

中执行第二个语句
if (UserExist > 0)
{
    //EmpNo exists
    sqlCmd = new SqlCommand("update tbl_Login set Emp_Pass=@Pass where Emp_ID=@EmpID", sqlcon);
    sqlCmd.Parameters.AddWithValue("@EmpID", empNo);
    sqlCmd.Parameters.AddWithValue("@Pass", "1234");
    sqlCmd.ExecuteNonQuery(); // <- execute update
    lblExists.Text = "Password reset!";
}// e