简体   繁体   English

System.ArgumentOutOfRangeException:索引超出范围。 必须是非负的并且小于集合的大小。 参数名称:索引

[英]System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

I am Getting this error System.ArgumentOutOfRangeException: Index was out of range.我收到此错误 System.ArgumentOutOfRangeException: 索引超出范围。 Must be non-negative and less than the size of the collection.必须是非负的并且小于集合的大小。 Parameter name: index参数名称:索引

Source Error:源错误:

Line 153: Session["Cat"] = e.CommandArgument.ToString();第 153 行:Session["Cat"] = e.CommandArgument.ToString();
Line 154:第 154 行:
Line 155: Session["Cat_ID"] = DataList3.DataKeys[e.Item.ItemIndex].ToString();//error第 155 行:Session["Cat_ID"] = DataList3.DataKeys[e.Item.ItemIndex].ToString();//错误
Line 156: Response.Redirect("Disp_Category.aspx?id=C");第 156 行:Response.Redirect("Disp_Category.aspx?id=C"); Line 157: }第 157 行:}

Please some one what is wrong with this code请问这个代码有什么问题

protected void DataList3_ItemCommand(object source, DataListCommandEventArgs e)
 {
     Session["L_Id"] = ddlLanguage.SelectedValue;
     Session["Cat"] = e.CommandArgument.ToString();

     Session["Cat_ID"] = DataList3.DataKeys[e.Item.ItemIndex].ToString();
     Response.Redirect("Disp_Category.aspx?id=C");
 }

on aspx file i have this在aspx文件上我有这个

<asp:DataList ID="DataList3" runat="server" DataKeyNames="C_Id" DataSourceID="SqlDataSource3" OnItemCommand="DataList3_ItemCommand">
                                <ItemTemplate>
                                    <table cellpadding="0" cellspacing="0" border=1 width=150>
                                        <tr>
                                            <td>
                                                <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("C_Name") %>'
                                                    Text='<%# Eval("C_Name") %>' CausesValidation="False"></asp:LinkButton></td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:DataList><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\New Projects\Sym\App_Data\Music.mdf;Integrated Security=True;User Instance=True"
                                ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Category] ORDER BY [C_Name]">
                            </asp:SqlDataSource>

You are not right.你看起来不太对劲。 I suggest to set the id (c_id) and not the name (c_name) in CommandArgument .我建议在CommandArgument设置 id (c_id) 而不是名称 (c_name)。 That's your key.那是你的钥匙。 In ItemCommand you can get entire record by id.ItemCommand您可以通过 id 获取整个记录。 Moreover:而且:

Response.Redirect("Disp_Category.aspx?id=C");

becomes变成

Response.Redirect("~/Disp_Category.aspx?id=C");  //id=C? sure?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.ArgumentOutOfRangeException: &#39;索引超出范围。 必须是非负的并且小于集合的大小。 参数名称:索引&#39; - System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' System.ArgumentOutOfRangeException 索引超出范围。 必须是非负数且小于集合的大小。 参数名称:索引 - System.ArgumentOutOfRangeException Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ArgumentOutOfRangeException:&#39;索引超出范围。 必须为非负数,并且小于集合的大小。” - System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.' 错误:System.ArgumentOutOfRangeException:索引超出范围。 必须为非负数且小于集合的大小 - Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:index - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:index - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM