gpt4 book ai didi

c# - 你调用的对象是空的。执行标量()

转载 作者:行者123 更新时间:2023-11-30 23:03:33 25 4
gpt4 key购买 nike

我总是收到“对象引用未设置到对象的实例”。我不知道我做错了什么。我确实尝试执行查询:“select count(*) from Registration where Username='Myname' and it returned 1;

代码:

 protected void Button1_Click(object sender, EventArgs e)
{

DBConnect con = new DBConnect();
string query = "select count(*) from Registration where Username='" + TextBox1.Text + "'";

//Open connection
if (con.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, con.Initialize());
//Create a data reader and Execute the command

int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());

if (temp == 1)
{
string query1 = "Select Password from Registration where Username='" + TextBox1.Text + "'";
MySqlCommand pass = new MySqlCommand(query1, con.Initialize());
string password = pass.ExecuteScalar().ToString();

if (password == TextBox2.Text)
{
Session["new"] = TextBox1.Text;
Response.Redirect("GreenhouseHomepage.aspx");
}
else
{
Label1.Text = "Invalid Password...!!";
Label1.Visible = true;
}
}
else
{
Label1.Text = "Invalid Username...!!";
Label1.Visible = true;
}
}

//close Connection
con.CloseConnection();
}

错误: enter image description here

最佳答案

试一试。

//Open connection
if (con.OpenConnection() == true)
{
// Since you know that the connection is open, you can just
// pass the entire conn object
MySqlCommand cmd = new MySqlCommand(query, con);
//Create a data reader and Execute the command

int temp = Convert.ToInt32(cmd.ExecuteScalar());

如果未找到任何记录,COUNT(*) 将返回 0,因此您始终会从语法中获取一个值。

关于c# - 你调用的对象是空的。执行标量(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22841336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com