PC Review


Reply
Thread Tools Rate Thread

Calling Class

 
 
=?Utf-8?B?Sko=?=
Guest
Posts: n/a
 
      22nd Sep 2005
Hi,

I call a class in my windows service app and in that class I access a
method that returns an OleDbReader. Now It does have records in the reader
when I step through the method but when I return to calling class the
OleDbReader dr is null.
What am I missing here?

Class1 calls Class2.Method which returns a OleDbReader. Class1's OleDbReader
is null.

OleDbReader dr = Class2.GetRecords();

JJ
 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      22nd Sep 2005
We can't tell you the problem if you don't post the content of the
GetRecords method. Or a reproduceable test case that shows the problem.

"JJ" <(E-Mail Removed)> wrote in message
news:C48F3FDF-6260-4510-A208-(E-Mail Removed)...
> Hi,
>
> I call a class in my windows service app and in that class I access a
> method that returns an OleDbReader. Now It does have records in the reader
> when I step through the method but when I return to calling class the
> OleDbReader dr is null.
> What am I missing here?
>
> Class1 calls Class2.Method which returns a OleDbReader. Class1's
> OleDbReader
> is null.
>
> OleDbReader dr = Class2.GetRecords();
>
> JJ



 
Reply With Quote
 
=?Utf-8?B?Sko=?=
Guest
Posts: n/a
 
      22nd Sep 2005
Calling Class1 method below:

private void GetUPS(string Path, bool CNote)
{
OleDbDataReader dr = blda.GetAccRecs(Path);

try
{
while(dr.Read())
{
}
catch(Exception ex)
{
string sEMess = "";

sEMess = ex.Message;
}


}
Other Class2 Method being called:

public OleDbDataReader GetAccRecs(string aPath)
{
OleDbConnection AccCon = new
OleDbConnection(Dev_Acc_Con1);
try
{
sSQL = "SELECT * FROM Customers";
OleDbCommand cmdUPS = new OleDbCommand();
cmdUPS.Connection = AccCon;
cmdUPS.CommandType = CommandType.Text;
cmdUPS.CommandText = sSQL;
AccCon.Open();

dr2 = cmdUPS.ExecuteReader();

return dr2;
}

So this procedure gets called and returns an OleDbReader to calling Class1.
I have checked there are records that get returned to Class1 but when Code
Statement
while(dr.Read()) gets executed, it bombs out and gives me this error:
"Invalid attempt to Read when reader is closed."

Any ideas as to why?

Thanks,

JJ


"Marina" wrote:

> We can't tell you the problem if you don't post the content of the
> GetRecords method. Or a reproduceable test case that shows the problem.
>
> "JJ" <(E-Mail Removed)> wrote in message
> news:C48F3FDF-6260-4510-A208-(E-Mail Removed)...
> > Hi,
> >
> > I call a class in my windows service app and in that class I access a
> > method that returns an OleDbReader. Now It does have records in the reader
> > when I step through the method but when I return to calling class the
> > OleDbReader dr is null.
> > What am I missing here?
> >
> > Class1 calls Class2.Method which returns a OleDbReader. Class1's
> > OleDbReader
> > is null.
> >
> > OleDbReader dr = Class2.GetRecords();
> >
> > JJ

>
>
>

 
Reply With Quote
 
Kai Brinkmann [MSFT]
Guest
Posts: n/a
 
      22nd Sep 2005
I'm not 100% positive, but I suspect your problem may be that the associated
OleDbConnection is closed. Since you are declaring the connection inside
your get method, it is only valid in this context. After GetAccRecs returns
the connection object is disposed of (and thus closed). The error message
about the closed OleDbDataReader seems consistent with this assumption.

Ideally, you should process your data inside GetAccRecs and properly close
both the reader and the connection after you're done. If you really must
process your data outside GetAccRecs, try writing it to another data
structure (maybe a DataSet) and returning a reference to this object. Theat
way you can safely close your connection before returning from GetAccRecs.
--
Kai Brinkmann [Microsoft]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

"JJ" <(E-Mail Removed)> wrote in message
news:5DA13617-2125-4740-87D3-(E-Mail Removed)...
> Calling Class1 method below:
>
> private void GetUPS(string Path, bool CNote)
> {
> OleDbDataReader dr = blda.GetAccRecs(Path);
>
> try
> {
> while(dr.Read())
> {
> }
> catch(Exception ex)
> {
> string sEMess = "";
>
> sEMess = ex.Message;
> }
>
>
> }
> Other Class2 Method being called:
>
> public OleDbDataReader GetAccRecs(string aPath)
> {
> OleDbConnection AccCon = new
> OleDbConnection(Dev_Acc_Con1);
> try
> {
> sSQL = "SELECT * FROM Customers";
> OleDbCommand cmdUPS = new OleDbCommand();
> cmdUPS.Connection = AccCon;
> cmdUPS.CommandType = CommandType.Text;
> cmdUPS.CommandText = sSQL;
> AccCon.Open();
>
> dr2 = cmdUPS.ExecuteReader();
>
> return dr2;
> }
>
> So this procedure gets called and returns an OleDbReader to calling
> Class1.
> I have checked there are records that get returned to Class1 but when Code
> Statement
> while(dr.Read()) gets executed, it bombs out and gives me this error:
> "Invalid attempt to Read when reader is closed."
>
> Any ideas as to why?
>
> Thanks,
>
> JJ
>
>
> "Marina" wrote:
>
>> We can't tell you the problem if you don't post the content of the
>> GetRecords method. Or a reproduceable test case that shows the problem.
>>
>> "JJ" <(E-Mail Removed)> wrote in message
>> news:C48F3FDF-6260-4510-A208-(E-Mail Removed)...
>> > Hi,
>> >
>> > I call a class in my windows service app and in that class I access a
>> > method that returns an OleDbReader. Now It does have records in the
>> > reader
>> > when I step through the method but when I return to calling class the
>> > OleDbReader dr is null.
>> > What am I missing here?
>> >
>> > Class1 calls Class2.Method which returns a OleDbReader. Class1's
>> > OleDbReader
>> > is null.
>> >
>> > OleDbReader dr = Class2.GetRecords();
>> >
>> > JJ

>>
>>
>>



 
Reply With Quote
 
Marina
Guest
Posts: n/a
 
      22nd Sep 2005
So that is not the same then as the method returning null. Those are 2 very
very very different things.

You are not showing what is after the 'try' block, but I suspect you have a
finally block afterwards, that is closing the connection.

A datareader requires an open and active connection to the database, since
it retrieves one row at a time. It is usually not a good idea to return
datareaders to callers outside the class, since they have to be responsible
for closing the connection - and if the developer forgets, you get
connection leaks.

You should fill a datatable instead, and return that.

"JJ" <(E-Mail Removed)> wrote in message
news:5DA13617-2125-4740-87D3-(E-Mail Removed)...
> Calling Class1 method below:
>
> private void GetUPS(string Path, bool CNote)
> {
> OleDbDataReader dr = blda.GetAccRecs(Path);
>
> try
> {
> while(dr.Read())
> {
> }
> catch(Exception ex)
> {
> string sEMess = "";
>
> sEMess = ex.Message;
> }
>
>
> }
> Other Class2 Method being called:
>
> public OleDbDataReader GetAccRecs(string aPath)
> {
> OleDbConnection AccCon = new
> OleDbConnection(Dev_Acc_Con1);
> try
> {
> sSQL = "SELECT * FROM Customers";
> OleDbCommand cmdUPS = new OleDbCommand();
> cmdUPS.Connection = AccCon;
> cmdUPS.CommandType = CommandType.Text;
> cmdUPS.CommandText = sSQL;
> AccCon.Open();
>
> dr2 = cmdUPS.ExecuteReader();
>
> return dr2;
> }
>
> So this procedure gets called and returns an OleDbReader to calling
> Class1.
> I have checked there are records that get returned to Class1 but when Code
> Statement
> while(dr.Read()) gets executed, it bombs out and gives me this error:
> "Invalid attempt to Read when reader is closed."
>
> Any ideas as to why?
>
> Thanks,
>
> JJ
>
>
> "Marina" wrote:
>
>> We can't tell you the problem if you don't post the content of the
>> GetRecords method. Or a reproduceable test case that shows the problem.
>>
>> "JJ" <(E-Mail Removed)> wrote in message
>> news:C48F3FDF-6260-4510-A208-(E-Mail Removed)...
>> > Hi,
>> >
>> > I call a class in my windows service app and in that class I access a
>> > method that returns an OleDbReader. Now It does have records in the
>> > reader
>> > when I step through the method but when I return to calling class the
>> > OleDbReader dr is null.
>> > What am I missing here?
>> >
>> > Class1 calls Class2.Method which returns a OleDbReader. Class1's
>> > OleDbReader
>> > is null.
>> >
>> > OleDbReader dr = Class2.GetRecords();
>> >
>> > JJ

>>
>>
>>



 
Reply With Quote
 
Scott Roberts
Guest
Posts: n/a
 
      22nd Sep 2005

"Kai Brinkmann [MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Since you are declaring the connection inside
> your get method, it is only valid in this context. After GetAccRecs

returns
> the connection object is disposed of (and thus closed).


Huh? I pass SqlDataReader objects out of function results all the time with
no problem. Why would the data reader be disposed? It is always referenced!


 
Reply With Quote
 
Scott Roberts
Guest
Posts: n/a
 
      22nd Sep 2005

"JJ" <(E-Mail Removed)> wrote in message
news:5DA13617-2125-4740-87D3-(E-Mail Removed)...
> public OleDbDataReader GetAccRecs(string aPath)
> {
> OleDbConnection AccCon = new
> OleDbConnection(Dev_Acc_Con1);
> try
> {
> sSQL = "SELECT * FROM Customers";
> OleDbCommand cmdUPS = new OleDbCommand();
> cmdUPS.Connection = AccCon;
> cmdUPS.CommandType = CommandType.Text;
> cmdUPS.CommandText = sSQL;
> AccCon.Open();
>
> dr2 = cmdUPS.ExecuteReader();
>
> return dr2;
> }


So this function never ends? ;-)

As Marina astutely pointed out, I bet you've got the following code that you
didn't post:

finally
{
// Some code that closes the data reader and/or connection.
}
}


The "finally" part of a "try..finally" will *ALWAYS* execute. That's why the
data reader is closed when the calling procedure tries to read from it.

Should I append my "single exit point" script here? ;-)


 
Reply With Quote
 
Kai Brinkmann [MSFT]
Guest
Posts: n/a
 
      22nd Sep 2005
I didn't say the reader was disposed! (It's not because there's still a
valid reference to it.) What I did say is that I suspected the *connection*
that the reader relies on was closed.
--
Kai Brinkmann [Microsoft]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

"Scott Roberts" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Kai Brinkmann [MSFT]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Since you are declaring the connection inside
>> your get method, it is only valid in this context. After GetAccRecs

> returns
>> the connection object is disposed of (and thus closed).

>
> Huh? I pass SqlDataReader objects out of function results all the time
> with
> no problem. Why would the data reader be disposed? It is always
> referenced!
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.Net PAge SRC Directive - Class calling another class tom@iostudios.co.uk Microsoft ASP .NET 3 12th Jun 2007 08:46 PM
Base class calling derived class methods Mike C# Microsoft VC .NET 8 14th Sep 2006 08:51 PM
calling method from one class without declaring the class again?? c#2006user Microsoft C# .NET 3 22nd Mar 2006 10:59 PM
Calling a class function from another class =?Utf-8?B?TWFjY2E=?= Microsoft C# .NET 2 13th Mar 2006 12:46 PM
InitializeComponent is generating the wrong code for my custom class. Calling it's base class constructor jrhoads23@hotmail.com Microsoft Dot NET Framework Forms 0 1st Feb 2005 07:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:13 PM.