Solved

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

Posted on 2010-08-26
9
810 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

617 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