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);
      }
  }
}
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Shakir Nasser
  • 43
  • 1
  • 2
  • 6
  • 4
    You are declaring `SqlDataReader dr = null;` and then on the next line invoking a `Read()` method on null, what else do you expect? – PSL Oct 20 '13 at 01:52
  • if i put SqlDataReader dr; it will give me this error :: Use of unassigned local variable 'dr' – Shakir Nasser Oct 20 '13 at 01:56
  • You would need to read up on variable scope. It is giving you that error because "dr" has not been assigned any value. – PhoenixReborn Oct 20 '13 at 01:59
  • thank you could you please tell me what the solution in my case – Shakir Nasser Oct 20 '13 at 02:01
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 20 '13 at 02:09

3 Answers3

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

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Mudeyanse
  • 50
  • 5
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

Jerry
  • 3,968
  • 2
  • 26
  • 57
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

Sudhakar Tillapudi
  • 24,787
  • 5
  • 32
  • 62