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

PLEASE PLEASE HELP - How can I get a return value from a SQL Stored Proc is ASP.NET?

Hi. I'm sorry to bother all of you, but I have spent two days looking
at code samples all over the internet, and I can not get a single one
of them to work for me. I am simply trying to get a value returned to
the ASP from a stored procedure. The error I am getting is: Item can
not be found in the collection corresponding to the requested name or
ordinal.

Here is my Stored Procedure code.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
Go
ALTER PROCEDURE [dbo].[sprocRetUPC]
@sUPC varchar(50),
@sRetUPC varchar(50) OUTPUT

AS

BEGIN
SET NOCOUNT ON;
SET @sRetUPC = (SELECT bcdDVD_Title FROM tblBarcodes WHERE bcdUPC =
@sUPC)
RETURN @sRetUPC

END
Here is my ASP.NET code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim oConnSQL As ADODB.Connection

oConnSQL = New ADODB.Connection
oConnSQL.ConnectionString = "DSN=BarcodeSQL"
oConnSQL.Open()

Dim oSproc As ADODB.Command
oSproc = New ADODB.Command
oSproc.ActiveConnection = oConnSQL
oSproc.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
oSproc.CommandText = "sprocRetUPC"

Dim oParam1
Dim oParam2
oParam1 = oSproc.CreateParameter("sRetUPC",
ADODB.DataTypeEnum.adVarChar,
ADODB.ParameterDirectionEnum.adParamOutput, 50)
oParam2 = oSproc.CreateParameter("sUPC", ADODB.DataTypeEnum.adVarChar,
ADODB.ParameterDirectionEnum.adParamInput, 50, "043396005396")

Dim res
res = oSproc("sRetUPC")

Response.Write(res.ToString())

End Sub

If I put the line -
oSproc.Execute()

above the "Dim res" line, I end up with the following error:
Procedure or function 'sprocRetUPC' expects parameter '@sUPC', which
was not supplied. I thought that oParam2 was the parameter. I was also
under the assumption that the return parameter has to be declared
first. What am I doing wrong here?

Nov 30 '06 #1
8 3216
jbonifacejr wrote:
>
Hi. I'm sorry to bother all of you, but I have spent two days looking
at code samples all over the internet, and I can not get a single one
of them to work for me. I am simply trying to get a value returned to
the ASP from a stored procedure. The error I am getting is: Item can
not be found in the collection corresponding to the requested name or
ordinal.

Here is my Stored Procedure code.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
Go
ALTER PROCEDURE [dbo].[sprocRetUPC]
@sUPC varchar(50),
@sRetUPC varchar(50) OUTPUT

AS

BEGIN
SET NOCOUNT ON;
SET @sRetUPC = (SELECT bcdDVD_Title FROM tblBarcodes WHERE bcdUPC =
@sUPC)
RETURN @sRetUPC

END

Here is my ASP.NET code.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim oConnSQL As ADODB.Connection

oConnSQL = New ADODB.Connection
oConnSQL.ConnectionString = "DSN=BarcodeSQL"
oConnSQL.Open()

Dim oSproc As ADODB.Command
oSproc = New ADODB.Command
oSproc.ActiveConnection = oConnSQL
oSproc.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
oSproc.CommandText = "sprocRetUPC"

Dim oParam1
Dim oParam2
oParam1 = oSproc.CreateParameter("sRetUPC",
ADODB.DataTypeEnum.adVarChar,
ADODB.ParameterDirectionEnum.adParamOutput, 50)
oParam2 = oSproc.CreateParameter("sUPC", ADODB.DataTypeEnum.adVarChar,
ADODB.ParameterDirectionEnum.adParamInput, 50, "043396005396")

Dim res
res = oSproc("sRetUPC")

Response.Write(res.ToString())

End Sub

If I put the line -
oSproc.Execute()

above the "Dim res" line, I end up with the following error:
Procedure or function 'sprocRetUPC' expects parameter '@sUPC', which
was not supplied. I thought that oParam2 was the parameter. I was also
under the assumption that the return parameter has to be declared
first. What am I doing wrong here?
Just a few pointers here:
- creating a parameter will just create a parameter. To use it, you need
to add it to the command object using oSProc.Parameters.Append
- in a stored procedure you can only use the RETURN keyword to return an
integer, so @sRetUPC is out of the question
- if you want to use the value of the output parameter, then you should
access it through the Parameters collection of the Command object. The
syntax you are currently using refers to the resultset, but the stored
procedure does not have one

HTH,
Gert-Jan
Nov 30 '06 #2
Thank you for your help. Any chance you have a moment to help just a
little more? Here is what I did, but I still can't access the value
output by the stored proc...

I removed the Return @sRetUPC line. I am guessing that I can rely on
the set @sRetUPC line to set the value of the output parameter
>From there, I appended the parameters in the ASP code...like this
oSproc.Parameters.Append(oParam2)
oSproc.Parameters.Append(oParam1)
--originally I tried to do Param1 then Param2, but I got an error
about the parameter
--type being an output, so I just figured I had them in the wrong
order because the
--first parameter in the code was the output one.

Then, I added the line oSproc.Execute()
After that is:
Dim res
res = oSproc.Parameters.Item("sRetUPC").Value.toString()

This is not working. Do you know how I can get access to the value of
the parameter that is returned?

Just a few pointers here:
- creating a parameter will just create a parameter. To use it, you need
to add it to the command object using oSProc.Parameters.Append
- in a stored procedure you can only use the RETURN keyword to return an
integer, so @sRetUPC is out of the question
- if you want to use the value of the output parameter, then you should
access it through the Parameters collection of the Command object. The
syntax you are currently using refers to the resultset, but the stored
procedure does not have one

HTH,
Gert-Jan
Nov 30 '06 #3
jbonifacejr (jb*********@hotmail.com) writes:
Thank you for your help. Any chance you have a moment to help just a
little more? Here is what I did, but I still can't access the value
output by the stored proc...

I removed the Return @sRetUPC line. I am guessing that I can rely on
the set @sRetUPC line to set the value of the output parameter
>>From there, I appended the parameters in the ASP code...like this
oSproc.Parameters.Append(oParam2)
oSproc.Parameters.Append(oParam1)
--originally I tried to do Param1 then Param2, but I got an error
about the parameter
--type being an output, so I just figured I had them in the wrong
order because the
--first parameter in the code was the output one.

Then, I added the line oSproc.Execute()
After that is:
Dim res
res = oSproc.Parameters.Item("sRetUPC").Value.toString()

This is not working. Do you know how I can get access to the value of
the parameter that is returned?
Never say "not working" in newsgroup post with explaining what it
means. Do you get an unexpected result? An error message? Something
else?

Since I don't even know how your code looks like right now, just two
notes:

1) Use parameter names with leading @. The underlying provider may
prefer that.

2) Use adParamInputOutput for the output value. T-SQL does not have any
true output-only parameters. (Save the return value, but there is a
separate enum value for return values as I recall.)
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 30 '06 #4
If you look at the top post you will see where I put the code I am
using. I also tried to let people know what happened when I tried their
suggestions. But, thanks for the advice...and I'll look at those SQL
Books online.

Jan

Erland Sommarskog wrote:
jbonifacejr (jb*********@hotmail.com) writes:
Thank you for your help. Any chance you have a moment to help just a
little more? Here is what I did, but I still can't access the value
output by the stored proc...

I removed the Return @sRetUPC line. I am guessing that I can rely on
the set @sRetUPC line to set the value of the output parameter
>From there, I appended the parameters in the ASP code...like this
oSproc.Parameters.Append(oParam2)
oSproc.Parameters.Append(oParam1)
--originally I tried to do Param1 then Param2, but I got an error
about the parameter
--type being an output, so I just figured I had them in the wrong
order because the
--first parameter in the code was the output one.

Then, I added the line oSproc.Execute()
After that is:
Dim res
res = oSproc.Parameters.Item("sRetUPC").Value.toString()

This is not working. Do you know how I can get access to the value of
the parameter that is returned?

Never say "not working" in newsgroup post with explaining what it
means. Do you get an unexpected result? An error message? Something
else?

Since I don't even know how your code looks like right now, just two
notes:

1) Use parameter names with leading @. The underlying provider may
prefer that.

2) Use adParamInputOutput for the output value. T-SQL does not have any
true output-only parameters. (Save the return value, but there is a
separate enum value for return values as I recall.)
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 30 '06 #5
Why are you using stored proc when you can use a function(s)? I always
thought the output parameter was a bit of a hack and clumsy to use.

jbonifacejr wrote:
If you look at the top post you will see where I put the code I am
using. I also tried to let people know what happened when I tried their
suggestions. But, thanks for the advice...and I'll look at those SQL
Books online.

Jan

Erland Sommarskog wrote:
jbonifacejr (jb*********@hotmail.com) writes:
Thank you for your help. Any chance you have a moment to help just a
little more? Here is what I did, but I still can't access the value
output by the stored proc...
>
I removed the Return @sRetUPC line. I am guessing that I can rely on
the set @sRetUPC line to set the value of the output parameter
>
>>From there, I appended the parameters in the ASP code...like this
oSproc.Parameters.Append(oParam2)
oSproc.Parameters.Append(oParam1)
--originally I tried to do Param1 then Param2, but I got an error
about the parameter
--type being an output, so I just figured I had them in the wrong
order because the
--first parameter in the code was the output one.
>
Then, I added the line oSproc.Execute()
After that is:
Dim res
res = oSproc.Parameters.Item("sRetUPC").Value.toString()
>
This is not working. Do you know how I can get access to the value of
the parameter that is returned?
Never say "not working" in newsgroup post with explaining what it
means. Do you get an unexpected result? An error message? Something
else?

Since I don't even know how your code looks like right now, just two
notes:

1) Use parameter names with leading @. The underlying provider may
prefer that.

2) Use adParamInputOutput for the output value. T-SQL does not have any
true output-only parameters. (Save the return value, but there is a
separate enum value for return values as I recall.)
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 1 '06 #6
jbonifacejr (jb*********@hotmail.com) writes:
If you look at the top post you will see where I put the code I am
using.
Since then you changed the code according to Gert-Jan's advice, and we
don't know what it looked after that.

Basically, if you only say "not working" without specifying why, and
don't show us the code, don't expect that much help. But that's your call.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 1 '06 #7
Thanks Erland...I see where I screwed up. I thought I had explained
what was wrong, but I did that in a different thread in another forum.

Anyway, I got this working using classic ASP and ADODB. Now I am going
to try to get it working over ASP.NET and ADO.NET. Wish me luck.

So far, eveerything works except that I am constantly being told that
the stored procedure expects a parameter that was not supplied.
However, The same two parameters are created and added to the
Parameters of the command object.

I'll continue to work on it and see if I can get it to work. Looks like
I need a datareader or some other object. I found a great KB article
that basically shows me everythig I am doing (right and wrong)...

http://support.microsoft.com/kb/306574
Erland Sommarskog wrote:
jbonifacejr (jb*********@hotmail.com) writes:
If you look at the top post you will see where I put the code I am
using.

Since then you changed the code according to Gert-Jan's advice, and we
don't know what it looked after that.

Basically, if you only say "not working" without specifying why, and
don't show us the code, don't expect that much help. But that's your call.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 1 '06 #8
jbonifacejr (jb*********@hotmail.com) writes:
Thanks Erland...I see where I screwed up. I thought I had explained
what was wrong, but I did that in a different thread in another forum.
Posting the same question independently to two forums is not a nice
thing to. This means that people can waste time on answering your post,
when it has already been answered elsewhere.
Anyway, I got this working using classic ASP and ADODB. Now I am going
to try to get it working over ASP.NET and ADO.NET. Wish me luck.

So far, eveerything works except that I am constantly being told that
the stored procedure expects a parameter that was not supplied.
However, The same two parameters are created and added to the
Parameters of the command object.
Again, without seeing your code it's hard to tell. There is a difference
between ADO and SqlClient though: with ADO, the parameter names are
just local to the application, so if you misspell a parameter name,
you may get away with it. Not so with SqlClient.
I'll continue to work on it and see if I can get it to work. Looks like
I need a datareader or some other object.
Since your procedure has an output parameter, but no result set, the most
conventient method to use is ExecuteNonQuery, in which case you only need
the Command object.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 2 '06 #9

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

Similar topics

2
by: Daniel | last post by:
hi ng, i am newbie to sqlserver and my problem seems simple, but i didn't find information about it: How can i display the RETURN @x value of a stored procedure in the sql analyzer of the...
5
by: adolf garlic | last post by:
Im trying to return xml from sql. The xml is made up of different fragments, some using FOR XML ... syntax. The result is a valid xml doc. There is a working stored proc that returns the xml In...
4
by: Learner | last post by:
Hi there, I have a storec proc that schedules a Sql job and finally it returns 0 then it was successfull and if it returns 1 then its unsuccessful. Now when i run the stored proc in the query...
2
by: philip | last post by:
hello, i am new to asp.net and sql server, and i have 3 questions for asking: 1. i am writing a store procedure of login validation for my asp.net application and wondering what the different...
0
by: balaji krishna | last post by:
Hi, I need to handle the return set from COBOL stored procedure from my invoking Java program. I do not know, how many rows the stored proc SQL fetches.I have declared the cursor in that proc, but i...
1
by: Eric Effer | last post by:
Hi I am a newbie with vb.net. I am working with vb.net 2.0 and sql server 2005. I am trying to get the return value from my insert stored proc.Does anyone know how to do this? thanks E
2
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...
4
by: Mick Walker | last post by:
Hi Everyone, I am stumped here. I have the following stored proceedure:P CREATE PROCEDURE . @SupplierSKU varchar(50), @RetVal int AS Select @Retval = count(*) from dbo.ImportLines Where =...
8
by: colmkav | last post by:
Can someone tell me how I can access the return value of a function called from Oracle as opposed to a store proc from oracle? my oracle function is get_num_dates_varposfile. I am only used to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.