oracle 如何解决索引超出范围。必须是非负的并且小于集合的大小。错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45514246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I resolve index was out of range. Must be non-negative and less than the size of the collection. Error
提问by Elay
I am getting following exceptions in my code
我的代码中出现以下异常
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
I could not understand why it is throwing that exception.
我不明白为什么它会抛出该异常。
Flow of program is something like this :
程序流程是这样的:
I have collection of number say 1 to 200. For each number in collections, it goes and run the below code. till 120, it did not throw the exception but for 121, its throwing the exception. And it does randomly, if run the program again, I might get same exception for 123.
我收集了 1 到 200 的数字。对于集合中的每个数字,它会运行以下代码。到120,它没有抛出异常,但对于121,它抛出了异常。它是随机的,如果再次运行该程序,我可能会得到 123 的相同异常。
What could be the reason? Is it something related with database connection? Is there any problem in my code? if so then should not it be throwing for the first iteration?
可能是什么原因?它与数据库连接有关吗?我的代码有问题吗?如果是这样,那么它不应该在第一次迭代中抛出吗?
What I have tried:
我尝试过的:
public DataTable GetGrade(DataTable dtReportTbl)
{
dtReportTbl.Columns.Add("Salary_Grade", typeof(System.String));
dtReportTbl.Columns.Add("InsertedDate", typeof(DateTime));
string query = "select Grade from student_grade where myTable in ('JMS','JSM') and student_Num in ({StudentNums}) order by student_Num";
?
try
{
using (OracleConnection con = new OracleConnection(connectionString))
{
List<string> storeNums = new List<string>();
?
foreach (DataRow row in dtReportTbl.Rows)
{
storeNums.Add(row["store"].ToString());
}
?
var cmd = new OracleCommand(query, con);
?
// extension method to get StudentNums as to achieve cmd.Parameters.Add(new OracleParameter("pStudentNums", studentNum)
cmd.AddArrayParametersOra(storeNums, "StudentNums");
?
var rdr = cmd.ExecuteReader();
List<string> salGradelist = new List<string>();
while (rdr.Read())
{
salGradelist.Add(rdr["Grade"].ToString());
}
?
int i = 0;
foreach (DataRow row in dtReportTbl.Rows)
{
row["Grade"] = salGradelist[i];
row["RecordInsertedDate"] = DateTime.Today;
i++;
}
}
}
catch (Exception ex)
{
throw;
}
?
return dtReportTbl;
}
采纳答案by Farhad Bagherlo
To find out, you need to look at the data in your app while it is running - and we can;t do that! So, its going to be up to you. Put a breakpoint on the first line in the method, 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.
要找出答案,您需要在应用程序运行时查看应用程序中的数据 - 而我们不能这样做!所以,这取决于你。在方法的第一行放置一个断点,然后通过调试器运行您的代码。然后查看您的代码和数据,并找出应该手动执行的操作。然后单步执行每一行,检查您期望发生的事情是否正是所做的事情。如果不是,那就是您遇到问题的时候,您可以回溯(或再次运行并更仔细地查看)以找出原因。
The most likely cause is that the number of rows returned from your query is not the same as the number of elements in your array.
最可能的原因是查询返回的行数与数组中的元素数不同。
int i = 0;
foreach (DataRow row in dtReportTbl.Rows)
{
... = salGradelist[i]; // <-- Exception here
...
i++;
}
If the query returns fewer rows than the number of elements in your array, then your code will just ignore the later elements.
如果查询返回的行数少于数组中的元素数,则您的代码将忽略后面的元素。
But if it returns more rows, then the variable i is going to go beyond the end of your array, and you'll get the "index out of bounds" exception.
但是如果它返回更多行,那么变量 i 将超出数组的末尾,并且您将得到“索引越界”异常。
You'll need to debug your code and your query to find out why the number of rows returned doesn't match the number of elements in your array.
您需要调试代码和查询以找出返回的行数与数组中的元素数不匹配的原因。