I am writing a C# application that will update the fields in a SQL Server database. The current algorithm I am testing simply pulls the data from a "State" field, stores each value in an ArrayList, capitalizes it, and then writes it back to the database. I am having a problem with logic.
I pull all of the values into the ArrayList and capitalize them. This is working fine. I now have an array with, for instance, 100 values (i.e., myArray[0]
- myArray[99]
). I then use a FOR
loop to write the values back to the database:
for (int i = 0; i <= (myArray.Count - 1); i++)
{
SqlCommand myCommand =
new SqlCommand("UPDATE myList SET State = '" + recordArray[i].ToString() +
"' WHERE uniqueID = '" + (i + 1) + "'", dbConnection);
myCommand.ExecuteNonQuery();
}
I am using "uniqueID" in the above example to place these values according to primary key. However, the problem is that the primary key is only nearly sequential; there are a few missing numbers in the sequence. Thus, even though I have exactly the number of values that I need, and they are in the correct order in the array to be pushed back out to the database, once I reach a lapse in the sequence, the rest of the data is placed in the wrong field. I know this is a lapse in my logic, but I am at a loss as to how I can ensure that every individual value is being placed correctly.
Thanks in advance for the help.