473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Calling Class

JJ
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
Nov 17 '05 #1
7 2009
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" <JJ@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
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

Nov 17 '05 #2
JJ
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" <JJ@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
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


Nov 17 '05 #3
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" <JJ@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
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" <JJ@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
> 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


Nov 17 '05 #4
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" <JJ@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
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" <JJ@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
> 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


Nov 17 '05 #5

"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in message
news:uz*************@TK2MSFTNGP12.phx.gbl...
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!
Nov 17 '05 #6

"JJ" <JJ@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
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? ;-)
Nov 17 '05 #7
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" <sc***********@no-spam.intelebill.com> wrote in message
news:eK**************@tk2msftngp13.phx.gbl...

"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in message
news:uz*************@TK2MSFTNGP12.phx.gbl...
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!

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jarmopy | last post by:
Hi, I have made a service with C# and calling that service class from another C# program with remoting. (Referendes from the calling program) That service class is configured so that garpage...
7
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is...
5
by: Dave Veeneman | last post by:
I'm using inheritance more than I used to, and I find myself calling a lot of base class methods. I generally call a base method from a dreived class like this: this.MyMethod(); I'm finding...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
2
by: sumanthsclsdc | last post by:
Hello friends, I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I...
6
by: Anthony Smith | last post by:
I can call a class using "->", but it complains about the :: I see on the net where :: is used. Is there a good explanation on when to use one over the other or the differences? $help = new...
10
by: Finger.Octopus | last post by:
Hello, I have been trying to call the super constructor from my derived class but its not working as expected. See the code: class HTMLMain: def __init__(self): self.text = "<HTML><BODY>";...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: Emile van Sebille | last post by:
mercado mercado wrote: Well, the class name is in __class__ so changing 'print x' to 'print __class__' will show foo in both cases, which is in fact the name of the class which is calling it....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.