[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

something obvious? TextWriter keeps closing

TextWriter keeps closing

        private void worker820()
        {

            SqlConnection oCN = null;
            SqlCommand oCM = null;

            oCN = new SqlConnection(CONN_STRING);
            oCM = new SqlCommand("usp_Gather820ExportInfoByCheck", oCN);
            oCM.CommandType = CommandType.StoredProcedure;
            oCM.CommandTimeout = 3200;
            oCN.Open();
            SqlDataReader runInfo = oCM.ExecuteReader(CommandBehavior.CloseConnection);

            double paid_amt = 0;
            double billed_amt = 0;

            bool header_already_written = false;
            FileStream fs = null;
            StreamWriter w = null;
            string temp_checkid = "--";
            string temp_checkid2 = "--";


            while (runInfo.Read())
            {
               
                if (temp_checkid != runInfo["checkid"].ToString().Trim())
                {
                    if (w != null)
                    {
                        w.Flush();
                        w.Close();
                        fs.Close();                        
                    }
                    else
                    {
                        temp_checkid = temp_checkid2;
                        DateTime checkDate = (DateTime)runInfo["checkdate"];
                        string checkDateText = checkDate.ToString("yyyyMMdd");
                        string temp_scac = runInfo["scac"].ToString();                        
                        fs = new FileStream("C:\\gen_out." + GenUniqueCode() + "_820_" + runInfo["checkid"].ToString().Trim() + "_" + checkDateText + ".txt", FileMode.Create);
                        w = new StreamWriter(fs, Encoding.UTF8);
                        header_already_written = false;
                    }
                }

                if (header_already_written == false)
                {
                    //Header                        
                    w.WriteLine("HDR" + runInfo["tpname"].ToString() + "Trnsprt");

                    //Check
                    w.WriteLine("CHECK" + DateTime.Parse(runInfo["checkdate"].ToString()).ToShortDateString() + runInfo["checkamt"].ToString().PadLeft(12) + runInfo["checknum"].ToString().PadLeft(10));

                    header_already_written = true;
                }

                paid_amt = Double.Parse(runInfo["paidamt"].ToString());
                billed_amt = Double.Parse(runInfo["billamt"].ToString());

                //Carrier
                w.WriteLine("CARRIER" + runInfo["scac"].ToString().PadRight(6) + runInfo["carriername"].ToString().PadRight(20) + runInfo["addr1"].ToString().PadRight(20) + runInfo["city"].ToString().PadRight(15) + runInfo["state"].ToString() + runInfo["zipcode"].ToString());

                //Client
                w.WriteLine("CLIENT" + runInfo["clientid"].ToString().PadRight(6) + runInfo["clientname"].ToString().PadRight(20) + runInfo["clientaddr1"].ToString().PadRight(20) + runInfo["clientcity"].ToString().PadRight(15) + runInfo["clientstate"].ToString() + runInfo["clientzip"].ToString());
               
                temp_checkid = runInfo["checkid"].ToString().Trim();
            }

            w.Flush();
            w.Close();
            fs.Close();

            runInfo.Close();
            oCN.Close();
        }
0
Tom Knowlton
Asked:
Tom Knowlton
  • 2
  • 2
1 Solution
 
Tom KnowltonWeb developerAuthor Commented:
Notice that when the check id changes, the new file gets created (or that is my intent).

So, one file per unique check id.
0
 
dstanley9Commented:
It looks like you're closing the writer and filestream then trying to write to them again.  It's hard to follow your logic, but that's probably the case.

This block in particular looks suspicious:

                    if (w != null)
                    {
                        w.Flush();
                        w.Close();
                        fs.Close();                        
                    }
                    else
                    {
                        temp_checkid = temp_checkid2;
                        DateTime checkDate = (DateTime)runInfo["checkdate"];
                        string checkDateText = checkDate.ToString("yyyyMMdd");
                        string temp_scac = runInfo["scac"].ToString();                        
                        fs = new FileStream("C:\\gen_out." + GenUniqueCode() + "_820_" + runInfo["checkid"].ToString().Trim() + "_" + checkDateText + ".txt", FileMode.Create);
                        w = new StreamWriter(fs, Encoding.UTF8);
                        header_already_written = false;
                    }

If w is not null, then it is closed, but it never gets re-opened.   So the following block:


               if (header_already_written == false)
                {
                    //Header                    
                    w.WriteLine("HDR" + runInfo["tpname"].ToString() + "Trnsprt");

                    //Check
                    w.WriteLine("CHECK" + DateTime.Parse(runInfo["checkdate"].ToString()).ToShortDateString() + runInfo["checkamt"].ToString().PadLeft(12) + runInfo["checknum"].ToString().PadLeft(10));

                    header_already_written = true;
                }

Will try to write to a closed stream.

Do you need to create the new stream right after you close the old one?
0
 
Tom KnowltonWeb developerAuthor Commented:
>>>Do you need to create the new stream right after you close the old one?

Yes...and I don't feel very good about how I am doing it.  Is there a better way?  I mean...I guess it is obvious that there is a better way.  :)
0
 
dstanley9Commented:
try this:

                if (temp_checkid != runInfo["checkid"].ToString().Trim())
                {
                    if (w != null)
                    {
                        w.Flush();
                        w.Close();
                        fs.Close();                        
                    }
                    temp_checkid = temp_checkid2;
                    DateTime checkDate = (DateTime)runInfo["checkdate"];
                    string checkDateText = checkDate.ToString("yyyyMMdd");
                    string temp_scac = runInfo["scac"].ToString();                        
                    fs = new FileStream("C:\\gen_out." + GenUniqueCode() + "_820_" + runInfo["checkid"].ToString().Trim() + "_" + checkDateText + ".txt", FileMode.Create);
                    w = new StreamWriter(fs, Encoding.UTF8);
                    header_already_written = false;
                }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now