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

コードがSQLコントロールに送信される前に発生するように、Gridview_ItemInsertingまたはgridview_itemupdatingのコードビハインドからエラーを処理する必要があります。

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 イベントのイベント ハンドラーを作成し、例外が発生したかどうかをテストし、必要なエラー レポートを実行してから、エラーを処理したことを示します。

    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

SQLDataSource の Selected イベントを処理し、例外のイベント引数を確認する必要があると思います。

于 2009-04-01T11:55:52.640 に答える
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 に答える