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

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

问题描述

我尝试从选定的行获取id值,当我运行代码时,我得到的值或id为null。这是我的代码,请帮帮我。



I try to get the id value from selected row and when i run the code i get the value or id is null. Here is my code, please help me.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Select")
                {
                    MultiView1.ActiveViewIndex = 1;
                    GridView1.Visible = true;

                    int index = Convert.ToInt32(e.CommandArgument.ToString());

                    string id = GridView2.DataKeys[index].Value.ToString();
                    var emp = db.tb_Employees.Where(x => x.Employee_id == id).FirstOrDefault();

                    if (emp != null)
                    {
                        text_editname.Text = emp.Name;
                        text_editaddress.Text = emp.Address;
                        text_editemail.Text = emp.Email;
                        text_editphone.Text = emp.Phone;
                        DropDownList2.SelectedValue = emp.Division_id;
                    }
                }
            }
            catch (Exception)
            { }


        }





我得到:

异常:指数超出范围。必须是非负的,小于集合的大小。(System.ArgumentOutOfRangeException)



I get :
Exception : "Index was out of range. Must be non-negative and less than the size of the collection."(System.ArgumentOutOfRangeException)

推荐答案

你好



导致此错误的原因是事件处理程序是GridView 1 而数据键检索是GridView 2



显然结果不匹配:)
Hello

The cause of this error is that the event handler is of "GridView1" whereas the datakey retrieval is of "GridView2"

Obviously the result won't match :)


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

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