簡體   English   中英

如何使用 SqlDataSource 處理異常

[英]How to handle exceptions with a SqlDataSource

我有一個 SqlDataSource 向我的 GridView 提供數據。 這就是我在表單上使用的全部內容,因此我根本沒有代碼。 但是在某個地方我需要一個 TRY CATCH 塊,以防我的連接丟失。 我必須在哪里放置什么代碼?

如果我收到錯誤,我希望我的 lblMessage 文本為“無連接”。

編輯

我的 GridView 在我的 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 下

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

   }

}

當我在我的選定事件中放置一個斷點時,仍然有一些准備好它甚至沒有到達它???

為什么?

SqlDataSource 有一個Selected事件。 像這樣向這個事件添加一個處理程序,並在這個處理程序中處理任何錯誤(顯示信息性消息等)。

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

對不起,但你將不得不在代碼隱藏中有一些代碼!

編輯

查看您的代碼,我認為您從未綁定過 GridView,因此您的 SqlDataSource 永遠不會嘗試 select 數據庫中的數據。

在您的 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;
    }
}

您需要處理來自 Gridview_ItemInserting 或 gridview_itemupdating 代碼后面的錯誤,以便在您的代碼提交到 sql 控件之前觸發。

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

這樣做也是為了更新

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

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

我相信您想要處理 SQLDataSource 的 Selected 事件,並檢查事件參數是否存在異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM