469,963 Members | 2,086 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

datareader return with one result instead of two

Hi,

I am using Datareader and stored procedure in C# ADO.NET.
When I am running the stored procedure in the SQL Query Analyzer and
recieve two rows (as I hace expected)
col1 col2
0 1
0 2
4 2

but when I run it via code the datareader returns with only one result.

Hope someone overcome this and could help
-------------------------------------------------------------------------------------------------------------------------
The Stored procedure

CREATE PROCEDURE FetchData @columnlist char(255), @recordlist
char(255), @tablename char(255)

AS
BEGIN
SET NOCOUNT ON

DECLARE @SQL varchar(8000)

SET @SQL =
' SELECT ' + @columnlist + ' FROM ' + @tablename + 'WHERE rd_id IN
(' + @recordlist + ')'

EXEC(@SQL)
END
GO

-------------------------------------------------------------------------------------------------------------------------
The code
string sqlCommand = "FetchData";

DBCommandWrapper dbCommandWrapper =
db.GetStoredProcCommandWrapper(sqlCommand);
dbCommandWrapper.AddInParameter("@columnlist", DbType.String,
sColumn);
dbCommandWrapper.AddInParameter("@recordlist", DbType.String, records);
dbCommandWrapper.AddInParameter("@tablename", DbType.String, table);
dataReader = db.ExecuteReader(dbCommandWrapper);
do {
while (dataReader.Read()) {
try{
o = dataReader["rms_v1_max"]; }catch{}
try{
o = dataReader["rms_v1_min"]; }catch{}

}
} while (dataReader.NextResult());
*****The innner while runs only one time because the NextResult return
false
Thanks in advance
Oren

Nov 20 '05 #1
2 3232
Well, the loop calling NextResult should only run once. You are only
returning one result set. From what I can tell, your code looks correct
(and from what you say, acting correct). Since you are returning one result
set, your call to NextResult runs once, and cycles through the rows in that
result set.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<or****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi,

I am using Datareader and stored procedure in C# ADO.NET.
When I am running the stored procedure in the SQL Query Analyzer and
recieve two rows (as I hace expected)
col1 col2
0 1
0 2
4 2

but when I run it via code the datareader returns with only one result.

Hope someone overcome this and could help
-------------------------------------------------------------------------------------------------------------------------
The Stored procedure

CREATE PROCEDURE FetchData @columnlist char(255), @recordlist
char(255), @tablename char(255)

AS
BEGIN
SET NOCOUNT ON

DECLARE @SQL varchar(8000)

SET @SQL =
' SELECT ' + @columnlist + ' FROM ' + @tablename + 'WHERE rd_id IN
(' + @recordlist + ')'

EXEC(@SQL)
END
GO

-------------------------------------------------------------------------------------------------------------------------
The code
string sqlCommand = "FetchData";

DBCommandWrapper dbCommandWrapper =
db.GetStoredProcCommandWrapper(sqlCommand);
dbCommandWrapper.AddInParameter("@columnlist", DbType.String,
sColumn);
dbCommandWrapper.AddInParameter("@recordlist", DbType.String, records);
dbCommandWrapper.AddInParameter("@tablename", DbType.String, table);
dataReader = db.ExecuteReader(dbCommandWrapper);
do {
while (dataReader.Read()) {
try{
o = dataReader["rms_v1_max"]; }catch{}
try{
o = dataReader["rms_v1_min"]; }catch{}

}
} while (dataReader.NextResult());
*****The innner while runs only one time because the NextResult return
false
Thanks in advance
Oren

Nov 20 '05 #2
Nicholas, thank you for your response,

Even if the NextResult should only run once I expect the datareader to
return first the col1 items:0,0,4 then col2 items: 1,2,2
In reality it only returns col1 items.
I have change the code in a way that I don't use stored procedure but
two select stetments and the datareader behave as It should, hence
returns the col1 items after the first iteration and then after the
NextResult, in the second inner loop iteration the values of col2.

Is it ring a bell to you or to anyone?

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by VIJAY KUMAR | last post: by
2 posts views Thread by Islam Elkhayat | last post: by
20 posts views Thread by Mark | last post: by
3 posts views Thread by Lubomir | last post: by
3 posts views Thread by tshad | last post: by
3 posts views Thread by tshad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.