ADO Net Database ASP.Net Tutorial

You can handle errors thrown by the SqlDataSource control by handling any or all of 
the following four events: Deleted, Inserted, Selected, Updated
Each of these events is passed an EventArgs parameter that includes any exceptions raised 
when the command was executed. 
<%@ Page Language="C#" %>
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    protected void srcProducts_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (e.Exception != null)
        {
            lblError.Text = e.Exception.Message;
            e.ExceptionHandled = true;
        }
    }



    
    

            id="lblError"
        EnableViewState="false"
        CssClass="error"
        Runat="server" />
            id="grdProducts"
        DataSourceID="srcProducts"
        Runat="server" />
            id="srcProducts"
        SelectCommand="SELECT * FROM DontExist"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        OnSelected="srcProducts_Selected"
        Runat="server" />
    

    


File: Web.config

  
             connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />