Hey, everyone.
So, I am working on an application that takes csv data files and imports them into a dataset (dsImport), then I want to have it sort the data based on the values of a column in a row. Pretty straight forward in theory, but I am running into a problem. I'll lay it out as best I can.
I have two datasets (dsImport and dsExport). I need to dynamically create tables in dsExport, based on the values of a csv file and 'Market Number/Code". Basically, each market will have a datatable in dsExport based on their Name (stored in a table in dsImport, along with their number). Then I need to go through the records of the ImportData table of ds Import and copy the rows with the corresponding Markets into the correct datatable in dsExport.
I am getting stuck in the sort and copy part of that. It seems to work for the very first row, but then loops over that same record infinitely, rather than move to the next row. I am sure I am missing something simple, here, but I have been staring at this code for far too long today. Time for a fresh set of eyes!

CODE
//Data variables/arrays
string[] headers = new string[dsImport.Tables["ImportData"].Columns.Count];
string[] markets = new string[dsImport.Tables["Markets"].Rows.Count];
dsExport.Tables.Add("Summary");
for (int i = 0; i < dsImport.Tables["Markets"].Rows.Count; i++)
{
markets[i] = dsImport.Tables["Markets"].Rows[i]["Market"].ToString();
}
for (int i = 0; i < markets.Length; i++)
{
string delimiter = ","; //Set Delimiter
StreamReader srHeaders = new StreamReader(@"C:\Program Files\Company\Data Extractor\lib\Headers.ini"); //Path to Header file
string[] headerColumns = srHeaders.ReadLine().Split(delimiter.ToCharArray());
dsExport.Tables.Add(markets[i]);
//Create the Columns/Headers from the Header.ini CSV file
foreach (string col in headerColumns)
{
bool added = false;
int j = 0;
while (!added)
{
string columnName = col;
columnName = columnName.Replace("#", "");
columnName = columnName.Replace("'", "");
columnName = columnName.Replace("&", "");
if (!dsExport.Tables[markets[i]].Columns.Contains(columnName))
{
dsExport.Tables[markets[i]].Columns.Add(columnName);
added = true;
}
else
{
j++;
}
}
}
//Not Working
//Copy Select data from Import to Export tables based on Market Number
DataTable dt = dsImport.Tables["ImportData"];
for (int k = dt.Rows.Count - 1; k >= 0; k--) //loop backwards to prevent 'Modified Collection/Enumeration' errors
{
DataRow r = dt.Rows[i];
if ((string)r["Market Number"] == dsImport.Tables["Markets"].Rows[i]["Company Market Code"].ToString())
{
dsExport.Tables[markets[i]].LoadDataRow(r.ItemArray, true);
}
}
dt.AcceptChanges(); //commit changes to DataSet
This post has been edited by grayn0de: 2 Jul, 2009 - 08:39 PM