473,398 Members | 2,404 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,398 software developers and data experts.

Why wont this datareader return any values?

I am connecting to an access database using a datareader in C#. I get
results when I run a certain query from Access but when I run it from Code
it does not retrieve any results. I have put a stop point after the string
is created and it is correct. Its an inner join query so I was wandering
whether that is too complicated for a datareader to execute, or if Im
missing something else here?

Heres what Im doing in code:

-------------------------------------------
string mySelectQuery = "SELECT
[Product-Packaging-Junction].[Product-Code], " +
"[Product-Packaging-Junction].[Component-Code],
[Product-Packaging-Junction].Quantity, " +
"[Component-Codes].[Quantity-In-Stock] " +
"FROM [Component-Codes] INNER JOIN [Product-Packaging-Junction] " +
"ON [Component-Codes].Code =
[Product-Packaging-Junction].[Component-Code]" +
"WHERE (([Product-Packaging-Junction].[Product-Code]='" + prodName +
"'))";

//at this point I can copy this string value into Access and get a result...

OleDbDataReader dataReader;
OleDbConnection myDataReaderConnection = new
OleDbConnection(Constants.CONNECTION_STRING);
OleDbCommand myDataReaderCommand = new OleDbCommand(mySelectQuery,
myDataReaderConnection);
myDataReaderCommand.Connection.Open();
dataReader =
myDataReaderCommand.ExecuteReader(CommandBehavior. CloseConnection);
while (dataReader.Read())
{
Nothing happens here because the datareader is not populated
}

---------------------------------------------
Any assistance is greatly appreciated,
Grant
Nov 17 '05 #1
6 6057
Hi Grant,

"Grant" <gp*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I am connecting to an access database using a datareader in C#. I get
results when I run a certain query from Access but when I run it from Code
it does not retrieve any results. I have put a stop point after the string
is created and it is correct. Its an inner join query so I was wandering
whether that is too complicated for a datareader to execute, or if Im
missing something else here?
Something else.
Perhaps there is something wrong with INNER JOIN or WHERE part (wrong in the
sense that it won't return any records).
Try running the same query without WHERE part and if there are no rows, cut
the INNER JOIN, too.
So we can pinpoint the problem.
The other suggestion would be to use parametrised query (passing parameters
instead of putting them into the sql itself).
--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Heres what Im doing in code:

-------------------------------------------
string mySelectQuery = "SELECT
[Product-Packaging-Junction].[Product-Code], " +
"[Product-Packaging-Junction].[Component-Code],
[Product-Packaging-Junction].Quantity, " +
"[Component-Codes].[Quantity-In-Stock] " +
"FROM [Component-Codes] INNER JOIN [Product-Packaging-Junction] " +
"ON [Component-Codes].Code =
[Product-Packaging-Junction].[Component-Code]" +
"WHERE (([Product-Packaging-Junction].[Product-Code]='" + prodName +
"'))";

//at this point I can copy this string value into Access and get a
result...

OleDbDataReader dataReader;
OleDbConnection myDataReaderConnection = new
OleDbConnection(Constants.CONNECTION_STRING);
OleDbCommand myDataReaderCommand = new OleDbCommand(mySelectQuery,
myDataReaderConnection);
myDataReaderCommand.Connection.Open();
dataReader =
myDataReaderCommand.ExecuteReader(CommandBehavior. CloseConnection);
while (dataReader.Read())
{
Nothing happens here because the datareader is not populated
}

---------------------------------------------
Any assistance is greatly appreciated,
Grant

Nov 17 '05 #2
yeah, I would look closely at your prodName variable.
The way you handle it is risky because the string must be exact... I
usually wrap my strings with a method to make sure the string is clean (like
trimming spaces, doubling any single apostrophe's in the string, etc), and
maybe add a %<string>% around it. Usually for string comparisons, I use a
LIKE in the where too.

Also, as an aside, if you alias your sql tables, it will make the sql look a
lot nicer, and you don't have to type as much. :)
"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:eM*************@TK2MSFTNGP10.phx.gbl...
Hi Grant,

"Grant" <gp*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I am connecting to an access database using a datareader in C#. I get
results when I run a certain query from Access but when I run it from Code
it does not retrieve any results. I have put a stop point after the string
is created and it is correct. Its an inner join query so I was wandering
whether that is too complicated for a datareader to execute, or if Im
missing something else here?


Something else.
Perhaps there is something wrong with INNER JOIN or WHERE part (wrong in
the sense that it won't return any records).
Try running the same query without WHERE part and if there are no rows,
cut the INNER JOIN, too.
So we can pinpoint the problem.
The other suggestion would be to use parametrised query (passing
parameters instead of putting them into the sql itself).
--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Heres what Im doing in code:

-------------------------------------------
string mySelectQuery = "SELECT
[Product-Packaging-Junction].[Product-Code], " +
"[Product-Packaging-Junction].[Component-Code],
[Product-Packaging-Junction].Quantity, " +
"[Component-Codes].[Quantity-In-Stock] " +
"FROM [Component-Codes] INNER JOIN [Product-Packaging-Junction] " +
"ON [Component-Codes].Code =
[Product-Packaging-Junction].[Component-Code]" +
"WHERE (([Product-Packaging-Junction].[Product-Code]='" + prodName +
"'))";

//at this point I can copy this string value into Access and get a
result...

OleDbDataReader dataReader;
OleDbConnection myDataReaderConnection = new
OleDbConnection(Constants.CONNECTION_STRING);
OleDbCommand myDataReaderCommand = new OleDbCommand(mySelectQuery,
myDataReaderConnection);
myDataReaderCommand.Connection.Open();
dataReader =
myDataReaderCommand.ExecuteReader(CommandBehavior. CloseConnection);
while (dataReader.Read())
{
Nothing happens here because the datareader is not populated
}

---------------------------------------------
Any assistance is greatly appreciated,
Grant


Nov 17 '05 #3
There is also a more serious risk - sql injection.
That's why you should always use parametrised queries.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"John Richardson" <j3**********@hotmail.com> wrote in message
news:OK**************@TK2MSFTNGP09.phx.gbl...
yeah, I would look closely at your prodName variable.
The way you handle it is risky because the string must be exact... I
usually wrap my strings with a method to make sure the string is clean
(like trimming spaces, doubling any single apostrophe's in the string,
etc), and maybe add a %<string>% around it. Usually for string
comparisons, I use a LIKE in the where too.

Also, as an aside, if you alias your sql tables, it will make the sql look
a lot nicer, and you don't have to type as much. :)
"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:eM*************@TK2MSFTNGP10.phx.gbl...
Hi Grant,

"Grant" <gp*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I am connecting to an access database using a datareader in C#. I get
results when I run a certain query from Access but when I run it from
Code it does not retrieve any results. I have put a stop point after the
string is created and it is correct. Its an inner join query so I was
wandering whether that is too complicated for a datareader to execute, or
if Im missing something else here?


Something else.
Perhaps there is something wrong with INNER JOIN or WHERE part (wrong in
the sense that it won't return any records).
Try running the same query without WHERE part and if there are no rows,
cut the INNER JOIN, too.
So we can pinpoint the problem.
The other suggestion would be to use parametrised query (passing
parameters instead of putting them into the sql itself).
--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Heres what Im doing in code:

-------------------------------------------
string mySelectQuery = "SELECT
[Product-Packaging-Junction].[Product-Code], " +
"[Product-Packaging-Junction].[Component-Code],
[Product-Packaging-Junction].Quantity, " +
"[Component-Codes].[Quantity-In-Stock] " +
"FROM [Component-Codes] INNER JOIN [Product-Packaging-Junction] " +
"ON [Component-Codes].Code =
[Product-Packaging-Junction].[Component-Code]" +
"WHERE (([Product-Packaging-Junction].[Product-Code]='" + prodName +
"'))";

//at this point I can copy this string value into Access and get a
result...

OleDbDataReader dataReader;
OleDbConnection myDataReaderConnection = new
OleDbConnection(Constants.CONNECTION_STRING);
OleDbCommand myDataReaderCommand = new OleDbCommand(mySelectQuery,
myDataReaderConnection);
myDataReaderCommand.Connection.Open();
dataReader =
myDataReaderCommand.ExecuteReader(CommandBehavior. CloseConnection);
while (dataReader.Read())
{
Nothing happens here because the datareader is not populated
}

---------------------------------------------
Any assistance is greatly appreciated,
Grant



Nov 17 '05 #4
Wrap the whole lot in a
try
{
catch (OleDbException e)
{
strLastErrorMessage = e.Message;
}

and put a break point on the exception. Message should tell you why it is
not doing anything if it throwing an exception.

tcss
"Grant" wrote:
I am connecting to an access database using a datareader in C#. I get
results when I run a certain query from Access but when I run it from Code
it does not retrieve any results. I have put a stop point after the string
is created and it is correct. Its an inner join query so I was wandering
whether that is too complicated for a datareader to execute, or if Im
missing something else here?

Heres what Im doing in code:

-------------------------------------------
string mySelectQuery = "SELECT
[Product-Packaging-Junction].[Product-Code], " +
"[Product-Packaging-Junction].[Component-Code],
[Product-Packaging-Junction].Quantity, " +
"[Component-Codes].[Quantity-In-Stock] " +
"FROM [Component-Codes] INNER JOIN [Product-Packaging-Junction] " +
"ON [Component-Codes].Code =
[Product-Packaging-Junction].[Component-Code]" +
"WHERE (([Product-Packaging-Junction].[Product-Code]='" + prodName +
"'))";

//at this point I can copy this string value into Access and get a result...

OleDbDataReader dataReader;
OleDbConnection myDataReaderConnection = new
OleDbConnection(Constants.CONNECTION_STRING);
OleDbCommand myDataReaderCommand = new OleDbCommand(mySelectQuery,
myDataReaderConnection);
myDataReaderCommand.Connection.Open();
dataReader =
myDataReaderCommand.ExecuteReader(CommandBehavior. CloseConnection);
while (dataReader.Read())
{
Nothing happens here because the datareader is not populated
}

---------------------------------------------
Any assistance is greatly appreciated,
Grant

Nov 17 '05 #5
I need a flogging for being so stoopid.

I was using the wrong database. Ive just spent the past couple of hours
swearing at the datareader.

Sorry bout that.

"Grant" <gp*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I am connecting to an access database using a datareader in C#. I get
results when I run a certain query from Access but when I run it from Code
it does not retrieve any results. I have put a stop point after the string
is created and it is correct. Its an inner join query so I was wandering
whether that is too complicated for a datareader to execute, or if Im
missing something else here?

Heres what Im doing in code:

-------------------------------------------
string mySelectQuery = "SELECT
[Product-Packaging-Junction].[Product-Code], " +
"[Product-Packaging-Junction].[Component-Code],
[Product-Packaging-Junction].Quantity, " +
"[Component-Codes].[Quantity-In-Stock] " +
"FROM [Component-Codes] INNER JOIN [Product-Packaging-Junction] " +
"ON [Component-Codes].Code =
[Product-Packaging-Junction].[Component-Code]" +
"WHERE (([Product-Packaging-Junction].[Product-Code]='" + prodName +
"'))";

//at this point I can copy this string value into Access and get a
result...

OleDbDataReader dataReader;
OleDbConnection myDataReaderConnection = new
OleDbConnection(Constants.CONNECTION_STRING);
OleDbCommand myDataReaderCommand = new OleDbCommand(mySelectQuery,
myDataReaderConnection);
myDataReaderCommand.Connection.Open();
dataReader =
myDataReaderCommand.ExecuteReader(CommandBehavior. CloseConnection);
while (dataReader.Read())
{
Nothing happens here because the datareader is not populated
}

---------------------------------------------
Any assistance is greatly appreciated,
Grant

Nov 17 '05 #6
Yeah, always blame the poor innocent datareader :-)

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Grant" <gp*****@hotmail.com> wrote in message
news:us**************@TK2MSFTNGP12.phx.gbl...
I need a flogging for being so stoopid.

I was using the wrong database. Ive just spent the past couple of hours
swearing at the datareader.

Nov 17 '05 #7

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

Similar topics

1
by: VIJAY KUMAR | last post by:
Hi, Problem: Return ArrayList where Values are Added using DataReader. Note: My Data Reader returns M Columns and N Rows I am using DataReader in Data Access layer. I want to return this...
5
by: Rob Wire | last post by:
For the code below, how could I add an item in the drop down lists for both company and location to be an "All" selection that would send to the stored proc. spRptAttachments a value of "%" so...
2
by: Martin Raychev | last post by:
Hi all, I have the following problem: I have a private method that returns a SqlDataReader. For this to work I have not to close the DB connection in the above method. I do this only to
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
1
by: Jim via DotNetMonster.com | last post by:
Hi, I'm trying to set one of the columns in the datareader to a variable so that I can use it in a different function. When I do that then all the other values in the data reader don't show up....
3
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
4
by: Shapper | last post by:
Hello, I have created a datareader function in a asp.net/vb web site. The datareader returns only one record with 2 fields: and In the same aspx.vb I want to use this values as follows:...
4
by: hazz | last post by:
The data access layer below returns, well, a mess as you can see on the last line of this posting. What is the best way to return customer objects via a datareader from the data layer into my view...
3
by: Johnny Jörgensen | last post by:
I've got an error that I simply cannot locate: I've got a form in which I use a datareader object to read information from a db. After the read, I close the reader and I dispose of both the...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.