代码之家  ›  专栏  ›  技术社区  ›  Etienne

如何使用SqlDataSource处理异常

  •  2
  • Etienne  · 技术社区  · 16 年前

    我有一个SqlDataSource,它正在向我的GridView提供数据。这就是我在表格上使用的全部内容,因此我根本就没有代码。但在某个地方我需要一个TRY-CATCH块,以防我的连接丢失。我必须把什么代码放在哪里?

    编辑

    我的网格视图机器.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>
    

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

    我的代码隐藏文件中的代码机器.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";  
    
       }
    
    }
    

    为什么?

    5 回复  |  直到 14 年前
        1
  •  9
  •   Paul Suart Wes    16 年前

    SqlDataSource具有 Selected 事件。像这样向这个事件添加一个处理程序,并在此处理程序中处理任何错误(显示信息性消息等)。

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

    编辑

    看看你的代码,我认为你从来没有绑定过GridView,所以你的SqlDataSource从不尝试从数据库中选择数据。

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

    进一步编辑

    尝试将SqlDataSource上的“onselected”更改为“onselected”,并删除在代码隐藏中绑定事件处理程序的行。

    如果这不起作用,我会很困惑,因为你已经有了一个最简单的例子。

    更进一步的编辑

    试试这个

    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;
        }
    }
    
        2
  •  1
  •   Jesse Mwangi    10 年前

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

    更新时也要这样做

        3
  •  0
  •   John Saunders    16 年前

    我相信您希望处理SQLDataSource的选定事件,并检查异常的事件参数。

        4
  •  0
  •   Lachlan Wetherall    14 年前

        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;
            }
        }
    
        5
  •  0
  •   Jesse Mwangi    8 年前
    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;
    }
    }