473,586 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_justificatio n_duplicate
AS
SELECT COUNT(*)
FROM tblJustificatio ns

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

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

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.SelectComman d.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 15636
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_justificatio n_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.co m

"Greg Smith" <gj*@umn.edu> wrote in message
news:ev******** ******@TK2MSFTN GP10.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_justificatio n_duplicate
AS
SELECT COUNT(*)
FROM tblJustificatio ns

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

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

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.SelectComman d.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
23507
by: Richard G | last post by:
I'm a database guy, so go easy on me here. :) How can I get the rowcount of the affected rows of a SQL statement from a stored procedure call? I know that "set nocount on" does not return the number of affected rows to the client, so I would assume that "set nocount off" sends the number of affected rows to the client, and therefore, is...
4
2981
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered techniques where i create a sql string in the page and pass it to the Db and retrieveing data from a stored procedure, but I can't get the hang of parameters. I have a method where I can get the...
3
1994
by: Beryl Small | last post by:
Hi, I have a third party software that needs to send information to an .aspx page for processing to communicate with an SQL database. The software sends the information something like this: Mypage.aspx?Password=Mypassword When this request hits the page, I need to pull that variable Mypassword to use in my VB function to query the...
6
3493
by: Max | last post by:
Anyone know why I'm always getting 0 returned? My stored procedure returns -1. Dim iErrorCode As Int32 iErrorCode = Convert.ToInt32(SqlHelper.ExecuteScalar(AppVars.strConn, _ "gpUpdateMember", _ Convert.ToInt32(lblMember_id.Text), _ -snip- -Max
1
2684
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting, paginantion or accepting the add and remove item events. What i am observing is that none of the vents are happening... (Sort, page, or item). I am sure I...
59
7472
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when the recordset opens a table. When I write Set rst = db.OpenRecordset("MyTable",dbOpenTable, dbReadOnly) I get an error. I believe it's...
2
13490
by: rafficabdullah | last post by:
I need to get input from user and search in the database in shell scrpit I have given the code below how to pass the value of varialbe Count to that query . I have bolded the variable since idont know to pass. Help me regarding this echo "Ennter a value" read Count VALUE=`sqlplus -silent invstgspring5/invstgspring5@invstg <<END set...
22
2784
by: Rickster66 | last post by:
As Instructed this is a new thread regarding my original post: "Select Only 10 Columns Going Back" I'm sorry for the late response. I've been gathering up information and carefully with as much detail as possible, making clear and straiforward for you. I need to create 3 new queries based on the queries that you wrote. Each query has a...
1
4912
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category order, Input field and a submit button. The Category name is being fetched from the oracle db along with the corresponding Category order. In...
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6614
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.