469,903 Members | 2,329 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

data reader question

I'm using 1 connection to open 3 separate data readers to read data into
3 data grids. However, when I try to use the 2nd reader it says that
the first reader must be closed. So I use Close() to close it and now
it says that 'Object reference not set to an instance of an object' on
that line of code. Can anybody help me out with this?

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTransitTest"]
);

//Associated Call Types
string strAssocCallTypes;

strAssocCallTypes = "SELECT CallType AS 'Call Type', Remark AS
'Description' FROM CallTypeSwitch ";
strAssocCallTypes += "WHERE OSValue = " + intIncomingRoute;

//You must open the connection before populating the DataReader
objConnection.Open();

SqlCommand objCommand = new SqlCommand(strAssocCallTypes,
objConnection);

//Create/Populate the DataReader
SqlDataReader objDataReader = null;

dgAssocCallTypes.DataSource = objCommand.ExecuteReader();
dgAssocCallTypes.DataBind();

//can only have 1 data reader open at any one time in 1 connection
objDataReader.Close();
Cheers,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #1
1 1206
It seems to me that you never create a SqlDataReader instance in the
objDataReader variable. You declare it, but in the next line, you pass the
result of ExecuteReader (which gives you back an instance of SqlDataReader)
to dgAssocCallTypes.DataSource. So objDataReader will still be null. To set
the datareader variable, you can do this:

objDataReader = objCommand.ExecuteReader();

Hope it helps...
"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:OI*************@TK2MSFTNGP10.phx.gbl...
I'm using 1 connection to open 3 separate data readers to read data into
3 data grids. However, when I try to use the 2nd reader it says that
the first reader must be closed. So I use Close() to close it and now
it says that 'Object reference not set to an instance of an object' on
that line of code. Can anybody help me out with this?

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTransitTest"]
);

//Associated Call Types
string strAssocCallTypes;

strAssocCallTypes = "SELECT CallType AS 'Call Type', Remark AS
'Description' FROM CallTypeSwitch ";
strAssocCallTypes += "WHERE OSValue = " + intIncomingRoute;

//You must open the connection before populating the DataReader
objConnection.Open();

SqlCommand objCommand = new SqlCommand(strAssocCallTypes,
objConnection);

//Create/Populate the DataReader
SqlDataReader objDataReader = null;

dgAssocCallTypes.DataSource = objCommand.ExecuteReader();
dgAssocCallTypes.DataBind();

//can only have 1 data reader open at any one time in 1 connection
objDataReader.Close();
Cheers,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by sabarish | last post: by
6 posts views Thread by Natan Vivo | last post: by
2 posts views Thread by Robert Smith jr. | last post: by
4 posts views Thread by Derek Hart | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.