LONG
LONG

Reputation: 4620

StreamWriter did not write to file

Below is part of my code, cannot figure out why the text stream did not write to target file.

...
StringBuilder Religion = new StringBuilder();
...         
    if (Religion.Length != 0)
    {

        sw = new System.IO.StreamWriter(Dts.Variables["User::RawData"].Value.ToString() + "Religion.csv");
        sw.WriteLine(Religion);
        MessageBox.Show(Religion.ToString());
    }

I added the MessageBox.Show to help me check whether the StringBuilder Religion is empty or not, but it did have all the rows, and I have multiple code block like this for each of my data file, do not know why only for this, the result file is EMPTY...

Any help would be appreciated.

Upvotes: 1

Views: 92

Answers (1)

Ive
Ive

Reputation: 1331

Use using:

...
StringBuilder Religion = new StringBuilder();
...         
if (Religion.Length != 0)
{

    using (sw = new System.IO.StreamWriter(Dts.Variables["User::RawData"].Value.ToString() + "Religion.csv"))
    {
        sw.WriteLine(Religion);
    }
    MessageBox.Show(Religion.ToString());
}

Alternatively:

...
StringBuilder Religion = new StringBuilder();
...         
if (Religion.Length != 0)
{

    sw = new System.IO.StreamWriter(Dts.Variables["User::RawData"].Value.ToString() + "Religion.csv"))
    try
    {
        sw.WriteLine(Religion);
    }
    finally
    {
        sw.Close();
    }
    MessageBox.Show(Religion.ToString());
}

Upvotes: 2

Related Questions