469,326 Members | 1,214 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Getting back a return value from a SQL Count query

I am trying to write a SQL query that will return a record count to my C#
application. I wrote the following query:

ALTER PROCEDURE up_justification_duplicate
AS
SELECT COUNT(*)
FROM tblJustifications

RETURN COUNT(*)
The table has four records in it. Running the query in the IDE gets me the
following results:

Running dbo."up_justification_duplicate".
-----------
4
No more results.
(1 row(s) returned)
@RETURN_VALUE = 1
Finished running dbo."up_justification_duplicate".

I have tried to extract the count in my application but all I know how to do
is read the return value (always a 1) with a statement like "x =
da.SelectCommand.Parameters[0].Value.ToString();".
How do I get back the correct count, in this case 4?

Any help is greatly appreciated.
Nov 16 '05 #1
1 15429
Greg,

The way you have it, the count is being returned in a result set. What
you want to do is execute the command getting the scalar. Call
ExecuteScalar on the SqlCommand you are using to access the database. If
you want to set the count to the return value, then you need to change your
stored procedure to return a value like this:

alter PROCEDURE up_justification_duplicate
AS
return select COUNT(*) from asset

Then you should be able to access the return value through the
parameters.

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

"Greg Smith" <gj*@umn.edu> wrote in message
news:ev**************@TK2MSFTNGP10.phx.gbl...
I am trying to write a SQL query that will return a record count to my C#
application. I wrote the following query:

ALTER PROCEDURE up_justification_duplicate
AS
SELECT COUNT(*)
FROM tblJustifications

RETURN COUNT(*)
The table has four records in it. Running the query in the IDE gets me the following results:

Running dbo."up_justification_duplicate".
-----------
4
No more results.
(1 row(s) returned)
@RETURN_VALUE = 1
Finished running dbo."up_justification_duplicate".

I have tried to extract the count in my application but all I know how to do is read the return value (always a 1) with a statement like "x =
da.SelectCommand.Parameters[0].Value.ToString();".
How do I get back the correct count, in this case 4?

Any help is greatly appreciated.

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Richard G | last post: by
4 posts views Thread by Mike Dinnis | last post: by
3 posts views Thread by Beryl Small | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.