Solved

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

Posted on 2010-08-26
9
793 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Optimization 14 42
Refresh Dev server with Production database 8 27
SQL Server 2012 r2 - Query results have 2 seperate results instead of 1 2 21
sql server insert 12 28
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

815 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

9 Experts available now in Live!

Get 1:1 Help Now