我得到这个错误 - “索引超出范围。必须是非负数且小于集合的大小。参数名称:index" .... plz help [英] i m getting this error- "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"....plz help

查看:78
本文介绍了我得到这个错误 - “索引超出范围。必须是非负数且小于集合的大小。参数名称:index" .... plz help的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  foreach (GridViewRow row  in  grdCat.Rows)
{
CheckBox ch =(CheckBox)row.FindControl( CheckBox1);
if (ch.Checked)
{
int id = Convert.ToInt32(grdCat.DataKeys [row.RowIndex]。价值);
string qry = 从简报中选择电子邮件其中id =' + id + ';
SqlConnection con = new SqlConnection(ConString);
SqlCommand cmd = new SqlCommand(qry,con);

con.Open();
string EID = cmd.ExecuteScalar()。ToString();
con.Close();
EmailTo + = EID + ;
}
}

解决方案

我们真的无法帮到你 - 我们没有任何访问权限到您的计算机,以及您放入其中的数据。首先,使用调试器查看DataKeys集合:它有多少个元素? row.RowIndex的值是多少?它在集合中吗? (不,它不是 - 但在你看看收藏品的大小和实际价值之前,你不知道为什么不这样做。)



可能,你还没有设置DataKeyNames属性,所以集合是空的?但是找到的唯一方法就是看看!


在Gridview中使用datakeynames





解决方案



的DataKeyNames

foreach (GridViewRow row in grdCat.Rows)
{
    CheckBox ch = (CheckBox)row.FindControl("CheckBox1");
    if (ch.Checked)
    {
        int id =Convert.ToInt32(grdCat.DataKeys[row.RowIndex].Value);
        string qry = "Select email from newsletter where id='" + id + "'";
        SqlConnection con = new SqlConnection(ConString);
        SqlCommand cmd = new SqlCommand(qry, con);
                
        con.Open();
        string EID = cmd.ExecuteScalar().ToString();
        con.Close();
        EmailTo += EID + ",";
    }
}

解决方案

We can't really help you much - we don;t have any access to your computer, and the data you have put in it. So start by looking at the DataKeys collection using the debugger: How many elements does it have? What is the value of row.RowIndex? Is it inside the collection? (No, it isn't - but until you look at the size of the collection and the actual value, you can't tell why not).

Possibly, you haven't set the DataKeyNames property, so the collection is empty? But teh only way to find out is to look and see!


use datakeynames in Gridview


Solution

Datakeynames


这篇关于我得到这个错误 - “索引超出范围。必须是非负数且小于集合的大小。参数名称:index" .... plz help的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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