Thanks Joe. You're right. I learned that datareaders need to have a
live connection just so you could
access its contents. What I did now is to use a dataset instead of a
datareader. My code now looks like this:
public bool getFromTableENROLLEE_TA(String OldGSISNo, out DataSet
dsENROLLEE_TA)
{
String sqlSelect = "SELECT ...";
OdbcDataAdapter adapter = new OdbcDataAdapter();
DataSet ds = new DataSet();
int rowCount = 0;
using (OdbcConnection cnn = new OdbcConnection(@DB2ConnectionStr))
{
using (OdbcCommand cmd = new OdbcCommand(sqlSelect, cnn))
{
cmd.CommandType = CommandType.Text;
adapter.SelectCommand = cmd;
rowCount = adapter.Fill(ds, "ENROLLEE_TAResult");
} // of inner using
} // of outer using
dsENROLLEE_TA = ds;
return (rowCount > 0); // return true if ds has data in it.
} // getFromTableENROLLEE_TA()
It works now. :)
Benj
On Sep 27, 3:36 am, Joe Enos <[EMAIL PROTECTED]> wrote:
> If you try running that code, you'll see that the reader is no good
> after it's passed out of that method. The reason is that the
> connection is disposed before the reader is returned - even though
> you're not explicitly doing it, it happens at the end of the using
> (which happens regardless of whether you return early).
>
> Try stepping through your code - reader[0] or reader.GetInt64(0) or
> whatever you use to access the data will be fine when you're inside
> the method, but once you leave, they'll be unavailable.
>
> On Sep 24, 6:54 pm, Benj Nunez <[EMAIL PROTECTED]> wrote:
>
> > Hello everyone,
>
> > Thanks again for your help. The reason why I asked this
> > question is because I am having doubts on what I did to
> > my code. You see, I have a code that looks like this:
>
> > public bool getFromTableENROLLEE_TA(String OldGSISNo, out
> > OdbcDataReader ETAReader)
> > {
>
> > string slqSelect = "select .. from <table>";
>
> > ETAReader = null;
>
> > using (OdbcConnection cnn = new OdbcConnection(@DB2ConnectionStr))
> > {
> > using (OdbcCommand cmd = new OdbcCommand())
> > {
> > cmd.Connection = cnn;
> > cmd.CommandText = sqlSelect;
> > cmd.CommandType = CommandType.Text;
>
> > OdbcDataReader reader = null;
>
> > try
> > {
> > cnn.Open();
> > reader = cmd.ExecuteReader();
>
> > if (reader.Read())
> > {
> > ETAReader = reader;
> > return;
> > }
> > ...
> > }
>
> > }
>
> > }
>
> > You will notice that I passed in a reader object as a parameter.
> > What concerns me is the state of the 3 objects: Connection, Command
> > and the reader objects. If I managed to get the values off from the
> > reader, how am I sure that the connection and command objects
> > were disposed of properly? Should I dispose them from the caller?
> > Is this considered a bad practice?
>
> > If this code needs revising I will do it, with your help of course. :)
>
> > Regards,
>
> > Benj
>
> > On Sep 25, 2:16 am, Joe Enos <[EMAIL PROTECTED]> wrote:
>
> > > I'm with rhaazy on this, except with one addition - make sure your
> > > connection is still open when you pass it out. For example:
>
> > > public void DoSomethingWithReader(string sql)
> > > {
> > > SqlDataReader reader = null;
> > > using (SqlConnection conn = {...})
> > > {
> > > using (SqlCommand comm = {...})
> > > {
> > > reader = comm.ExecuteReader();
> > > GetStuffFromReader(reader); // This one should be fine
> > > }
> > > }
> > > GetStuffFromReader(reader); // This one is not ok, since the
> > > connection is disposed
>
> > > }
>
> > > On Sep 24, 2:50 am, Benj Nunez <[EMAIL PROTECTED]> wrote:
>
> > > > Hello everyone,
>
> > > > Just out of curiosity: Is it alright to pass a datareader object to a
> > > > method or not?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://cm.megasolutions.net/forums/default.aspx
<p><a href="http://feeds.feedburner.com/DotNetDevelopment"><img
src="http://feeds.feedburner.com/~fc/DotNetDevelopment?bg=99CCFF&fg=444444&anim=1"
height="26" width="88" style="border:0" alt="" /></a></p>
-~----------~----~----~----~------~----~------~--~---