0

I have 5 DataTables that needs to be converted to TXT files. Instead of creating them separately, I thought I use a for loop. Here's my code:

StringBuilder sb = new StringBuilder();
for (int i = 1; i < 5; i++)
{
    DataTable dtFile1 = file1BLO.SelectFile1ForCSV()
    foreach (DataRow dr in dtFile1.Rows)
    {
        string[] fields = dr.ItemArray.Select(field => field.ToString()).ToArray();
        sb.AppendLine(string.Join("|", fields) + "|");
    }
    Response.ContentType = "application/text";
    Response.AddHeader("content-disposition", "attachment;filename=CAPRES-FILE1-"
                + DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".txt");
    Response.Output.Write(sb);
    Response.Flush();
    Response.End();
    sb.Clear();
}

I would want the iterator to be appended to the variable names and methods. Like this DataTable dtFile + i = file + i + BLO.SelectFile + i + ForCSV();

Thanks!

Requested Code for SelectFile1ForCSV()

public DataTable SelectFile1ForCSV()
{
    return file1DAO.SelectFile1ForCSV();
}

EDIT: I am sorry, it seems that I haven't provided enough details, and my question has caused confusion. Edited now.

16
  • Please show (the body of) SelectFile1ForCSV() Commented Jun 9, 2015 at 8:50
  • 3
    If there are five files, shouldn't it be i = 1 or i < 5? Commented Jun 9, 2015 at 8:50
  • 1
    @Robert Good catch! I'll edit it. Commented Jun 9, 2015 at 8:52
  • Response.End inside the loop? Commented Jun 9, 2015 at 8:52
  • 1
    Where's file1DAO declared? Stop putting 1 in all your method and variable names and simply pass i as a parameter to the point where you build a filename. Commented Jun 9, 2015 at 9:03

3 Answers 3

1

You cannot just append a number to a variable name at runtime to magically reference a new variable. What you should do instead is:

Define an an interface:

public interface IFileBLO
{
    DataTable SelectFileForCSV();
}

Have File1BLO, File2BLO etc all implement IFileBLO and fix the method names so that they are all SelectFileForCSV rather than SelectFile1ForCSV etc.

Add a lookup for reference these objects:

var bloList = new IFileBLO[]
{
    file1BLO, file2BLO, file3BLO, file4BLO, file5BLO
};

Finally, change your loop to:

for (int i = 0; i < 5; i++)
{
    var dtFile = bloList[i].SelectFileForCSV();
    foreach (var dr in dtFile.Rows)
    {
        ...
Sign up to request clarification or add additional context in comments.

Comments

1

There are not enough information in the question to know exactly what the problem is, so I'm just guessing here. Your problem is that you have five objects, that all have a method, and these method have different names. The methods return the same thing, though, DataTable, that can be used in a loop.

If that's the case then just take out of the loop that which is different, so that in the loop remains that which is identical. Something like this:

DataTable[] fiveTables =
{
    file1BLO.SelectFile1ForCSV(),
    file2BLO.SelectFile2ForCSV(),
    file3BLO.SelectFile3ForCSV(),
    file4BLO.SelectFile4ForCSV(),
    file5BLO.SelectFile5ForCSV()
}

for (int i = 1; i <= 5; i++)
{
    // Use fiveTables[i] for DataTable, and i for file name
}

4 Comments

This püart reads the data from the database. It misses writing .txt files (as the original question)
Also: i <=5 instead i<5
You are correct sir. I'll try this. Why haven't I thought of creating a DataTable array? Hehe. Thanks!
@Jeano This is the quick and dirty fix, but there is a more elegant (and proper) way. See answer from David Arno for that.
0

Use this:

Response.AddHeader("content-disposition", "attachment;filename=CAPRES-FILE"
            + i
            + "-"
            + DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".txt");

This will create a filename containing the value of i.

2 Comments

That's only part of the problem. The main problem is working with the right object in the loop. Other two answers address that.
Yes, and this answer answers the second part of the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.