Solved

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

Posted on 2010-08-26
9
781 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now