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

Need help: Call server side Stored Procedure and get the return va

I have trouble with it.

I have created a sproc in SQL Server, called SearchClass, which returns a
searched key from a table. I have tested this procedure in Analyzer and it
works fine. However, in the client side, when I use ExecuteNonQuery as follows
---
Me.SqlConnection1.Open()
classKey = Me.SqlCommand2.ExecuteNonQuery()
Me.SqlConnection1.Close()
--
it always returns -1 rather than 5 that should be.
What is the problem?

Thank you for any help.

David
Oct 19 '06 #1
3 1515
ExecuteNonQuery does not return the return value of a sp, it returns rows
affected (addedd, deleted, changed). a query always returns -1

if you want a return values, you need to added a return value parameter:

SqlCommand cmd = new SqlCommand(spname,myConnection);
SqlParameter retparam = new SqlParameter("RV",SqlDbType.Int);
retparam.Direction= ParameterDirection.ReturnValue;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
int returnValue = (int) retparam.Value;

-- bruce (sqlwork.com)
"david" <da***@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
>I have trouble with it.

I have created a sproc in SQL Server, called SearchClass, which returns a
searched key from a table. I have tested this procedure in Analyzer and it
works fine. However, in the client side, when I use ExecuteNonQuery as
follows
---
Me.SqlConnection1.Open()
classKey = Me.SqlCommand2.ExecuteNonQuery()
Me.SqlConnection1.Close()
--
it always returns -1 rather than 5 that should be.
What is the problem?

Thank you for any help.

David

Oct 19 '06 #2
Thank you very much.
I will try it tomorrow in my office.

David

"bruce barker (sqlwork.com)" wrote:
ExecuteNonQuery does not return the return value of a sp, it returns rows
affected (addedd, deleted, changed). a query always returns -1

if you want a return values, you need to added a return value parameter:

SqlCommand cmd = new SqlCommand(spname,myConnection);
SqlParameter retparam = new SqlParameter("RV",SqlDbType.Int);
retparam.Direction= ParameterDirection.ReturnValue;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
int returnValue = (int) retparam.Value;

-- bruce (sqlwork.com)
"david" <da***@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
I have trouble with it.

I have created a sproc in SQL Server, called SearchClass, which returns a
searched key from a table. I have tested this procedure in Analyzer and it
works fine. However, in the client side, when I use ExecuteNonQuery as
follows
---
Me.SqlConnection1.Open()
classKey = Me.SqlCommand2.ExecuteNonQuery()
Me.SqlConnection1.Close()
--
it always returns -1 rather than 5 that should be.
What is the problem?

Thank you for any help.

David


Oct 20 '06 #3
Hi, Bruce:

It does not work for me. I try either ExecuteNonQuery or ExecuteScalar, I
got 0 and -1 for ExecuteNonQuery, and 0 and 0 for ExecuteScalar. Now it
should be 11.

My Sproc code and VB.Net and output are in the following.

1. Stored procedure:
CREATE PROCEDURE SearchClassRecord2
(
@ModalityID int,
@PurposeID int,
@TechID int,
@SubtechID int,
@ContrastID int
)
AS
SET NOCOUNT ON

DECLARE @SearchedKey AS int

Select @SearchedKey = MAX(ClassKey)
FROM ClassDescription
WHERE ModalityID=@ModalityID and PurposeID=@PurposeID and TechID=TechID and
SubtechID=@SubtechID and ContrastAgentID=@ContrastID
RETURN @SearchedKey
GO

output:
Running dbo."SearchClassRecord2" ( @ModalityID = 1, @PurposeID = 1, @TechID
= 1, @SubtechID = 1, @ContrastID = 1 ).

No rows affected.
No more results.
(0 row(s) returned)
@RETURN_VALUE = 11
Finished running dbo."SearchClassRecord2".

2. VB.NET code:
Me.SqlCommand2.CommandText = "dbo.[SearchClassRecord2]"
Me.SqlCommand2.CommandType = System.Data.CommandType.StoredProcedure
Me.SqlCommand2.Connection = Me.SqlConnection1
Me.SqlCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RETURN_VALUE" ,
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
False, CType(0, Byte), CType(0, Byte), "",
System.Data.DataRowVersion.Current, Nothing))
Me.SqlCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@ModalityID", System.Data.SqlDbType.Int,
4))
Me.SqlCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@PurposeID", System.Data.SqlDbType.Int,
4))
Me.SqlCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@TechID", System.Data.SqlDbType.Int, 4))
Me.SqlCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@SubtechID", System.Data.SqlDbType.Int,
4))
Me.SqlCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@ContrastID", System.Data.SqlDbType.Int,
4))

Dim classKey As Integer
Dim psStatus As Integer

Me.SqlCommand2.Parameters("@ModalityID").Value =
ModalityDS1.Tables(0).Rows(ddlModality.SelectedInd ex)("ModalityKey")
'CStr(ddlModality.SelectedValue)
Me.SqlCommand2.Parameters("@PurposeID").Value =
PurposeDS1.Tables(0).Rows(ddlPurpose.SelectedIndex )("ID")
'ddlPurpose.SelectedValue
Me.SqlCommand2.Parameters("@TechID").Value =
TechDS1.Tables(0).Rows(ddlTech.SelectedIndex)("ID" ) 'ddlTech.SelectedValue
Me.SqlCommand2.Parameters("@SubtechID").Value =
SubtechDS1.Tables(0).Rows(ddlSubtech.SelectedIndex )("ID")
'ddlSubtech.SelectedValue
Me.SqlCommand2.Parameters("@ContrastID").Value =
ContrastDS1.Tables(0).Rows(ddlContrast.SelectedInd ex)("ID")
'ddlContrast.SelectedValue

Dim ClassKeyReturn As New
System.Data.SqlClient.SqlParameter("@RETURN_VALUE" , SqlDbType.Int)

ClassKeyReturn.Direction = ParameterDirection.ReturnValue
'test
lblInfo.Text &=
ModalityDS1.Tables(0).Rows(ddlModality.SelectedInd ex)("ModalityKey") & ", " &
PurposeDS1.Tables(0).Rows(ddlPurpose.SelectedIndex )("ID") & ", " &
TechDS1.Tables(0).Rows(ddlTech.SelectedIndex)("ID" ) & ", " &
SubtechDS1.Tables(0).Rows(ddlSubtech.SelectedIndex )("ID") & ", " &
ContrastDS1.Tables(0).Rows(ddlContrast.SelectedInd ex)("ID")

Me.SqlConnection1.Open()
'psStatus = Me.SqlCommand2.ExecuteNonQuery()
'classKey = CType(ClassKeyReturn.Value, Integer)
psStatus = CType(Me.SqlCommand2.ExecuteScalar(), Integer)
classKey = CType(ClassKeyReturn.Value, Integer)
Me.SqlConnection1.Close()
lblInfo.Text &= " classKey: " & classKey & " status: " & psStatus

output: 1, 1, 1, 1, 1 classKey: 0 status: 0
--------------------------
"bruce barker (sqlwork.com)" wrote:
ExecuteNonQuery does not return the return value of a sp, it returns rows
affected (addedd, deleted, changed). a query always returns -1

if you want a return values, you need to added a return value parameter:

SqlCommand cmd = new SqlCommand(spname,myConnection);
SqlParameter retparam = new SqlParameter("RV",SqlDbType.Int);
retparam.Direction= ParameterDirection.ReturnValue;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
int returnValue = (int) retparam.Value;

-- bruce (sqlwork.com)
"david" <da***@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
I have trouble with it.

I have created a sproc in SQL Server, called SearchClass, which returns a
searched key from a table. I have tested this procedure in Analyzer and it
works fine. However, in the client side, when I use ExecuteNonQuery as
follows
---
Me.SqlConnection1.Open()
classKey = Me.SqlCommand2.ExecuteNonQuery()
Me.SqlConnection1.Close()
--
it always returns -1 rather than 5 that should be.
What is the problem?

Thank you for any help.

David


Oct 20 '06 #4

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

Similar topics

1
by: Aston | last post by:
I have this stored procedure that takes a few parameters like date and merchant ID, and basically goes through a set of if-then statements to build a SQL SELECT string. When we upgraded from SQL...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
5
by: Ralph | last post by:
Hi all, I'm a newbie to MS-SQL UDFs and seem to have a real big problem. I need to implement a logic to receive an adress build out of various user definable fields from various user defined...
8
by: Mauricio | last post by:
Hello, Currently we have an ASP.NET 2003 app running, on one function the app calls to a stored procedure to SQLServerONE, that stored procedure creates some TEMP tables with the results of a...
3
by: Jack Black | last post by:
Help!! I'm trying to call a custom stored procedure from a VB.Net code-behind page in an ASP.Net application, and I keep getting an error with no real helpful info... Basically, I'm accepting a...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
3
by: ASP .NET Newbie | last post by:
I am trying to insert a new record into my database, and have it return a uniqueidentifier as the "newcatid". I don't use integers as my "id"'s, but rather uniqueidentifiers. Here's my Stored...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.