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!
You want to do the following:
and don’t close the connection. When you close the datareader it will close it for you if you use the code above.