仅当错误的密码时,对象引用才设置为对象的实例

时间:2015-05-26 09:22:14

标签: c# .net

我编写代码进行登录。当我输入正确的登录名和密码时,一切正常,但是当我输入正确的登录信息但密码错误时,我会在此行Object reference not set to an instance of an object.收到错误string password = passComm.ExecuteScalar().ToString().Replace(" ", "");。任何消化如何解决它?

代码:

        protected void Button_LogIN_Click1(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(
       ConfigurationManager.
       ConnectionStrings["RegistrationConnectionString"].
       ConnectionString);

        conn.Open();

        string checkuser = "select count(*) from UserData where name = '" + TextBox_Login.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 UserData where password ='" + TextBox_Password.Text + "'";
            SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn);

            string password = passComm.ExecuteScalar().ToString().Replace(" ", "");
            if (password == TextBox_Password.Text)
            {
                Session["New"] = TextBox_Login.Text;
                Response.Write("pass ok");
            }
            else
            {
                Response.Write("pass is not ok!");
            }
        }
        else
        {
            Response.Write("Login not ok");
        }
        conn.Close();

        TextBox_Login.Text = "";
        TextBox_Password.Text = "";
    }

0 个答案:

没有答案