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. |