指数超出范围。必须是非负数且小于集合的大小? [英] Index was out of range. Must be non-negative and less than the size of the collection ?

查看:82
本文介绍了指数超出范围。必须是非负数且小于集合的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指数超出范围。必须是非负的并且小于集合的大小。

我正在尝试获取gridwiew行的id并使用链接按钮在另一个页面中打印行信息并保持给定我这个错误,

任何帮助请



 < ;   asp:GridView     ID   =  GridView1    runat   =  server    AllowPaging   =  True     AllowSorting   =  True < span class =code-attribute> 

< span class =code-attribute> AutoGenerateColumns = False DataSourceID = SqlDataSource1 < span class =code-keyword>>
< >
< asp:BoundField DataField = emp_fname HeaderText = emp_fname

SortExpression = emp_fname / >
< asp:BoundField DataField = emp_lname 他aderText = emp_lname

SortExpression = emp_lname / >
< /列 >
< / asp:GridView > ;
< asp:SqlDataSource ID = < span class =code-keyword> SqlDataSource1 runat = server

ConnectionString = < span class =code-keyword> <% $ ConnectionStrings:dbConnectionString1%>

< span class =code-attribute> SelectCommand = SELECT [emp_fname],[emp_lname] FROM [ employee] WHERE([com_id] = @com_id) >
< SelectParameters >
< asp:SessionParameter 名称 < span class =code-keyword> = com_id SessionField = comid 类型 = Int32 / >
< / SelectParameters >

< / asp:SqlDataSource >



< pre lang =c#> protected void LinkBut​​ton1_Click( object sender,EventArgs e)
{

LinkBut​​ton lnkbtn = sender as LinkBut​​ton;
// 获取特定行链接按钮
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
// 获取特定行的email_id
int email_id = Convert.ToInt32(GridView1.DataKeys [ 0 ]。Value.ToString());
string email_from = gvrow.Cells [ 0 ]。文字;
Response.Redirect( 〜/ readmsg.aspx?email_id = + email_id);
con.Close();
}

解决方案

ConnectionStrings:dbConnectionString1%>

SelectCommand = SELECT [emp_fname],[emp_lname] FROM [employee] WHERE([com_id] = @com_id) < span class =code-keyword>>
< SelectParameters >
< asp:SessionParameter 名称 = com_id SessionField = comid 类型 = < span class =code-keyword> Int32 / >
< / SelectParameters >

< / asp:SqlDataSource >



  protected   void  LinkBut​​ton1_Click( object  sender,EventArgs e)
{

LinkBut​​ton lnkbtn = sender as LinkBut​​ton;
// 获取特定行链接按钮
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
// 获取特定行的email_id
int email_id = Convert.ToInt32(GridView1.DataKeys [ 0 ]。Value.ToString());
string email_from = gvrow.Cells [ 0 ]。文字;
Response.Redirect( 〜/ readmsg.aspx?email_id = + email_id);
con.Close();
}


尝试访问索引超出数组边界的数组元素时引发的异常。 [ ^ ]

看来你的网格没有 DataKeys [ ^ ]。你确定你的 GridView [ ^ ]填充了记录?因为情况似乎并非如此。

无论如何,只需调试和单步执行代码就可以显示 GridView.DataKeys [0] 确实存在。



编辑:

你应该总是使用Count或Length(扩展)方法或集合的属性来确保那里在尝试访问之前,集合中有足够的对象。

请考虑以下事项:

  if (GridView.DataKeys.Count >   0 
{
// 访问集合中第一个元素的代码就在这里。
// 请记住,DataKeys [0]返回第一个元素(count> = 1)。

// 这是安全的,因为Count i s大于0,所以至少有一个项目。
GridView.DataKeys [ 0 ]。Value.ToString()
// 可能仍会抛出异常,因为你不确定count = 2。
GridView.DataKeys [ 1 ]。Value.ToString()
}

希望有帮助:)


Index was out of range. Must be non-negative and less than the size of the collection.
I'm trying to get the id of gridwiew row and use the link button to print the row info in another page and it keeps give me this error ,
any help please

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"

    AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
  <asp:BoundField DataField="emp_fname" HeaderText="emp_fname"

      SortExpression="emp_fname" />
  <asp:BoundField DataField="emp_lname" HeaderText="emp_lname"

      SortExpression="emp_lname" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"

    ConnectionString="<%$ ConnectionStrings:dbConnectionString1 %>"

    SelectCommand="SELECT [emp_fname], [emp_lname] FROM [employee] WHERE ([com_id] = @com_id)">
<SelectParameters>
    <asp:SessionParameter Name="com_id" SessionField="comid" Type="Int32" />
</SelectParameters>

</asp:SqlDataSource>


protected void LinkButton1_Click(object sender, EventArgs e)
{

    LinkButton lnkbtn = sender as LinkButton;
    //getting particular row linkbutton
    GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
    //getting email_id of particular row
    int email_id = Convert.ToInt32(GridView1.DataKeys[0].Value.ToString());
    string email_from = gvrow.Cells[0].Text;
    Response.Redirect("~/readmsg.aspx?email_id=" + email_id);
    con.Close();
}

解决方案

ConnectionStrings:dbConnectionString1 %>" SelectCommand="SELECT [emp_fname], [emp_lname] FROM [employee] WHERE ([com_id] = @com_id)"> <SelectParameters> <asp:SessionParameter Name="com_id" SessionField="comid" Type="Int32" /> </SelectParameters> </asp:SqlDataSource>


protected void LinkButton1_Click(object sender, EventArgs e)
{

    LinkButton lnkbtn = sender as LinkButton;
    //getting particular row linkbutton
    GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
    //getting email_id of particular row
    int email_id = Convert.ToInt32(GridView1.DataKeys[0].Value.ToString());
    string email_from = gvrow.Cells[0].Text;
    Response.Redirect("~/readmsg.aspx?email_id=" + email_id);
    con.Close();
}


The exception that is thrown when an attempt is made to access an element of an array with an index that is outside the bounds of the array.[^]
It seems your grid has no DataKeys[^]. Are you sure your GridView[^] is filled with records? Because that doesn't seem to be the case.
Anyway, simply debugging and stepping through the code should reveal if GridView.DataKeys[0] does actually exist.

Edit:
You should always use the Count or Length (extension) method or Property of collections to make sure there are enough objects in the collections before trying to access one.
Consider the following:

if (GridView.DataKeys.Count > 0)
{
    // Your code accessing the 1st element in the collection goes here.
    // Remember that DataKeys[0] returns the first element (count >= 1).

    // This is safe, since Count is bigger than 0, so there is at least one item. 
    GridView.DataKeys[0].Value.ToString()
    // Might still throw an exception, since you're not sure if count = 2.
    GridView.DataKeys[1].Value.ToString()
}

Hope that helps :)


这篇关于指数超出范围。必须是非负数且小于集合的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆