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

execute reader

I'm using this code to get data from table 'Slike'.
I would like to get also the number of row from that table.
What must I change in code to make it work?

SqlDataReader sqlRead = null;

System.Data.SqlClient.SqlCommand Slike = new
System.Data.SqlClient.SqlCommand();

Slike.CommandText = "dbo.[Slike]";

Slike.CommandType = System.Data.CommandType.StoredProcedure;

Slike.Connection = db.sqlConn;

Slike.Parameters.Add( new System.Data.SqlClient.SqlParameter("@RowCount",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
false, ((System.Byte)(0)), ((System.Byte)(0)), "",
System.Data.DataRowVersion.Current, null) );

sqlRead=Slike.ExecuteReader();
int slikeCount = (int)Slike.Parameters["@RowCount"].Value;

Hrcko
Jul 10 '07 #1
8 2798
You might have to read through all the data before the values are
available - and of course your SP would have to RETURN some suitable
number (the return value is not the same as the ROWCOUNT output that
you get by default). RETURN @@ROWCOUNT might be what you are after?

To test if this is the case (without making life too complex), just
put in:

// ... ExecuteReader
do {
while(sqlRead.Read()) {}
} while (sqlRead.NextResult());
// ... slikeCount =
this will blitz through all the data (throwing it all away) - but it
will allow you to check this fairly easily.

Marc

Jul 10 '07 #2
Hi,

"Hrvoje Voda" <hr*********@luatech.comwrote in message
news:f7**********@ss408.t-com.hr...
I'm using this code to get data from table 'Slike'.
I would like to get also the number of row from that table.
What must I change in code to make it work?

SqlDataReader sqlRead = null;

System.Data.SqlClient.SqlCommand Slike = new
System.Data.SqlClient.SqlCommand();

Slike.CommandText = "dbo.[Slike]";

Slike.CommandType = System.Data.CommandType.StoredProcedure;
Is Slike a SP or a table?
Slike.Connection = db.sqlConn;

Slike.Parameters.Add( new System.Data.SqlClient.SqlParameter("@RowCount",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
false, ((System.Byte)(0)), ((System.Byte)(0)), "",
System.Data.DataRowVersion.Current, null) );

sqlRead=Slike.ExecuteReader();
I'm not clear of your DB objects, you have several options though:
1- use a SP and an output parameter and do a
SET @param = @@ROWcount

inmediately after your SELECT .... query

You could also use SqlDataReader.RecordsAffected , but it's not active
until after the reader is closed.
Jul 10 '07 #3
Problem is in ExecuteReader function.

When I use ExecuteNonQuery then it works, but I need to use sqlRead variable
to get integer values for image.
How can I combaine those two?
Hrcko

"Marc Gravell" <ma**********@gmail.comwrote in message
news:eS****************@TK2MSFTNGP03.phx.gbl...
You might have to read through all the data before the values are
available - and of course your SP would have to RETURN some suitable
number (the return value is not the same as the ROWCOUNT output that you
get by default). RETURN @@ROWCOUNT might be what you are after?

To test if this is the case (without making life too complex), just put
in:

// ... ExecuteReader
do {
while(sqlRead.Read()) {}
} while (sqlRead.NextResult());
// ... slikeCount =
this will blitz through all the data (throwing it all away) - but it will
allow you to check this fairly easily.

Marc

Jul 10 '07 #4
Did you try my suggestion (this is to test what I suggested)?

As I recall, output params (and return values) are sent at the far end
of the stream (since they might get updated after your last SELECT),
and as such aren't updated until after you have read *past* the data
in the stream. With ExecuteNonQuery it will essentially do this for
you, since it isn't interested in the results.

For the same reason, even when I only expect a single row/grid, I
always recommend making sure you have read past all the data;
otherwise you can get oddities such as missed errors (if the error
follows a SELECT).

Marc
Jul 10 '07 #5
Hi,

"Hrvoje Voda" <hr*********@luatech.comwrote in message
news:f7**********@ss408.t-com.hr...
Problem is in ExecuteReader function.

When I use ExecuteNonQuery then it works, but I need to use sqlRead
variable to get integer values for image.
How can I combaine those two?
Hrcko
If you are using a SqlDataReader then the output parameter will be accesible
ONLY AFTER you finish iterating in the reader.

I think that you need to let us know what you want to do in the first place.
Jul 10 '07 #6
I want to get data from table with store procedure "Slike" and I want to
know how many rows are in that
result.
In store procedure after select I use RETURN @RowCount.
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:eB**************@TK2MSFTNGP05.phx.gbl...
Hi,

"Hrvoje Voda" <hr*********@luatech.comwrote in message
news:f7**********@ss408.t-com.hr...
>Problem is in ExecuteReader function.

When I use ExecuteNonQuery then it works, but I need to use sqlRead
variable to get integer values for image.
How can I combaine those two?
Hrcko

If you are using a SqlDataReader then the output parameter will be
accesible ONLY AFTER you finish iterating in the reader.

I think that you need to let us know what you want to do in the first
place.

Jul 10 '07 #7
Hi,

"Hrvoje" <hr**********@zg.htnet.hrwrote in message
news:f7**********@ss408.t-com.hr...
>I want to get data from table with store procedure "Slike" and I want to
know how many rows are in that
result.
In store procedure after select I use RETURN @RowCount.
You have to consume the Reader and only at the end you will have access to
the output parameter, in the mean time it will be null.
ADO.NET (or maybe SQL Server) send first the resultset and at the end the
output/return parameters
Jul 10 '07 #8
and I want to know how many rows are in that result.

As has been mentioned a handful of times, you can only do this after
iterating the rows... but just remember that in this case, you can
just as easily count the rows as you process them. If you *only* want
the count, then "SELECT COUNT(1)". If you absolutely need the count
first, then the only real option (short of loading all the data into
memory) is to issue two commands - one to SELECT the COUNT, and a
second command to SELECT the data.

Marc

Jul 10 '07 #9

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

Similar topics

8
by: Todd Bright | last post by:
Is there a way to get the current XmlNode from the reader while in the validation event handler? What I'd like to do is display the error message along with the name of its parent node. In my...
15
by: Madhanmohan S | last post by:
Hi All, I want to run a command line appplication from C#. When i start the application, it will go into specific mode. After that, i have to give commands to use the application. How Can This Be...
4
by: Paul | last post by:
Hello, I tried to execute a button on an aspx webpage using WebRequest, but it failed. When posting data as if I did a login (see code), I just receive the login page, instead of the page I...
4
by: AndrewDotHay | last post by:
Periodically, we see an ASPX page fail with this appearing in the logs. Any ideas? It happens about every 3 to 4 hours, requiring a restart of the application domain to resolve it. Is this a COM...
3
by: Russell Verdun | last post by:
From a vb.net application I'm want to execute an Oracle Stored Procedure via a web service. What would be the best way to approach this? I can't pass and Oracle Command or the parameter objects...
2
by: joe1977 | last post by:
Win2k3, PHP 5, Apache 2, Acrobat 7 when I go to my server, pull out cmd.exe and type as follows: "c:\Program Files\Adobe\Acrobat 7.0\\Reader\AcroRd32.exe" /t "c:\Program Files\Adobe\Acrobat...
1
by: Veerle | last post by:
Hi, I have a web page and at the end of the page I have inserted some javascript that checks if Acrobat Reader plugin is installed. If it is not installed, I disable some elements (buttons,...
0
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post by:
Hi all people, everybody, We have multiple versions of Acrobat Reader from 5.x to 8.x, I want to create a method in C# or VB.NET to check to see if the registry key for the versions...
4
by: K Viltersten | last post by:
Today, i run the code below and while it works, i can't stop wondering if it can be performed in a better way. Especially, i'd like to know if the declaration of the adapter is neccessary. ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.