![](/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.