【问题标题】:How to handle exceptions with a SqlDataSource如何使用 SqlDataSource 处理异常
【发布时间】:2010-10-16 20:23:11
【问题描述】:

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

如果我收到错误,我希望我的 lblMessage 文本为“无连接”。

编辑

我的 Machine.aspx 中的 GridView

<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 下的我的连接

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

我的代码在我的 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";  

   }

}

当我在我的选定事件中放置一个断点时,仍然有些准备就绪,它甚至没有到达它???

为什么?

【问题讨论】:

    标签: c# asp.net vb.net datagridview sqldatasource


    【解决方案1】:
    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;
    }
    }
    

    【讨论】:

      【解决方案2】:

      您需要处理来自 Gridview_ItemInserting 或 gridview_itemupdating 后面代码的错误,以便在您的代码提交到 sql 控件之前触发。

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

      更新也是如此

      【讨论】:

        【解决方案3】:

        为 SqlDataSource 的 Selected 事件创建一个事件处理程序,测试是否发生异常,执行您想要的任何错误报告,然后表明您现在已经处理了该错误。

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

        【讨论】:

          【解决方案4】:

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

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

          抱歉,您必须在代码隐藏中添加一些代码!

          编辑

          查看您的代码,我认为您从未绑定过 GridView,因此您的 SqlDataSource 永远不会尝试从数据库中选择数据。

          在您的 Page_Load 方法中,添加以下代码:

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

          【讨论】:

          • 当我把刹车点放在那里时,它甚至没有进入处理程序。
          • 您是否在页面加载或初始化中连接了事件处理程序:只需键入“GridView1.Selected +=”,然后按两次 Tab。简单!
          • 在后面的代码中,在 page_init 事件处理程序中,通过键入“GridView1.Selected += new SqlDataSourceStatusEventHandler(GridView1_Selected);”来连接事件处理程序,其中“GridView1”是您的 Id网格视图。
          • 酷,我这样做了,但当我在我的 SqlDataSource1_Selected 事件中放置一个断点时,它仍然没有进入。我还在 OnInit 中放置了连接事件。
          • 它实际上是在选择什么吗?您可能会发现这篇文章很有用:msdn.microsoft.com/en-us/library/2ccyd347(VS.71).aspx - 恐怕我不能再添加任何内容了。
          【解决方案5】:

          我相信你想处理 SQLDataSource 的 Selected 事件,并检查事件参数是否存在异常。

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-09
          • 2017-04-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多