4

I have a SqlDataSource that is supplying data to my GridView. Thats all i am using on my form, thus i have NO code behind at all. But somewhere i need a TRY CATCH block just in case my connection get's lost. What code must i place where?

If i get a error i want my lblMessage Text to be "No connection".

Edit

My GridView in my Machine.aspx

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" 
    Height="209px" PageSize="7" Width="331px" AllowSorting="True" 
                DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="True" 
            SortExpression="Total" DataFormatString="{0:R#,###,###}" >
            <HeaderStyle HorizontalAlign="Left" />
        </asp:BoundField>
        <asp:BoundField DataField="b134_rmcid" HeaderText="Machine"  ReadOnly="True" 
            SortExpression="b134_rmcid" >
            <HeaderStyle HorizontalAlign="Left" />
        </asp:BoundField>
        <asp:BoundField DataField="b134_recdate" DataFormatString="{0:d/MM/yyyy}" 
            HeaderText="Date" ReadOnly="True" SortExpression="b134_recdate" >
            <HeaderStyle HorizontalAlign="Left" />
        </asp:BoundField>
    </Columns>
</asp:GridView>

My Connection right under my Gridview in my Machine.aspx

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ODBC_ConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ODBC_ConnectionString.ProviderName %>" 

    SelectCommand="SELECT SUM(b134_nettpay) AS Total, b134_rmcid, b134_recdate FROM B134HRE" 
    onselected="SqlDataSource1_Selected">

</asp:SqlDataSource>

My Code in my Code Behind file in my Machine.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    lblError.Text = "hello there";
    SqlDataSource1.Selected += new SqlDataSourceStatusEventHandler(SqlDataSource1_Selected);


}


protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{

  if (e.ExceptionHandled)
   {

       lblError.Text = "There is a problem";  

   }

}

And still for some ready when i place a BreakPoint in my Selected Event it does not even get to it???

Why?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Etienne
  • 7,141
  • 42
  • 108
  • 160

5 Answers5

9

The SqlDataSource has an Selected event. Add a handler to this event like so, and handle any errors (show an informative message etc) in this handler.

void GridView1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    if (e.ExceptionHandled)
    {
        //Show error message
    }
}

Sorry, but you're going to have to have some code in the code-behind!

Edit

Looking at your code, I don't think you're ever binding your GridView, so your SqlDataSource is never trying to select the data from your database.

In your Page_Load method, add the following code:

    if (!IsPostBack)
    {
        GridView1.DataBind();
    }

Further edit

Try changing "onselected" to "OnSelected" on your SqlDataSource and remove the line to bind your event handler in the code behind.

I'm stumped if that doesn't work as you've basically got the simplest-possible example.

Even further edit

Try this instead

void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    if (e.Exception != null)
    {
        //Show error message
        lblError.Text = "There is a problem"; 

        //Set the exception handled property so it doesn't bubble-up
        e.ExceptionHandled = true;
    }
}
Paul Suart
  • 6,505
  • 7
  • 44
  • 65
  • Its not even going into the handler when i put a brake point there. – Etienne Apr 01 '09 at 12:24
  • did you wire up the event handler in page load or init: just type "GridView1.Selected +=" and then hit tab twice. Simple! – Paul Suart Apr 01 '09 at 12:34
  • In the code behind, in the page_init event handler, wire-up the event handler by typing "GridView1.Selected += new SqlDataSourceStatusEventHandler(GridView1_Selected);", where "GridView1" is the Id of your GridView. – Paul Suart Apr 01 '09 at 12:50
  • Cool, i did this and still when i place a Break Point in my SqlDataSource1_Selected event it does not go in. I also placed wired up the event in the OnInit. – Etienne Apr 01 '09 at 13:00
  • Is it actually selecting anything? You may find this article useful: http://msdn.microsoft.com/en-us/library/2ccyd347(VS.71).aspx - I can't add anything more I'm afraid. – Paul Suart Apr 01 '09 at 13:02
  • Wait this is what i placed in my Page-Load Event... SqlDataSource1.Selected += new SqlDataSourceStatusEventHandler(SqlDataSource1_Selected); If i replace the SqlDataSource1 with GridView1 there is a error. – Etienne Apr 01 '09 at 13:03
  • My mistake - replace any time I've mentioned GridView with SqlDataSource. This has nothing to do with the GridView whatsoever :/ – Paul Suart Apr 01 '09 at 13:13
  • Thanks for all the help, none of it worked for me though, ai ai. – Etienne Apr 01 '09 at 13:36
  • Can you post your code? After investing this much time, I'd like to get you on the right path. It will be something very straightforward. Post your code-behind and your SqlDataSource code please. – Paul Suart Apr 01 '09 at 13:38
  • Code is posted! Thank you so much for your help hey!! – Etienne Apr 01 '09 at 14:03
  • Hi, there is nothing wrong with my code. Like when i run everything works. When you create a SqlDataSource with the wizard you dont even need ANY code in the code behind. What i am doing is i am changing the connection string the the web.config to see what will happen when the project cant...... – Etienne Apr 02 '09 at 06:57
  • .....find the database. It just so that when the DB go down that the user wont see the ugly ERROR page you know. So all i want to do is when there is a error i need to give the user some kind of message to say what wrong is. – Etienne Apr 02 '09 at 06:59
  • Thanks for the new Updates, still, when i place a break point inside my Selected Event it does not even go to it. It goes straight to the ugly error screen when i change my connectionsting. – Etienne Apr 02 '09 at 09:24
  • Nope, still when i place a break point inside event it does not step into it. Maybe i must place it insite another event? Because all i am doing with the GRID is fill it with data on run time. – Etienne Apr 02 '09 at 13:42
  • Event started a BRAND NEW project and i get the same error when i change my connection string in my web.config. Is there no way of handeling something if ANYTHING goes wrong in ANY EVENT? – Etienne Apr 02 '09 at 13:46
1

you need to handle the error from code behind of your Gridview_ItemInserting or gridview_itemupdating so as to fire before your code are submitted to the sql control.

protected void GridView1_ItemInserting(object sender,DetailsViewInsertEventArgs e)
{
if (e.Exception != null)
{
Lblerror.text="error message"
}
}

do so also for updating

Jesse Mwangi
  • 155
  • 2
  • 8
0
void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
if (e.Exception = null)
{
//bind data to gridview
GridView1.DataBind();
}
else
{
//Show error message    
lblError.Text = "There is a problem";
//Set the exception handled property so it doesn't bubble-up
e.ExceptionHandled = true;
}
}
Jesse Mwangi
  • 155
  • 2
  • 8
0

Create an event handler for the Selected event of the SqlDataSource, test if an exception occurred, perform whatever error reporting you want, then indicate that you've now handled the error.

    mSqlDataSource.Selected += new sqlDataSourceStatusEventHandler(mSqlDataSource_Selected);


    void mSqlDataSource_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (e.Exception != null)
        {
            mErrorText.Text = e.Exception.Message;
            mErrorText.Visible = true;
            e.ExceptionHandled = true;
        }
    }
0

I believe you want to handle the Selected event of the SQLDataSource, and check the event argument for the exception.

John Saunders
  • 160,644
  • 26
  • 247
  • 397