ASP.NET
  Home arrow ASP.NET arrow Page 2 - Updating and Inserting Data with ADO.NET a...
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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? 
ASP.NET

Updating and Inserting Data with ADO.NET and ASP.NET 2.0
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-09-24

    Table of Contents:
  • Updating and Inserting Data with ADO.NET and ASP.NET 2.0
  • Closing SqlConnection and SqlDataReader objects through the using block
  • Inserting and Updating Data using the SqlCommand
  • Using Parameterized Queries to Retrieve Data

  • 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


    Updating and Inserting Data with ADO.NET and ASP.NET 2.0 - Closing SqlConnection and SqlDataReader objects through the using block


    (Page 2 of 4 )

    In C# we can make sure that any object that has a Dispose() method, by implementing the IDisposable interface, can be disposed of with the using block. The using block is passed a reference to an object that is used inside the block;  when the execution of the block's code completes, the object's Dispose() method is called. Note that the Dispose() method will also be called in the event of an exception; in fact the Dispose() method will be called no matter what happens. Both the SqlConnection and the SqlDataReader implement the IDisposable interface so they have a Dispose() method defined and implemented. The Dispose() method implicitly calls the Close() method which is exactly what we need. Let's see the modified version of the above example that uses this technique.

    Replace the Page_Load() event handler with the following:

    protected void Page_Load(object sender, EventArgs e){
      // create the connection and set the connection string
      // through the connection object's constructor
      string connectionString = "Data Source=(local);
      Initial Catalog=Northwind;Integrated Security=True";
      SqlConnection connection = new SqlConnection(connectionString);

      // create the command object and the T-SQL SELECT statement
      // and assign the connection object along with the T-SQL
      // SELECT statement to command object through the constructor
      string commandText = "SELECT LastName, FirstName FROM
    Employees";
      SqlCommand command = new SqlCommand(commandText, connection);

      // we have initialized the connection and the command so it's
      // time to open the connection and execute the command
      // using a try/catch block to handle exceptions
      SqlDataReader dataReader;
      try{
        // check if the connection is already open
        if (connection.State == ConnectionState.Closed){
          connection.Open();
        }
        // make sure that the connection is closed after we finish
        // using it
        using (connection){
          // get a SqlDataReader object from the Command's 
          // ExecuteReader() method and make sure that the data 
          // reader is closed too
          using (dataReader = command.ExecuteReader()){
            // adding the returned rows to the listbox control
            while (dataReader.Read()){
              ListBox1.Items.Add(dataReader["LastName"] + ", " +
    dataReader["FirstName"]);
            }
          }
        }
      }
      catch (Exception ex){
        Label1.Text = ex.Message;
      }
    }

    What we have added in this example is two using blocks, one for the connection object and the other for the data reader object. Note that you can create the object with the using block like so:

    using(SqlConnection connection = new SqlConnection(connectionString)){
      connection.Open();
      // execute the command and return the data reader

    // here the SqlConnection.Dispose() method will be called 
    // which in turn calls the SqlConnection.Close() method

    But in our example we passed a reference to the connection object because we wanted to test whether or not the connection is open before we use it. We have also used another nested using block with the data reader object to make sure that it's closed when we finish using it. Actually if we don't close the SqlDataReader object, the SqlConnection object can't be used for anything else because it's busy serving the SqlDataReader object, so it's very important to close the SqlDataReader object. We have removed the finally block because it's not needed anymore; we have the using block which closes the connection, and the data reader, when it finishes executing its statements.

    More ASP.NET Articles
    More By Michael Youssef


       · Before reading this article, please read the article Introducing ADO.NET with...
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek