4

我有一个向我的 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";  

   }

}

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

为什么?

4

5 回答 5

9

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;
    }
}
于 2009-04-01T12:05:41.227 回答
1

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

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

这样做也是为了更新

于 2014-10-02T08:21:11.010 回答
0

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

于 2009-04-01T11:55:52.640 回答
0

为 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;
        }
    }
于 2011-06-24T02:49:01.807 回答
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;
}
}
于 2017-03-07T07:11:06.040 回答