如何解决指数超出范围。必须是非负数且小于集合的大小。 [英] How to solve index was out of range. Must be non-negative and less than the size of the collection.

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

问题描述





有谁知道这个。



怎么解决索引超出范围。必须是非负面的并且小于

集合的大小?



代码是:



我尝试过:



Hi,

Can anyone know this.

how to solve index was out of range. must be non-negative and less than the size of
the collection?

code is:

What I have tried:

HiddenField hfId = (HiddenField)gridData.Rows[gridData.SelectedIndex].FindControl("hfId");


此行上发生
错误




error coming on this line

 protected void lnkUpdate_Click(object sender, EventArgs e)
        {
HiddenField hg = (HiddenField)gridData.Rows[gridData.SelectedIndex].FindControl("hfId");
 try
                {
                    
                    List<SqlParameter> parameters = new List<SqlParameter>
                    {
                    };
                    parameters.Add(new SqlParameter("@P_APPLICATION_ID", hg));
                    parameters.Add(new SqlParameter("@P_APPLICATION_NAME", txtApplicationName.Text));
                    parameters.Add(new SqlParameter("@P_VERSION", txtAppVersion.Text));
                    parameters.Add(new SqlParameter("@P_DESCRIPTION", txtDesc.Text));
                    parameters.Add(new SqlParameter("@P_USER", lblName.Text));
                    parameters.Add(new SqlParameter("@P_MODE", "E"));

                    string strResponse = objAppFunctionModule.ExecuteNonQueryMethod(strUSP_APPLICATION_MST, parameters);
                    if (strResponse == "1")
                    {
                        divMessage.Visible = true;
                        divDelete.Visible = false;
                        lnkSave.Visible = true;
                        lnkAddNew.Visible = false;
                        lnkUpdate.Visible = false;
                        PopulateGrid();
                        ClearControls();
                    }
                    else
                    {
                        lblResponse.Text = "Record Not updated.";
                    }
                }


                catch (Exception ex)
                {
                    lblResponse.Text = ex.Message;
                }

推荐答案

错误消息非常明确:用于索引数组的值不正确 - 它需要介于0和元素数减1之间。



猜测,没有选中的项目,所以teh索引返回-1并且你得到错误 - 但是如果不像你那样完全运行代码,我们无法分辨。我们不能这样做!



所以,它取决于你。

幸运的是,你有一个可用的工具你将帮助你找出发生了什么:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
The error message is pretty explicit: the value you are using to index the array is not correct - it needs to be between 0 and the number of elements minus one.

At a guess, there is no selected item, so teh index returns -1 and you get the error - but without running your code exactly as you do, we can't tell. And we can't do that!

So, its going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


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

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