C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,470 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,744 people online right now. Registration is fast and FREE... Join Now!




Multiple DataSets/DataTables manipulation

 

Multiple DataSets/DataTables manipulation, pulling rows from a DataTable in one DS to another...

grayn0de

2 Jul, 2009 - 08:23 PM
Post #1

New D.I.C Head
*

Joined: 2 Jul, 2009
Posts: 2

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! smile.gif

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

User is offlineProfile CardPM
+Quote Post


masteryee

RE: Multiple DataSets/DataTables Manipulation

5 Jul, 2009 - 07:06 PM
Post #2

D.I.C Regular
***

Joined: 16 May, 2009
Posts: 266



Thanked: 36 times
My Contributions
Try replacing the "i" index variable with "k":

DataRow r = dt.Rows[i]; =>
DataRow r = dt.Rows[k];

See if that fixes your problem.
User is offlineProfile CardPM
+Quote Post

grayn0de

RE: Multiple DataSets/DataTables Manipulation

6 Jul, 2009 - 05:45 AM
Post #3

New D.I.C Head
*

Joined: 2 Jul, 2009
Posts: 2

That was it! I knew it had to be something small I was overlooking.

Thanks a ton! biggrin.gif

This post has been edited by grayn0de: 6 Jul, 2009 - 05:46 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 02:44AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month