MS SQL Server
  Home arrow MS SQL Server arrow Page 3 - Working with NULL, OUTPUT and RETURN in AD...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

Working with NULL, OUTPUT and RETURN in ADO.NET
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2006-04-18

    Table of Contents:
  • Working with NULL, OUTPUT and RETURN in ADO.NET
  • How to work with an OUTPUT parameter of a stored procedure using ADO.NET from ASP.NET: stored procedure
  • How to work with an OUTPUT parameter of a stored procedure using ADO.NET from ASP.NET: the code
  • How to retrieve the value returned by a stored procedure using ADO.NET from ASP.NET: the stored procedure

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with NULL, OUTPUT and RETURN in ADO.NET - How to work with an OUTPUT parameter of a stored procedure using ADO.NET from ASP.NET: the code


    (Page 3 of 4 )

     

    Now we need to call the stored procedure discussed in the previous section.  Add a new web form (call it “outputParamDemo”) to an already existing project (as specified in my previous article).  Add a button (named “Execute Retrieve OUTPUT parameter”) and a label (named “lblName”).

    After designing the form, switch to the code and add the following line at the top.

    Imports System.Data.SqlClient

    Add the following code to your “Execute Retrieve OUTPUT parameter” button:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles Button1.Click
            Dim name As String
            Dim cn As New SqlConnection("Data Source=.;initial
    catalog=Northwind;user id=sa")
            Dim cmd As New SqlCommand
            With cmd
                .CommandType = CommandType.StoredProcedure
                .CommandText = "sp_emp_getEname"
                Dim paramName As New SqlParameter
                With paramName
                    .ParameterName = "@name"
                    .SqlDbType = SqlDbType.VarChar
                    .Direction = ParameterDirection.Output
                    .Size = 20
                End With
                .Parameters.Add("@empno", 1002)
                .Parameters.Add(paramName)
                .Connection = cn
                .Connection.Open()
                .ExecuteNonQuery()
                name = paramName.Value
                'release resources
                .Connection.Close()
                .Dispose()
            End With
            Me.lblName.Text = name
        End Sub

    Set the start page, execute your application (by pressing F5) and click on the button “Execute Retrieve OUTPUT parameter.”  Once it executes successfully, you should be able to see the value sent by the stored procedure to your ASP.NET web form.

    The most important issue from the above code is that you need to specify “ParameterDirection.Output” as part of the “direction” property.  What happens if the stored procedure cannot find any row?  It places NULL within the parameter “@name” and sends it back.  So, it is our task to check it out in our program.

    The code can be further simplified (without creating an explicit parameter object) as follows:

                With cmd
                .CommandType = CommandType.StoredProcedure
                .CommandText = "sp_emp_getEname"
                .Parameters.Add("@empno", 1002)
                .Parameters.Add("@name", SqlDbType.VarChar, 20)
                .Parameters("@name").Direction =
    ParameterDirection.Output
                .Connection = cn
                .Connection.Open()
                .ExecuteNonQuery()
                name = .Parameters("@name").Value
                'release resources
                .Connection.Close()
                .Dispose()
            End With

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hello guys. This is second in series focusing on "Working with Stored Procedures...
       · i really liked article on working with out parameter of stored procedure....this...
     

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT