如何处理与一个SqlDataSource例外 [英] How to handle exceptions with a SqlDataSource

查看:225
本文介绍了如何处理与一个SqlDataSource例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是我的GridView控件提供的数据一个SqlDataSource。这就是所有我用我的形式,因此,我背后有没有在所有code。但地方我需要一个try catch块,以防万一我的连接得到的丢失。什么code我必须放置在哪里?

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?

如果我得到一个错误,我想我的lblMessage文本是无连接。

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

修改

我在我的GridView的Machine.aspx

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>

我在我的Machine.aspx连接权在我的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>

我的code在我的code在我Machine.aspx.cs Behind文件

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

为什么?

推荐答案

SqlDataSource控件有一个事件。处理程序添加到这一事件像这样,并处理任何错误(显示一条提示消息等),在此处理。

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

很抱歉,但你将不得不在其code-背后的一些code!

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

修改

看你的code,我不认为你曾经绑定你的GridView,所以你的SqlDataSource是从来没有试图从数据库中选择数据。

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.

在你的Page_Load方法中,添加以下code:

In your Page_Load method, add the following code:

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

进一步编辑

试着改变onselected到OnSelected你SqlDataSource和删除线,以你的事件处理程序绑定在code的后面。

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.

均匀进一步修改

试试这个

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;
    }
}

这篇关于如何处理与一个SqlDataSource例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆