gpt4 book ai didi

c# - 未将对象引用设置为对象的实例 asp.net

转载 作者:行者123 更新时间:2023-12-02 21:46:17 26 4
gpt4 key购买 nike

在下面的代码中,当我按下按钮2时,它会显示:

object reference not set to an instance of an object

发生什么事了?

public partial class rec : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|DB.mdf;Integrated Security=True;User Instance=True");

SqlCommand cmd;
con.Open();

cmd = new SqlCommand("SELECT SrviceType, Msg FROM OrderNum ", con);

SqlDataReader dr;
dr = cmd.ExecuteReader();

dr.Read();

Label1.Text = dr[0].ToString();
TextBox1.Text = dr[1].ToString();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}

protected void Button2_Click(object sender, EventArgs e)
{
SqlDataReader dr = null;

try
{
dr.Read();
Label1.Text = dr[0].ToString();
TextBox1.Text = dr[1].ToString();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}

最佳答案

SqlDataReader dr = null;

然后你尝试从

读取空对象
dr.Read();

如果您想获取Button_click1数据回放,请确保这是您无法保持状态的网页

关于c# - 未将对象引用设置为对象的实例 asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19473089/

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