System.NullReferenceException: 'Object reference not set to an instance of an object.' 这里的问题是什么?

System.NullReferenceException: 'Object reference not set to an instance of an object.' What is the Issue here?

错误说 System.Data.Common.DbCommand.ExecuteScalar(...) 返回了 null。

protected void Button3_OnClick(object sender, EventArgs e)
            {
    
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisterConnectionString"].ConnectionString);
            conn.Open();
            string checkuser = "select count(*) from [User] where emailAddress='" + TextBox6.Text + "'";
            SqlCommand cmd = new SqlCommand(checkuser, conn);
            int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());
            conn.Close();
            if (temp >= 1)
            {
                conn.Open();
                string checkPasswordQuery = "select password from [User] where emailAddress='" + TextBox7.Text + "'";
                SqlCommand Passcmd = new SqlCommand(checkPasswordQuery, conn);
                string password = Passcmd.ExecuteScalar().ToString();
                if (password == TextBox7.Text)
                {
                    Session["New"] = TextBox6.Text;
                    Response.Write("Password is correct");
                    Response.Redirect("WebForm1.aspx");
                }
                else
                    Response.Write("Password is incorrect");

            }

            else
                Response.Write("Username not found");
        }
    }
}

据说错误发生在这样写的行上:string password = Passcmd.ExecuteScalar().ToString();
以下是我的网络配置:
    <configuration>
  <connectionStrings>
    <add name="RegisterConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\User.mdf;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" />
  </system.web>
    <system.webServer>

您收到错误是因为您在已返回 'null' 的 ExecuteScaler 上调用 .ToString()。

基于此,问题是为什么它给你 'null'?

以下是基于我们的代码的推测,但是...

您的第一个查询(似乎有效)使用 'TextBox6.Text' 作为电子邮件地址,而您的第二个查询(无效)使用 'TextBox7.Text' 作为电子邮件地址。

如果这不是故意的(即如果 TextBox6.Text 是电子邮件地址的正确框)那么我建议您的第二个查询应该是:

string checkPasswordQuery = "select password from [User] where emailAddress='" + TextBox6.Text + "'";

尝试注释掉代码中的第二个 conn.Open() 和 con.close()。

protected void Button3_OnClick(object sender, EventArgs e)
            {
    
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisterConnectionString"].ConnectionString);
            conn.Open();
            string checkuser = "select count(*) from [User] where emailAddress='" + TextBox6.Text + "'";
            SqlCommand cmd = new SqlCommand(checkuser, conn);
            int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());
            // conn.Close();  ********* comment out ***********
            if (temp >= 1)
            {
                // conn.Open();  ********* comment out ***********
                string checkPasswordQuery = "select password from [User] where emailAddress='" + TextBox7.Text + "'";
                SqlCommand Passcmd = new SqlCommand(checkPasswordQuery, conn);
                string password = Passcmd.ExecuteScalar().ToString();
                if (password == TextBox7.Text)
                {
                    Session["New"] = TextBox6.Text;
                    Response.Write("Password is correct");
                    Response.Redirect("WebForm1.aspx");
                }
                else
                    Response.Write("Password is incorrect");

            }

            else
                Response.Write("Username not found");
        }
    }
}