MS SQL Server
  Home arrow MS SQL Server arrow 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 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
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 / 11
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Working with NULL, OUTPUT and RETURN in ADO.NET


    (Page 1 of 4 )

    This is the second article in a series focused on working with stored procedures in ADO.NET. In this article we mainly focus on passing NULLS to a stored procedure, working with the OUTPUT parameter, and retrieving RETURN values of stored procedures.
    A downloadable file for this article is available here.

    If you are new to stored procedures (or in accessing them using ADO.NET) in SQL Server, I strongly suggest you go through my first article before proceeding further.

    To work with the stored procedures in this article, you need the simple extra tables “emp” and “dept” within the “Northwind” database.  The structures of those two tables are available in the first article of this series.

    How to pass a NULL to a stored procedure parameter using ADO.NET from ASP.NET

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

    CREATE PROCEDURE dbo.sp_Emp_add
          (
                @empno      int,
                @ename      varchar,
                @sal  float,
                @deptno     int
          )
    AS
    INSERT INTO emp
    (
          empno,
          ename,
          sal,
          deptno
    )
    VALUES
    (
          @empno,
          @ename,
          @sal,
          @deptno
    )
    RETURN

    The above stored procedure simply adds a new row to the table “emp” based on the parameters you provided.  The stored procedure is named “sp_Emp_add.”  Now, we need to use ADO.NET to access the same in ASP.NET.

    The steps would be very similar to the steps I discussed in my first article in this series.  So, I shall start directly with Visual Studio.  Add a new web form (call it “SendingNull2SPParam”) to an already existing project (as specified in my previous article).  Add a button named “Execute SP with Parameter value as NULL.”

    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 SP with Parameter value as NULL” button:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles Button1.Click
            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_add"
                .Parameters.Add("@empno", 2001)
                .Parameters.Add("@ename", System.DBNull.Value)
                .Parameters.Add("@sal", 3400)
                .Parameters.Add("@deptno", 30)
                .Connection = cn
                .Connection.Open()
                .ExecuteNonQuery()
                .Connection.Close()
                .Dispose()
            End With
        End Sub

    Set the start page, execute your application (by pressing F5) and click on the button “Execute SP with Parameter value as NULL.”  Once it executes successfully, check your table “emp.”  You should be able to see a new row without “ename” (or with NULL)!

    The most important tip from the above code is that I used “System.DBNull.Value.” This is the most efficient way to deal with NULL values while passing parameters to stored procedures.

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hello guys. This is second in series focusing on "Working with Stored Procedures...
     

    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