473,386 Members | 1,779 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,386 software developers and data experts.

Getting Return Value From Stored Proceedure.

Hi Everyone,

I am stumped here. I have the following stored proceedure:P

CREATE PROCEDURE [dbo].[ImportLinesProductExists]
@SupplierSKU varchar(50),
@RetVal int
AS
Select @Retval = count(*) from dbo.ImportLines
Where [SupplierSKUCode] = @SupplierSKU
if @Retval 0
BEGIN
Return 0
END
-- It wasn't found so we can now return -1
Return -1

Which works perfectly when I execute it with SQL Server Management Studio.

I call the stored proceedure with the following code:

Public Sub CheckProduct(ByVal _ConnString As String, ByVal
supplierSKUCode as Integer)
Dim ReturnValue As Integer = 0 ' Our Return Value
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
conn.ConnectionString = _ConnString
cmd.Connection = conn
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = "dbo.ImportLinesProductExists"

With cmd.Parameters
.AddWithValue("@SupplierSKU", supplierSKUCode)
.AddWithValue("@RetVal", System.DBNull.Value)
End With
Try
conn.Open()
ReturnValue = cmd.ExecuteScalar()
Catch ex As SqlException
Throw ex
Finally
conn.Close()
cmd.Parameters.Clear()
End Try
If ReturnValue = 0 Then
InsertTempProducts(_ConnString, Item)
End If
End Sub

But no matter what, the sub always returns 0, even when I manually add a
entry which should conflict, into the database.

Does anyone know whay this is happening?

Kind Regards
Mick Walker
Jun 20 '07 #1
4 2205
You have to set the parameter direction to ParameterDirection.ReturnValue
explicitly for the return parameter..

"Mick Walker" wrote:
Hi Everyone,

I am stumped here. I have the following stored proceedure:P

CREATE PROCEDURE [dbo].[ImportLinesProductExists]
@SupplierSKU varchar(50),
@RetVal int
AS
Select @Retval = count(*) from dbo.ImportLines
Where [SupplierSKUCode] = @SupplierSKU
if @Retval 0
BEGIN
Return 0
END
-- It wasn't found so we can now return -1
Return -1

Which works perfectly when I execute it with SQL Server Management Studio.

I call the stored proceedure with the following code:

Public Sub CheckProduct(ByVal _ConnString As String, ByVal
supplierSKUCode as Integer)
Dim ReturnValue As Integer = 0 ' Our Return Value
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
conn.ConnectionString = _ConnString
cmd.Connection = conn
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = "dbo.ImportLinesProductExists"

With cmd.Parameters
.AddWithValue("@SupplierSKU", supplierSKUCode)
.AddWithValue("@RetVal", System.DBNull.Value)
End With
Try
conn.Open()
ReturnValue = cmd.ExecuteScalar()
Catch ex As SqlException
Throw ex
Finally
conn.Close()
cmd.Parameters.Clear()
End Try
If ReturnValue = 0 Then
InsertTempProducts(_ConnString, Item)
End If
End Sub

But no matter what, the sub always returns 0, even when I manually add a
entry which should conflict, into the database.

Does anyone know whay this is happening?

Kind Regards
Mick Walker
Jun 20 '07 #2
Hi Everyone,
>
I am stumped here. I have the following stored proceedure:P

CREATE PROCEDURE [dbo].[ImportLinesProductExists]
@SupplierSKU varchar(50),
@RetVal int
AS
Select @Retval = count(*) from dbo.ImportLines
Where [SupplierSKUCode] = @SupplierSKU
if @Retval 0
BEGIN
Return 0
END
Which works perfectly when I execute it with SQL Server Management
Studio.

I call the stored proceedure with the following code:

Public Sub CheckProduct(ByVal _ConnString As String, ByVal
supplierSKUCode as Integer)
Dim ReturnValue As Integer = 0 ' Our Return Value
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
conn.ConnectionString = _ConnString
cmd.Connection = conn
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = "dbo.ImportLinesProductExists"
With cmd.Parameters
.AddWithValue("@SupplierSKU", supplierSKUCode)
.AddWithValue("@RetVal", System.DBNull.Value)
as mentioned before, you need to set the Direction to ParameterDirection.ReturnValue
End With
Try
conn.Open()
ReturnValue = cmd.ExecuteScalar()
you don't need ExecuteScalar here: that will return the first value in the
first column
in the first table returned, and you don't return any tables at all.
An ExecuteNonQuery should do.
Catch ex As SqlException
Throw ex
Finally
conn.Close()
cmd.Parameters.Clear()
before you clear the parameters, read the value of @RetVal - this is the
value you want.
End Try
If ReturnValue = 0 Then
InsertTempProducts(_ConnString, Item)
End If
End Sub
But no matter what, the sub always returns 0, even when I manually add
a entry which should conflict, into the database.

Does anyone know whay this is happening?

Kind Regards
Mick Walker

Jun 20 '07 #3
you are confused.

executescaler returns the first column value of the first row of the
first resultset. your proc does not return a resultset, so it should be
throwing an error. you should be calling ExecuteNonquery().

after processing the resultsets (or call ExecuteNonquery which does
this) you can access output parameters (though you need to set the
parameter direction to output before making the query).

to get the actual return value of a proc, you add an int parameter with
the direction of ReturnValue. after all resultsets have been returned,
you can accesses this parameter's value to get the return value.
-- bruce (sqlwork.com)

Mick Walker wrote:
Hi Everyone,

I am stumped here. I have the following stored proceedure:P

CREATE PROCEDURE [dbo].[ImportLinesProductExists]
@SupplierSKU varchar(50),
@RetVal int
AS
Select @Retval = count(*) from dbo.ImportLines
Where [SupplierSKUCode] = @SupplierSKU
if @Retval 0
BEGIN
Return 0
END
-- It wasn't found so we can now return -1
Return -1

Which works perfectly when I execute it with SQL Server Management Studio.

I call the stored proceedure with the following code:

Public Sub CheckProduct(ByVal _ConnString As String, ByVal
supplierSKUCode as Integer)
Dim ReturnValue As Integer = 0 ' Our Return Value
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
conn.ConnectionString = _ConnString
cmd.Connection = conn
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = "dbo.ImportLinesProductExists"

With cmd.Parameters
.AddWithValue("@SupplierSKU", supplierSKUCode)
.AddWithValue("@RetVal", System.DBNull.Value)
End With
Try
conn.Open()
ReturnValue = cmd.ExecuteScalar()
Catch ex As SqlException
Throw ex
Finally
conn.Close()
cmd.Parameters.Clear()
End Try
If ReturnValue = 0 Then
InsertTempProducts(_ConnString, Item)
End If
End Sub

But no matter what, the sub always returns 0, even when I manually add a
entry which should conflict, into the database.

Does anyone know whay this is happening?

Kind Regards
Mick Walker
Jun 20 '07 #4

"Mick Walker" <Mi*********@privacy.netwrote in message
news:5d*************@mid.individual.net...
Hi Everyone,

I am stumped here. I have the following stored proceedure:P

CREATE PROCEDURE [dbo].[ImportLinesProductExists]
@SupplierSKU varchar(50),
@RetVal int
AS
Select @Retval = count(*) from dbo.ImportLines
Where [SupplierSKUCode] = @SupplierSKU
if @Retval 0
BEGIN
Return 0
END
-- It wasn't found so we can now return -1
Return -1

Which works perfectly when I execute it with SQL Server Management Studio.

I call the stored proceedure with the following code:

Public Sub CheckProduct(ByVal _ConnString As String, ByVal
supplierSKUCode as Integer)
Dim ReturnValue As Integer = 0 ' Our Return Value
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
conn.ConnectionString = _ConnString
cmd.Connection = conn
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = "dbo.ImportLinesProductExists"

With cmd.Parameters
.AddWithValue("@SupplierSKU", supplierSKUCode)
.AddWithValue("@RetVal", System.DBNull.Value)
End With
Try
conn.Open()
ReturnValue = cmd.ExecuteScalar()
Catch ex As SqlException
Throw ex
Finally
conn.Close()
cmd.Parameters.Clear()
End Try
If ReturnValue = 0 Then
InsertTempProducts(_ConnString, Item)
End If
End Sub

But no matter what, the sub always returns 0, even when I manually add a
entry which should conflict, into the database.

Does anyone know whay this is happening?

Kind Regards
Mick Walker
This looks like a function rather than a procedure. Make this a Stored
Function and return the count as the return value for the function.

LS

Jun 20 '07 #5

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

Similar topics

6
by: John Webb | last post by:
Hope someone can help. I am trying to write a stored proceedure to display sales activity by month and then sum all the columbs. The problem is that our sales year starts in April and end in...
1
by: Melissa Meyer via SQLMonster.com | last post by:
How do you set a stored proceedure for automatic execution? -- Message posted via http://www.sqlmonster.com
1
by: John Michael | last post by:
I have a form that has a subform that has a subform. The subform loads a record based on a combo lookup box in the main form. I'm trying to set a value in the subform based on a value in a...
4
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...
2
by: Martin Raychev | last post by:
Hi all, I have the following problem: I have a private method that returns a SqlDataReader. For this to work I have not to close the DB connection in the above method. I do this only to
6
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",...
9
by: Problematic coder | last post by:
The intention is to call a stored proceedure which sets flags in the database, this requires no parameters and the page does not need an output from the stored proceedure, though is this is...
1
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I can successfully insert and update the oracle database by calling a oracles stored proc from my .net code. This oracle stored proc is returning some value. I cannot see that...
8
by: jthep | last post by:
Is there a way to create a stored procedure where more than one type of parameter can be entered? For example, a user can enter a int type, char(4) type, or nothing in the same stored proceedure.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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:
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
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...

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.