![](/img/trans.png)
[英]System.NullReferenceException: „Object reference not set to an instance of an object.” problem
[英]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");
}
}
}
<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>
您收到错误是因为您在 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");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.