Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

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();
        }
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

Notice that when the check id changes, the new file gets created (or that is my intent).

So, one file per unique check id.
Avatar of dstanley9
dstanley9

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?
>>>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.  :)
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial