2

In the below code, when I press button2 it says:

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);
      }
  }
}
5
  • 4
    You are declaring SqlDataReader dr = null; and then on the next line invoking a Read() method on null, what else do you expect? Commented Oct 20, 2013 at 1:52
  • if i put SqlDataReader dr; it will give me this error :: Use of unassigned local variable 'dr' Commented Oct 20, 2013 at 1:56
  • You would need to read up on variable scope. It is giving you that error because "dr" has not been assigned any value. Commented Oct 20, 2013 at 1:59
  • thank you could you please tell me what the solution in my case Commented Oct 20, 2013 at 2:01
  • Almost all cases of NullReferenceException are the same. Please see "What is a NullReferenceException in .NET?" for some hints. Commented Oct 20, 2013 at 2:09

3 Answers 3

3
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

Sign up to request clarification or add additional context in comments.

Comments

1

You need to assign the reader dr to a command.

Take a look at the example here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

Comments

1

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. hence before Trying to read from SqlDataReader object it should have some data.

which can be accomplished by following statement:

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.

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).

Thank you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.