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

Problem Receiving Output Param Value...

Hello,

I'm still new to the whole .Net thing and I'm having a problem with
something that should be so simple -- executing a query and returning
an output parameter. It's a standard "Add" stored procedure, where I
add a user to the database. I insert the record, and return the user's
ID.

I can run it just fine and it does execute the code because the user is
being inserted successfully. But I can't receive any output values.
I've since pared it down to a very simple stored procedure that simply
takes one value and it's an output Integer. I hardcore the value.
This is the stored procedure:

-----------------------------------
ALTER PROCEDURE dbo.User_Test

(
@myOutput INT = NULL OUTPUT
)

AS

SET NOCOUNT ON
SELECT @myOutput = 8
SET NOCOUNT OFF
-----------------------------------
And here is the code being run on the web form:

-----------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Trace.IsEnabled = True

Dim objConn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
Dim objComm As SqlCommand = New SqlCommand()
objConn.Open()
objComm.CommandType = CommandType.StoredProcedure
objComm.CommandText = "dbo.User_Test"
Dim objParam As SqlParameter
objParam = New SqlParameter("@myOutput", SqlDbType.Int, 4,
ParameterDirection.Output)
objComm.Parameters.Add(objParam)
Try
objComm.Connection = objConn
objComm.ExecuteNonQuery()

Response.Write("=================<br>")
Trace.Warn("Test")
Trace.Warn(objParam.Value)
Trace.Warn(objComm.Parameters("myOutput").Value)
Response.Write("***<br>")
Response.Write("=================<br>")

objConn.Close()
Catch
Response.Write(Err.Description)
End Try

End Sub
-----------------------------------

You can see I tried Warn'ing the parameter value in 2 different ways.
Nothing ever gets returned.

Can anyone spot what I'm doing wrong?

Thanks,

Chris T

Apr 25 '06 #1
4 1658
It all looks correct to me except for one line:
You need to set the value for the parameter.

Instead of:
objParam = New SqlParameter("@myOutput", SqlDbType.Int, 4,
ParameterDirection.Output)
objComm.Parameters.Add(objParam)

Do this:

objParam = New SqlParameter("@myOutput", SqlDbType.Int, 4,
ParameterDirection.Output)
objParam.Value = yourVariable
objComm.Parameters.Add(objParam)

"Tifer" wrote:
Hello,

I'm still new to the whole .Net thing and I'm having a problem with
something that should be so simple -- executing a query and returning
an output parameter. It's a standard "Add" stored procedure, where I
add a user to the database. I insert the record, and return the user's
ID.

I can run it just fine and it does execute the code because the user is
being inserted successfully. But I can't receive any output values.
I've since pared it down to a very simple stored procedure that simply
takes one value and it's an output Integer. I hardcore the value.
This is the stored procedure:

-----------------------------------
ALTER PROCEDURE dbo.User_Test

(
@myOutput INT = NULL OUTPUT
)

AS

SET NOCOUNT ON
SELECT @myOutput = 8
SET NOCOUNT OFF
-----------------------------------
And here is the code being run on the web form:

-----------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Trace.IsEnabled = True

Dim objConn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
Dim objComm As SqlCommand = New SqlCommand()
objConn.Open()
objComm.CommandType = CommandType.StoredProcedure
objComm.CommandText = "dbo.User_Test"
Dim objParam As SqlParameter
objParam = New SqlParameter("@myOutput", SqlDbType.Int, 4,
ParameterDirection.Output)
objComm.Parameters.Add(objParam)
Try
objComm.Connection = objConn
objComm.ExecuteNonQuery()

Response.Write("=================<br>")
Trace.Warn("Test")
Trace.Warn(objParam.Value)
Trace.Warn(objComm.Parameters("myOutput").Value)
Response.Write("***<br>")
Response.Write("=================<br>")

objConn.Close()
Catch
Response.Write(Err.Description)
End Try

End Sub
-----------------------------------

You can see I tried Warn'ing the parameter value in 2 different ways.
Nothing ever gets returned.

Can anyone spot what I'm doing wrong?

Thanks,

Chris T

Apr 25 '06 #2
Woops.. never mind. I didn't see that you set the output param to 8 in the
sproc.
"Chris Mohan" wrote:
It all looks correct to me except for one line:
You need to set the value for the parameter.

Instead of:
objParam = New SqlParameter("@myOutput", SqlDbType.Int, 4,
ParameterDirection.Output)
objComm.Parameters.Add(objParam)

Do this:

objParam = New SqlParameter("@myOutput", SqlDbType.Int, 4,
ParameterDirection.Output)
objParam.Value = yourVariable
objComm.Parameters.Add(objParam)

"Tifer" wrote:
Hello,

I'm still new to the whole .Net thing and I'm having a problem with
something that should be so simple -- executing a query and returning
an output parameter. It's a standard "Add" stored procedure, where I
add a user to the database. I insert the record, and return the user's
ID.

I can run it just fine and it does execute the code because the user is
being inserted successfully. But I can't receive any output values.
I've since pared it down to a very simple stored procedure that simply
takes one value and it's an output Integer. I hardcore the value.
This is the stored procedure:

----------------------------------- -----------------------------------
And here is the code being run on the web form:

-----------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Trace.IsEnabled = True

Dim objConn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
Dim objComm As SqlCommand = New SqlCommand()
objConn.Open()
objComm.CommandType = CommandType.StoredProcedure
objComm.CommandText = "dbo.User_Test"
Dim objParam As SqlParameter
objParam = New SqlParameter("@myOutput", SqlDbType.Int, 4,
ParameterDirection.Output)
objComm.Parameters.Add(objParam)
Try
objComm.Connection = objConn
objComm.ExecuteNonQuery()

Response.Write("=================<br>")
Trace.Warn("Test")
Trace.Warn(objParam.Value)
Trace.Warn(objComm.Parameters("myOutput").Value)
Response.Write("***<br>")
Response.Write("=================<br>")

objConn.Close()
Catch
Response.Write(Err.Description)
End Try

End Sub
-----------------------------------

You can see I tried Warn'ing the parameter value in 2 different ways.
Nothing ever gets returned.

Can anyone spot what I'm doing wrong?

Thanks,

Chris T

Apr 25 '06 #3

Chris Mohan wrote:
Woops.. never mind. I didn't see that you set the output param to 8 in the
sproc.


Correct, and before I posted this message, I have also tried passing in
a NULL value:
----------------------------------------
objParam = New SqlParameter("@myOutput", SqlDbType.Int, 4,
ParameterDirection.Output)
objParam.Value = System.DBNull.Value
----------------------------------------

But that didn't produce anything helpful.

This seems like such an easy problem, yet it's holding me up. Any time
I want to mess around with .Net, I always run into one of these and it
keeps me from wanting to continue.

I'm still holding out hope that someone here has an answer for me.

Apr 25 '06 #4
Just a quick update. A buddy helped me solve the problem, but we're
still stumped as to why my code didn't work. The ONLY thing I changed
was the below declaration of a parameter that I then added to the
collection:
----------------------------------------
Dim objParam As SqlParameter
objParam = New SqlParameter("@myOutput", SqlDbType.Int, 4,
ParameterDirection.Output)
objComm.Parameters.Add(objParam)

To this:
----------------------------------------
objComm.Parameters.Add("@myOutput", SqlDbType.Int, 4)
objComm.Parameters("@myOutput").Direction = ParameterDirection.Output

Now I receive my output param just fine. So I gues the big question
now is: Why the HELL does it have to be done this way? I was following
examples online which led me to my initial code. Is there something
wrong in my original code that I'm just not seeing?

Thanks in advance.

Apr 25 '06 #5

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

Similar topics

12
by: Patrick Russell | last post by:
I am trying to figure out why the code listed below works in php v4.3.0 but not in v4.3.10: $string = "Param1=Val1;Param2=Val2;Param3=Val3"; $exploded_string = explode(";",$string);...
0
by: johkar | last post by:
My XML and XSL is below. Also below is a textual representation of what I want to get out of the XML with XSL. For each Extension node in XML, I am only concerned with those nodes with...
5
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the...
6
by: Stephen Cook | last post by:
Having worked through the problems around enabling the document function using an XmlUrlResolver I started work on building a useful class to hide the intricacies. Trying to generalise the process...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
5
by: Lasse Edsvik | last post by:
Hello I have this: public SqlParameter GetParameter(string sqlparam) { SqlParameter param = cmd.Parameters.Add(sqlparam);
2
by: Theodoros.Savvides | last post by:
Hi All, We are developing an application and we have a problem for which I cannot find any information anywhere. The application consists of a windows user control and an ASP.NET application...
0
by: michael | last post by:
Hi. I have a problem using the CollectioEditor. In my custom control I have a public property that returns ItemList. ItemList is inherited from CollectionBase and contains very simple items...
2
by: QTR | last post by:
Hello, I discovered a strange XSLT behaviour on Weblo 8.1. Let me explain it (see at the end my example XSLT): I call a template (1) with one parameter (A) which calls another template (2) with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: 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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.