1

I have a base class that contains a SqlDataReader which gets the data using a stored procedure, this works great until I return the data reader back up saying the connection is null.

Does anyone have any ideas? here's my code:

public SqlDataReader GetDataReader(string QueryName, SqlParameter[] Params)
{
   SqlConnection conn = new SqlConnection(this.ConnectionString);
   SqlDataReader reader;

   using (conn)
   {
      SqlCommand command = new SqlCommand(QueryName,conn);
      command.CommandType = CommandType.StoredProcedure;

      if(Params !=null)
          command.Parameters.Add(Params);

      conn.Open();

      reader = command.ExecuteReader();                
   }

   // conn.Close();

   return reader;
}

If you notice, I have the close part commented out, this was me trying to get it to work, for some reason when returning the datareader back up it is set to close???

Thanks!

2
  • 1
    Quite honestly: I would never pass around an "opened" SqlDataReader - that's a horrible design and just a recipe for disaster. Instead, have your method actually read the data, put it into a List<Something>, close your SqlDataReader again as quickly as possible, and return that list back to the caller... Commented Nov 14, 2011 at 9:16
  • what if you have the reader in a base class? Surely thats better design than copying and pasting the code everytime you want to access data as opposed to just using the method in the bass class which returns a reader Commented Nov 14, 2011 at 14:42

6 Answers 6

2

You want to do the following:

    SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection)

and don't close the connection. When you close the datareader it will close it for you if you use the code above.

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

1 Comment

Oh, and like DeveloperX said don't use the 'using' statement because you need the connection to stay open until the datareader is done.
2

When using (conn) block finishes, it closes the database connection, that's why you are getting that error. Just delete that line.

Comments

2

You are using "using" which does the same things as calling conn.Close().

Comments

2

Because your connection is closed and disposed in using block before returning the reader . See this SO post

1 Comment

Its not the reader, but connection being closed and disposed.
2

when you use using (conn) it automatically disposes the conn object

Comments

1

DataReader is a connected object. Means to get the data from the DataReader, the connection underneath need to be open at that moment. This is not so if you work with DataSets, which work in disconnected mode. You are closing the connection in your code before you return the DataReader. So the DataReader cannot give you any data.

A better design alternative might be providing the connection from outside (dependency injection).

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.