Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# file DataRow
I have an application that converts .xlsx files into .txt files. On one of my files, there are multiple agencies I need to separate and write the similar ones to their own file. For example, There is an agency 55 and agency 99. There are several entries of each and not necessarily in order. I need a file for each agency with only entries in each file that have the correlating agency number.
 
Now the kicker is I won't know what the agency number is or how long it will be on any given row. I have figured out how to get the agency number for each row I have, but I need help on how to store those values and use them to write a file that has only those agency numbers in them.
 
Here is what I have so far to get the agency number I need and my initial code that just wrote to one file.
 
if (ddlType.SelectedValue == "authorize.net")
        {
            string filename = FileUpload1.FileName.ToString();
            FileUpload1.SaveAs("C:\\inetpub\\temp\\" + filename);
 
            if (filename != "")
            {
                FileInfo file = new FileInfo("C:\\inetpub\\temp\\" + filename);
 
                if (file.Exists)
                {
                    DataTable dt = new DataTable();
                    using (TextReader tr = File.OpenText("C:\\inetpub\\temp\\" + filename))
                    {
                            string line;
                            while ((line = tr.ReadLine()) != null)
                            {
                                string[] items = line.Split('\t');
                                if (dt.Columns.Count == 0)
                                {
                                    // Create the data columns for the data table based on the number of items 
                                    // on the first line of the file
                                    for (int i = 0; i < items.Length; i++)
                                    {
                                        dt.Columns.Add(new DataColumn("Column" + i, typeof(string)));
                                    }
                                }
                                dt.Rows.Add(items);
                            }
                            tr.Close();
                    }
                    ////Print out all the values 
                    string file99 = "C:\\inetpub\\temp\\99-Authorize.Net - " + date1 + " @ " + date2 + ".ach";
                    StreamWriter sw2 = new StreamWriter(file99, true);
 
                    foreach (DataRow dr in dt.Rows)
                    {
                        string agencyNumber;
                        int index = dr[12].ToString().IndexOf("|");
                        if (index > 0)
                        {
                            agencyNumber = dr[12].ToString().Substring(0, index);
                        }
                        if (dr[0].ToString() != "2")
                        {
                            if (dr[9].ToString() != "Total Amount")
                            {
                                sw2.Write("6                            ");
                                sw2.Write(dr[9].ToString().Replace(".", "").Replace(",", "").PadLeft(10, '0'));
                                sw2.Write((dr[12].ToString().Remove(0, index)).PadRight(15, ' '));
                                sw2.Write((dr[13].ToString() + " " + dr[14].ToString()).PadRight(40));
                                sw2.Write(sw2.NewLine);
                            }
                        }
                    }
                    sw2.Write("9");
                    sw2.Write(sw2.NewLine);
                    sw2.Close();
                    Session.Add("DatedFileName2", file99);
                }
                LinkButton1.Visible = true;
                LinkButton2.Visible = true;
            }
        }
Posted 25-Jun-12 7:40am
Edited 25-Jun-12 8:05am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Maybe I'm oversimplifying the issue here, but try the following.
 
In your last loop, try the following
////Print out all the values 
foreach (DataRow dr in dt.Rows)
{
    StringBuilder sb = new StringBuilder();
 
    string agencyNumber;
    int index = dr[12].ToString().IndexOf("|");
    if (index > 0)
    {
        agencyNumber = dr[12].ToString().Substring(0, index);
    }
    if (dr[0].ToString() != "2")
    {
        if (dr[9].ToString() != "Total Amount")
        {
            sb.Append("6                            ");
            sb.Append(dr[9].ToString().Replace(".", "").Replace(",", "").PadLeft(10, '0'));
            sb.Append((dr[12].ToString().Remove(0, index)).PadRight(15, ' '));
            sb.Append((dr[13].ToString() + " " + dr[14].ToString()).PadRight(40));
            sb.Append(sw2.NewLine);
        }
    }
    }
 
sb.Append("9");
sb.Append(sw2.NewLine);
                   
string file = "C:\\inetpub\\temp\\" + agencyNumber + "-Authorize.Net - " + date1 + " @ " + date2 + ".ach";
using (StreamWriter sw2 = new StreamWriter(file, true)
{
    sw2.WriteLine(sb.ToString());
}
 
So I'm using the foreach loop to decide which agency you're dealing with and changing the file name to write to each time. This minor change in logic will give you multiple files with the data specific to each agency. It probably isn't perfect, but a great start towards your final solution.
 
Hogan
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 565
1 Maciej Los 319
2 Richard MacCutchan 220
3 BillWoodruff 164
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 25 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100