MS SQL Server
  Home arrow MS SQL Server arrow Page 4 - 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 retrieve the value returned by a stored procedure using ADO.NET from ASP.NET: the stored procedure


    (Page 4 of 4 )

    Every stored procedure returns an integer value back to the calling environment (or ADO.NET in this scenario).  This happens even if you don’t use a RETURN statement.  By default it returns zero (if no RETURN is used).

    We can use the RETURN statement to return integer values according to our requirements.  In this demonstration, I would like to return a value showing whether an employee exists or not using a RETURN statement.  Let us see how it works.

    Before going to the ADO.NET code, we need to create a simple stored procedure in SQL Server which returns a value.  Using “Query Analyzer,” execute the following script in “Northwind” database.

    CREATE PROCEDURE dbo.sp_emp_isEmployeeExist
          (
                @empno int
          )
    AS
          IF EXISTS (SELECT ename FROM emp WHERE empno =@empno)
                      RETURN 1
          ELSE
                      RETURN 0     

    According to the above stored procedure, you can observe that I am sending a value 1 if the employee is found or 0 if no employee is found.  It simply accepts “Empno” (“@empno”) as a parameter (which needs to be searched).  The stored procedure is named “sp_Emp_isEmployeeExists.”  Now, we need to go to ADO.NET to access the same in ASP.NET.  The next section accomplishes this.

    How to retrieve the value returned by a stored procedure using ADO.NET from ASP.NET: the code

    We need to call the stored procedure defined in the previous section.  Add a new web form (call it “ReturnValueDemo”) to an already existing project (as specified in my previous article).  Add a button named “Receive RETURN value.”

    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 “Receive RETURN value” 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_isEmployeeExist"
                Dim paramName As New SqlParameter
                With paramName
                    .ParameterName = "@ReturnValue"
                    .SqlDbType = SqlDbType.Int
                    .Direction = ParameterDirection.ReturnValue
                End With
                .Parameters.Add("@empno", 1201)
                .Parameters.Add(paramName)
                .Connection = cn
                .Connection.Open()
                .ExecuteNonQuery()
                name = IIf(paramName.Value = 0, "Not found", "found")
                '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 “Receive RETURN value.” Once it executes successfully, it returns a value which would be either “found” or “not found.”

    The most important issue in the above code is that I used “ParameterDirection.ReturnValue” as part of “Direction” property.  You can also see the use of IIF function used to simply notate our own true/false values.

    Summary

    Even though, I am using only the “SQLCommand” object throughout all of my examples, you can still achieve the same results using “SQLDataReader” or “SQLDataAdapter.”  The best and the most efficient approach to working with stored procedures is working with “SQLCommand.”  Again, I say, depending on your needs (such as working with data tables) we need to shift to the most efficient way.

    I developed the application using Microsoft Windows Server 2003 Standard Edition with Microsoft Visual Studio.NET 2003 Enterprise Architect and Microsoft SQL Server 2000. If anything does not work, please drop me a line so that I can guide you.  The entire solution for this article is freely available in the form of a zip downloadable. 

    In the next article, we shall wok with data tables, data sets, and so on using stored procedures. If possible, we shall also look into stored functions and compare both. 

    Any comments, suggestions, feedback, bugs, errors, enhancements are highly appreciated at jag_chat@yahoo.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 1 hosted by Hostway
    Stay green...Green IT