Solved

Passing SQL stored procedure output parameter to ASP.net (VB)

Posted on 2010-08-26
9
797 Views
Last Modified: 2013-12-25
I am working with SQL output parameters for the first time, and I'm running into some difficulty.  It's in a stored procedure for an ASP.net (VB) website.

Here is the error message I'm getting at Line 15:

"System.NullReferenceException was unhandled by user code"
Message="Object reference not set to an instance of an object."

If someone is able to review my code and see if they can spot my error, it would be greatly appreciated.




   
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim conSQL As New SqlConnection(strSQLConnection)
        conSQL.Open()

        Dim cmdSQL As New SqlCommand("sp_inventory", conSQL)
        cmdSQL.CommandType = CommandType.StoredProcedure

        Dim paramResult As New SqlParameter
        paramResult.ParameterName = "@TotalUnit1"
        paramResult.Direction = ParameterDirection.Output
        cmdSQL.Parameters.Add(paramResult)


       
        Literal1.Text = cmdSQL.Parameters("@TotalUnit1").Value.ToString


        cmdSQL.ExecuteNonQuery()

        conSQL.Close()
    End Sub

Open in new window

Alter PROCEDURE [dbo].[sp_inventory]


@TotalUnit1 int OUTPUT

AS

Begin

Select @TotalUnit1 = SUM(Quantity)
From Table1
Where (Condition1 = '1') AND (Condition2 = '2') 
	
END

Open in new window

0
Comment
Question by:Jorhal
  • 5
  • 2
  • 2
9 Comments
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33538844
replace

cmdSQL.ExecuteNonQuery()
Literal1.Text = cmdSQL.Parameters("@TotalUnit1").Value.ToString

with

cmdSQL.ExecuteNonQuery()
Literal1.Text = cmdSQL.Parameters("@TotalUnit1").Value.ToString

please check the following example as well...
http://www.4guysfromrolla.com/articles/062905-1.aspx
0
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33538851
i mean call cmdSQL.ExecuteNonQuery() first and then retreive the value..
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33538863



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim conSQL As New SqlConnection(strSQLConnection)
        conSQL.Open()

        Dim cmdSQL As New SqlCommand("sp_inventory", conSQL)
        cmdSQL.CommandType = CommandType.StoredProcedure

        Dim paramResult As New SqlParameter
        paramResult.ParameterName = "@TotalUnit1"
        paramResult.Direction = ParameterDirection.Output
        cmdSQL.Parameters.Add(paramResult)

cmdSQL.ExecuteNonQuery()
       
        Literal1.Text = paramResult.Value.ToString


                conSQL.Close()

conSQL.dispose()
    End Sub

Open in new window

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33538881

Alter PROCEDURE [dbo].[sp_inventory]


@TotalUnit1 int OUTPUT

AS

Begin

Select @TotalUnit1 = SUM(Quantity)
From Table1
Where (Condition1 = '1') AND (Condition2 = '2')

Return  @TotalUnit1
	
END

Open in new window

0
 

Author Comment

by:Jorhal
ID: 33538990
Thanks for the responses.  Made the change to call cmdSQL.ExecuteNonQuery() first.  However, cmdSQL.ExecuteNonQuery() now gets the following error:

[InvalidOperationException: String[0]: the Size property has an invalid size of 0.]
 
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33539006
Dim paramResult As New SqlParameter
        paramResult.ParameterName = "@TotalUnit1"
        paramResult.Direction = ParameterDirection.Output
        cmdSQL.Parameters.Add(paramResult)


define the type of output parameter, by default it is taking it as varchar, that need size to be set, you have to define it as int, same as stored procedure that you are using.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33539016
paramResult.DbType = DbType.Int32
0
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 500 total points
ID: 33539067
replace with
Dim paramResult As New SqlParameter
        paramResult.ParameterName = "@TotalUnit1"
paramResult.DbType = DbType.Int32
        paramResult.Direction = ParameterDirection.Output
        cmdSQL.Parameters.Add(paramResult)

Open in new window

0
 

Author Closing Comment

by:Jorhal
ID: 33539142
Works perfectly -- Thanks!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question