Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-26
9
Medium Priority
?
816 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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