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

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

问题描述

在第一页中将显示五条记录,在第二页中将显示另外五条记录。



假设在第一页中我检查两行和在第二页中我检查另外两行意味着选中的复选框行要在表中的[transact]。[transaction_item]中更新。



当我运行时下面的代码显示错误如下



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



错误显示在下面的行如下



 index =( int )grdRpt.DataKeys [gvrow.RowIndex] .Value; 





如何解决这个错误。请告诉我。



我的尝试:



< pre lang =c#> protected void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}


受保护 void BindData()
{
字符串 strConnString = ConfigurationManager.ConnectionStrings [ ConnectionStrings]。ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand( select *来自[transact]。[transaction_item]其中status ='new',con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
grdRpt.DataSource = ds;
grdRpt.DataBind();
}


受保护 void grdRpt_PageIndexChanging(< span class =code-keyword> object sender,GridViewPageEventArgs e)
{
grdRpt.PageIndex = e.NewPageIndex;
BindData();
}


私有 void PopulateCheckedValues()
{
System.Collections.ArrayList userdetails =(System.Collections.ArrayList)Session [ CHECKED_ITEMS];
if (userdetails!= null && userdetails.Count > 0
{
foreach (GridViewRow gvrow in grdRpt.Rows)
{
int index =( int )grdRpt.DataKeys [gvrow.RowIndex] .Value;
if (userdetails.Contains(index))
{
CheckBox myCheckBox =(CheckBox)gvrow.FindControl( chkselecdata);
myCheckBox.Checked = true ;
}
}
}
}

private void SaveCheckedValues()
{
System.Collections.ArrayList userdetails = new System.Collections.ArrayList();
int index = -1;
foreach (GridViewRow gvrow in grdRpt.Rows)
{
index =( int )grdRpt.DataKeys [gvrow.RowIndex] .Value;
bool result =((CheckBox)gvrow.FindControl( chkselecdata))经过。


if (会话[ CHECKED_ITEMS]!= null
userdetails =(System.Collections.ArrayList)Session [ CHECKED_ITEMS];
if (result)
{
if (!userdetails。包含(索引))
userdetails.Add(index);
}
else
userdetails.Remove(index);
}
如果(userdetails!= null && userdetails.Count > 0
会话[ CHECKED_ITEMS] = userdetails;
}


受保护 void btnsubmit_Click(< span class =code-keyword> object
sender,EventArgs e)
{
String strConnString = ConfigurationManager.ConnectionStrings [< span class =code-string>
ConnectionStrings]。ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
foreach (GridViewRow row in grdRpt.Rows)
{
int key =( int )grdRpt.DataKeys [row.RowIndex] .Value;
CheckBox cb =(CheckBox)row.FindControl( chkselecdata);
SqlCommand cmd = new SqlCommand( 更新[transact]。[transaction_item] SET Status ='Ready'其中Id = + key.ToString(),con);
cmd.ExecuteNonQuery();
}
con.Close();
}

解决方案

错误正是消息所说的:

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

如果它出现在这一行:

 index =(int)grdRpt.DataKeys [gvrow.RowIndex] .Value; 

然后 gvrow.RowIndex 对于DataKeys集合来说是负数或太大。使用调试器找出实际值是什么,以及集合的大小。然后你就可以开始研究为什么它的价值......



我们不能为你做到这一点:我们无权访问你的数据,并且无法在相同的条件下运行您的代码。


in the first page five records will be displayed and in the second page another five records will be displayed.

suppose in the first page i check two rows and in the second page i check another two rows means that selected check box rows to be update in the [transact].[transaction_item] in the table.

when i run below code shows error as follows

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


The error shows in below line as follows

index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;



how to solve this error. please let me know.

What I have tried:

protected void Page_Load(object sender, EventArgs e)
        {
           if (!IsPostBack)
                {
                     BindData();
                }
       }


 protected void BindData()
        {
             String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);
                SqlCommand cmd = new SqlCommand("select * from [transact].[transaction_item] where status = 'new'", con);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da.SelectCommand = cmd;
                da.Fill(ds);
                grdRpt.DataSource = ds;
                grdRpt.DataBind();
        }


 protected void grdRpt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdRpt.PageIndex = e.NewPageIndex;
            BindData();
        }


private void PopulateCheckedValues()
        {
            System.Collections.ArrayList userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
            if (userdetails != null && userdetails.Count > 0)
            {
                foreach (GridViewRow gvrow in grdRpt.Rows)
                {
                    int index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;
                    if (userdetails.Contains(index))
                    {
                        CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkselecdata");
                        myCheckBox.Checked = true;
                    }
                }
            }
        }

   private void SaveCheckedValues()
        {
            System.Collections.ArrayList userdetails = new System.Collections.ArrayList();
            int index = -1;
            foreach (GridViewRow gvrow in grdRpt.Rows)
            {
                index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;
                bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

               
                if (Session["CHECKED_ITEMS"] != null)
                    userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
                if (result)
                {
                    if (!userdetails.Contains(index))
                        userdetails.Add(index);
                }
                else
                    userdetails.Remove(index);
            }
            if (userdetails != null && userdetails.Count > 0)
                Session["CHECKED_ITEMS"] = userdetails;
        }


 protected void btnsubmit_Click(object sender, EventArgs e)
        {
           String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                foreach (GridViewRow row in grdRpt.Rows)
                {
                    int key = (int)grdRpt.DataKeys[row.RowIndex].Value;
                    CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
                    SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where Id=" + key.ToString(), con);
                    cmd.ExecuteNonQuery();
                }
                con.Close();
        }

解决方案

The error is exactly what the message says:

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

If it occurs on this line:

index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;

then gvrow.RowIndex is either negative, or too big for the DataKeys collection. Use the debugger to find out what the actual value is, and what the size of the collection is. Then you can start working out why it's the value it is...

We can't do that for you: we don't have access to your data, and can't run your code under the same conditions you can.


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

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