ASP.NET
  Home arrow ASP.NET arrow Page 4 - Introducing ADO.NET with ASP.NET 2.0
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? 
ASP.NET

Introducing ADO.NET with ASP.NET 2.0
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-09-19

    Table of Contents:
  • Introducing ADO.NET with ASP.NET 2.0
  • Populating a ListBox control from the Northwind database
  • Explaining the code
  • Using Exception Handling code

  • 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


    Introducing ADO.NET with ASP.NET 2.0 - Using Exception Handling code


    (Page 4 of 4 )

    While connecting to data sources it's best to wrap the code with a try/catch/finally block to make sure that the connection object is closed and the error is handled. I have added a Label control to the form which is assigned the Exception.Message in case an Exception has been thrown.

    <form id="form1" runat="server">
    <div>
    <asp:ListBox ID="ListBox1" runat="server" Height="165px"
    Width="152px"></asp:ListBox>
    <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
    </div>
    </form>

    The code of the Default.aspx.cs is as follows:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    // use the namespace of the ADO.NET SQL Server Data Provider
    using System.Data.SqlClient;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    // create the connection and set the connection string
    // through the connection object's ConnectionString property
    SqlConnection connection = new SqlConnection();
    string connectionString = "Data Source=(local);Initial
    Catalog=Northwind;Integrated Security=True";
    connection.ConnectionString = connectionString;

    // create the command object and assign the connection object to
    it
    // through the Connection property
    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    // creating the T-SQL SELECT statement and assign it to the
    command
    // through the CommandText property
    string commandText = "SELECT LastName, FirstName FROM
    Employees";
    command.CommandText = commandText;
    // assigning the value CommandType.Text to the CommandType
    property
    // because we are using a T-SQL statement not a stored procedure
    command.CommandType = CommandType.Text;

    // 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
    {
    connection.Open();
    // get a SqlDataReader object from the Command's ExecuteReader()
    method
    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;
    }
    finally
    {
    connection.Close();
    }
    }
    }
     

    Remove the last key/value pair from the connection string and run the page. You will now get the exception's message in the label control.

    The finally block is called whether or not an exception has been thrown. There is a better technique that we will look at in the next few articles which implies using the statement to implicitly close objects that implement the IDisposable interface. In other words the objects that have the Dispose() method defined.


    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.

       · Yes you can use the SqlDataSource control without knowing much about ADO.NET 2.0,...
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





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