Answered by:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Question
-
I cant seem to figure this one out either!
int intCount = 0; foreach (string str in activeListOfFields) { foreach (string ztr in strColumnNames) { if (str == ztr) { activeColumns[intCount] = ztr; intCount++; } } }
"activeColumns" is a List<string> I am trying to match up all the filled in controls on a form to the fields in a database table and store the fields of the database table to "activeColumns" but I keep getting this error that states my index is out of range. As far as I can see in debugging my index is initially at 0 which is fine for a list no? Is it not that the first position in a list or an array is [0]?Friday, August 19, 2011 5:20 PM
Answers
-
Hi Vamp10988;
You created a string array and initialized it with intNum elements.
string[] strColumnNames = new string[intNum];
But intNum at the time you initialized it was set to 0. Therefore the array strColumnNames has zero elements and therefore can not hold anything. So trying to assign anything to it will fail with an index out of range. Change the value of intNum at the top of the code to a value as large as you will need so that it will have slots to insert data. This is also true for strDataTypes array.
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Proposed as answer by shanuapril Friday, August 19, 2011 9:45 PM
- Marked as answer by Vamp10988 Friday, August 19, 2011 9:56 PM
Friday, August 19, 2011 5:58 PM
All replies
-
Hi Vamp10988;
Place a break point on this line of code:
activeColumns[intCount] = ztr;
When the debugger stops on the line see what the value of intCount is and check the size of the List activeColumns. Also how do you initialize the List activeColumns, can you post that part of the code.
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".Friday, August 19, 2011 5:32 PM -
Hello Fernando,
both values are 0. The list size and the intcount.
I will post the code I have up untill the problem point.
public static void insertBuilder(string strTableName, string strConnectionString, DataSet dS, List<Control> ctrl) { //strFieldNames = new string[30]; int intNum = 0; int intNumOfActiveFields = 0; string[] strDataTypes = new string[intNum]; string[] strColumnNames = new string[intNum]; List<string> activeColumns = new List<string>(); List<Control> newCtrl = new List<Control>(); List<string> activeControlNames = new List<string>(); List<string> activeListOfFields = new List<string>(); OleDbConnection Launch = new OleDbConnection(); Launch.ConnectionString = strConnectionString; int x = 0; int intStringCount = 0; string strInsertStatement; //String builder is declared to start building the INSERT statement StringBuilder myStringBuilder = new StringBuilder("INSERT INTO "); myStringBuilder.Append(strTableName); myStringBuilder.Append(" ("); //DataTable table = GetTable(strTableName); foreach (DataTable dT in dS.Tables) { if (strTableName == dT.TableName) { intNum= dT.Columns.Count; strDataTypes = new string[intNum]; strColumnNames = new string[intNum]; for (int z = 0; z < intNum; z++) { strDataTypes[z] = dT.Columns[z].DataType.ToString(); strColumnNames[z] = dT.Columns[z].ColumnName; } } } foreach (Control cT in ctrl) {//DateTimePickers will never have a null value & Empty Indexes will have text value of "";?i think if (cT.Text != "") { newCtrl.Add(cT); activeControlNames.Add(cT.Name); activeListOfFields.Add(cT.Name); } } for (int i = 0; i < activeControlNames.Count; ++i) { string str = activeControlNames[i]; str = str.Remove(0, 3); activeListOfFields[i] = str; //MessageBox.Show(str); } int intCount = 0; foreach (string str in activeListOfFields) { foreach (string ztr in strColumnNames) { if (str == ztr) { activeColumns[intCount] = ztr; intCount++; } } }
Friday, August 19, 2011 5:43 PM -
Hi Vamp10988;
You created a string array and initialized it with intNum elements.
string[] strColumnNames = new string[intNum];
But intNum at the time you initialized it was set to 0. Therefore the array strColumnNames has zero elements and therefore can not hold anything. So trying to assign anything to it will fail with an index out of range. Change the value of intNum at the top of the code to a value as large as you will need so that it will have slots to insert data. This is also true for strDataTypes array.
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Proposed as answer by shanuapril Friday, August 19, 2011 9:45 PM
- Marked as answer by Vamp10988 Friday, August 19, 2011 9:56 PM
Friday, August 19, 2011 5:58 PM