简体   繁体   English

对象引用未设置为对象asp.net的实例

[英]object reference not set to an instance of an object asp.net

In the below code, when I press button2 it says: 在下面的代码中,当我按下button2时,它说:

object reference not set to an instance of an object 你调用的对象是空的

What's going on? 这是怎么回事?

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;

Then you try to read from null object from 然后您尝试从中读取空对象

dr.Read();

Make sure this is web page you can not keep the state if you want to get the Button_click1 data rearder 确保这是网页,你不能保持状态,如果你想获得Button_click1数据rearder

You need to assign the reader dr to a command. 您需要将阅读器dr分配给命令。

Take a look at the example here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx 在这里看看示例: http : //msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldatareader.aspx

SqlDataReader object is used to hold the one time result of executed/fetched data from the database.which can be used to iterate over each row to get the required columns. SqlDataReader对象用于保存从数据库中执行/获取的数据的一次性结果,可用于遍历每一行以获取所需的列。 hence before Trying to read from SqlDataReader object it should have some data. 因此,在尝试从SqlDataReader对象读取之前,它应该具有一些数据。

which can be accomplished by following statement: 可以通过以下语句来完成:

SqlDataReader sqldatareaderobject=sqlcommandobject.ExecuteReader(); SqlDataReader sqldatareaderobject = sqlcommandobject.ExecuteReader();

you are following this above principle in Button1_click function but you are missing the same principle in Button2_click function. 您在Button1_click函数中遵循了上述原理,但是在Button2_click函数中缺少了相同原理。

SqlDataReader object in your case "dr" contains null as you have missed to call the ExecuteReader() function, and it is throwing exception as you are calling Read() function on top of null object(dr). 在您的案例“ dr”中,SqlDataReader对象包含空值,因为您错过了调用ExecuteReader()函数的权限,而在空对象(dr)之上调用Read()函数时,它抛出了异常。

Thank you 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM