Click here to Skip to main content
15,886,590 members
Please Sign up or sign in to vote.
1.67/5 (8 votes)
See more:
I've used similar code before.. but this is stumping me right now. No matter what I try I'm getting the same error (Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index). I'm trying to pull data with the second query to add to my results table. The line that is throwing the error is: res_bom[doc_item.IndexOf(p)] = onbom;

           List<string> doc_item = new List<string>();
           List<int> doc_qty = new List<int>();
           List<int> doc_qty_rem = new List<int>();
           List<decimal> doc_minprice = new List<decimal>();
           List<decimal> doc_maxprice = new List<decimal>();
           List<string> res_bom = new List<string>();
             this.cimDocumentDetailTableAdapter.FillByDoc(this.dsTools.cimDocumentDetail, doc);
           foreach (DataRow dr in this.dsTools.cimDocumentDetail.Rows)
           {
               doc_item.Add(dr.ItemArray[1].ToString());
               doc_qty.Add(int.Parse(dr.ItemArray[2].ToString()));
               doc_qty_rem.Add(int.Parse(dr.ItemArray[2].ToString()));
               doc_minprice.Add(decimal.Parse(dr.ItemArray[9].ToString()));
               doc_maxprice.Add(decimal.Parse(dr.ItemArray[10].ToString()));
               res_bom.Add("");
               res_oocust.Add(0);
           }

{
               string q = @"
                       SELECT LTRIM(RTRIM(CPN_I)) as 'Part', 'Yes' AS Expr1    FROM   bm010115 AS bm010115_1
                       WHERE (CPN_I like '10-C%') and (CPN_I not like '10-C%C') ORDER BY 'PART'";
               DataTable data = ExecuteQuery("Tools", q);
               foreach (DataRow dr in data.Rows)
               {
                   string p = dr.ItemArray[0].ToString();
                   string onbom = dr.ItemArray[1].ToString();
                   try
                   {
                       res_bom[doc_item.IndexOf(p)] = onbom;
                   }
                   catch (SqlException ex)
                   {
                       MessageBox.Show(ex.Message);
                   }
               }
           }


Any help would be greatly appreciated
Posted
Updated 25-Sep-14 7:39am
v2
Comments
[no name] 25-Sep-14 13:49pm    
Well debug your code, find the index that is less than zero or greater than the number of elements in your array.
dae0c 25-Sep-14 14:03pm    
I've debugged, I just can't comprehend where the problem lies. Amateur coder at best here...

SS
Sergey Alexandrovich Kryukov 25-Sep-14 14:18pm    
Why, why showing bitmaps trying to ask or clarify questions? Isn't so difficult to present all information in text, which anyone can copy, perform text search, and so on?
Please, do this favor to everyone, avid it.
—SA
dae0c 25-Sep-14 14:19pm    
It's not a bitmap, and it's in text above. Just highlighting to show what I see in VS. But thank you very much for your input.
Sergey Alexandrovich Kryukov 25-Sep-14 15:53pm    
It is a bitmap. I mean what we see in SS. RDdgTEy.jpg is a bitmap. Functionally, there is no a text, there are pixels. I hope you understand what I mean. It's inconvenient to use, people cannot select text, copy, do search...
—SA

Isn't that obvious? If data.Rows has zero rows, the first statement in loop will throw this exception, because the element dr.ItemArray[0] does not exist. If data.Rows has only one row, the second statement in loop will throw this exception, because the element dr.ItemArray[1] does not exist.

As to "amateur coder at best", yes, the attempt to work with strings representing the data instead data itself, hard-coded strings and indices tells the tale… :-)

[EDIT]

You are give contradictory information in code, screenshot (why doing it at all?) and comment. In all other cases, do the same thing: put a break point at the assignment which throws the exception, examine an index, compare with the number of elements in the set being indexed. Then find out why index is beyond 0..N−1, where N is the number of elements.

—SA
 
Share this answer
 
v2
I can't tell you what is wrong, but I will say that I do not put functions inside my square brackets for this very purpose. Here is how I would debug this code. Put this in your "try" block.

int x = doc_item.IndexOf(p);
if (x >= 0 && x < res_bom.Count())
{
   res_bom[x] = onbom;
}
else
{
   MessageBox.Show("res_bom has " + res_bom.Count().ToString() + 
           " items and you are looking for number " + x.ToString();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900