DataGrid & datasouce close

P

PaulMac

Hello All,

I have a webpage containing a DataGrid control. The datasource is a
DataReader. The code is like so:

AsaCommand Cmd = new AsaCommand( "SELECT ... FROM ...", Db.Connection );
DataGridNet.DataSource = Cmd.ExecuteReader();
DataGridNet.DataBind();

Is it necessary to do an explicit close on the DataReader? In which case,
the code could be this:

AsaCommand Cmd = new AsaCommand( "SELECT ... FROM ...", Db.Connection );
AsaDataReader Reader = Cmd.ExecuteReader();
DataGridNet.DataSource = Reader;
DataGridNet.DataBind();
Reader.Close();

Thanks,
Paul
 
D

David

Is it necessary to do an explicit close on the DataReader?

Yes, you should always close the DataReader explicitly. And you also
should close the connection, although you can specify this in the
ExecuteReader call.
In which case,
the code could be this:
AsaCommand Cmd = new AsaCommand( "SELECT ... FROM ...", Db.Connection );
AsaDataReader Reader = Cmd.ExecuteReader();
DataGridNet.DataSource = Reader;
DataGridNet.DataBind();
Reader.Close();

Either,

ASADataReader = Cmd.ExecuteReader()
DataGridNet.DataSource = Reader;
DAtaGridNet.DataBind();
Reader.Close();
Db.Connection.Close();

or....
AsaDataReader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
DataGridNet.DataSource = Reader;
DataGridNet.DataBind();
Reader.Close;
 
P

PaulMac

Thank you, David. I appreciate your help.

Paul

David said:
Yes, you should always close the DataReader explicitly. And you also
should close the connection, although you can specify this in the
ExecuteReader call.



Either,

ASADataReader = Cmd.ExecuteReader()
DataGridNet.DataSource = Reader;
DAtaGridNet.DataBind();
Reader.Close();
Db.Connection.Close();

or....
AsaDataReader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
DataGridNet.DataSource = Reader;
DataGridNet.DataBind();
Reader.Close;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top