0

私のコードは(Asp.Net、C#)です

    int index = Convert.ToInt16(e.CommandArgument);
    string str = GridView2.DataKeys[index].Value.ToString();
    Session["studyuid2"] = str;

2行目は、エラーインデックスが範囲外でした。負ではなく、コレクションのサイズよりも小さい必要があります。パラメータ名:インデックス

そして私のグリッドビューは

<asp:GridView ID="GridView2" runat="server" AllowPaging="True" Height="100px" 
                    RowStyle-Height="25px" HeaderStyle-Height="30px" FooterStyle-Height="30px" 
                    CellPadding=5 CellSpacing=5  
                    AutoGenerateColumns="False" 
                    DataSourceID="SqlDataSource1" EnableModelValidation="True" 
                    Width="100%" 
                    DataKeyNames="StudyUID" 
                    onrowcommand="GridView2_RowCommand" 
                    AllowSorting="True">
<RowStyle Height="25px"></RowStyle>
                    <Columns>

-------------------------------

                    </Columns>

<FooterStyle Height="30px"></FooterStyle>

<HeaderStyle Height="30px"></HeaderStyle>
                </asp:GridView>
4

1 に答える 1

2

このような状況では、エラー処理を追加すると便利です。残念ながらArgumentOutOfRangeException、引数の値と有効な範囲がわかりません。あなたはこのようにそれをすることができます、そしてそれはあなたがデバッグするのを助けるでしょう。

int index = Convert.ToInt16(e.CommandArgument);

try
{
    string str = GridView2.DataKeys[index].Value.ToString();
    Session["studyuid2"] = str;
}
catch (ArgumentOutOfRangeException ex) 
{
    throw new ArgumentOutOfRangeException(
        String.Format(
            "The index passed is not valid for the collection.  The index is '{0}' and must be between 0 and '{1}'.",
            index,
            GridView2.DataKeys.Count));
}

DataKeys[index]キャッチして再スローするために例外処理に依存する代わりに、を呼び出す前にインデックスを検証することによってこれを行うこともできます。

于 2012-08-24T16:02:13.657 に答える