Tom Knowlton
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_Gather820E xportInfoB yCheck", oCN);
oCM.CommandType = CommandType.StoredProcedur e;
oCM.CommandTimeout = 3200;
oCN.Open();
SqlDataReader runInfo = oCM.ExecuteReader(CommandB ehavior.Cl oseConnect ion);
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"].ToStrin g().Trim() )
{
if (w != null)
{
w.Flush();
w.Close();
fs.Close();
}
else
{
temp_checkid = temp_checkid2;
DateTime checkDate = (DateTime)runInfo["checkda te"];
string checkDateText = checkDate.ToString("yyyyMM dd");
string temp_scac = runInfo["scac"].ToString() ;
fs = new FileStream("C:\\gen_out." + GenUniqueCode() + "_820_" + runInfo["checkid"].ToStrin g().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["ch eckdate"]. ToString() ).ToShortD ateString( ) + runInfo["checkamt"].ToStri ng().PadLe ft(12) + runInfo["checknum"].ToStri ng().PadLe ft(10));
header_already_written = true;
}
paid_amt = Double.Parse(runInfo["paid amt"].ToSt ring());
billed_amt = Double.Parse(runInfo["bill amt"].ToSt ring());
//Carrier
w.WriteLine("CARRIER" + runInfo["scac"].ToString() .PadRight( 6) + runInfo["carriername"].ToS tring().Pa dRight(20) + runInfo["addr1"].ToString( ).PadRight (20) + runInfo["city"].ToString() .PadRight( 15) + runInfo["state"].ToString( ) + runInfo["zipcode"].ToStrin g());
//Client
w.WriteLine("CLIENT" + runInfo["clientid"].ToStri ng().PadRi ght(6) + runInfo["clientname"].ToSt ring().Pad Right(20) + runInfo["clientaddr1"].ToS tring().Pa dRight(20) + runInfo["clientcity"].ToSt ring().Pad Right(15) + runInfo["clientstate"].ToS tring() + runInfo["clientzip"].ToStr ing());
temp_checkid = runInfo["checkid"].ToStrin g().Trim() ;
}
w.Flush();
w.Close();
fs.Close();
runInfo.Close();
oCN.Close();
}
private void worker820()
{
SqlConnection oCN = null;
SqlCommand oCM = null;
oCN = new SqlConnection(CONN_STRING)
oCM = new SqlCommand("usp_Gather820E
oCM.CommandType = CommandType.StoredProcedur
oCM.CommandTimeout = 3200;
oCN.Open();
SqlDataReader runInfo = oCM.ExecuteReader(CommandB
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"].ToStrin
{
if (w != null)
{
w.Flush();
w.Close();
fs.Close();
}
else
{
temp_checkid = temp_checkid2;
DateTime checkDate = (DateTime)runInfo["checkda
string checkDateText = checkDate.ToString("yyyyMM
string temp_scac = runInfo["scac"].ToString()
fs = new FileStream("C:\\gen_out." + GenUniqueCode() + "_820_" + runInfo["checkid"].ToStrin
w = new StreamWriter(fs, Encoding.UTF8);
header_already_written = false;
}
}
if (header_already_written == false)
{
//Header
w.WriteLine("HDR" + runInfo["tpname"].ToString
//Check
w.WriteLine("CHECK" + DateTime.Parse(runInfo["ch
header_already_written = true;
}
paid_amt = Double.Parse(runInfo["paid
billed_amt = Double.Parse(runInfo["bill
//Carrier
w.WriteLine("CARRIER" + runInfo["scac"].ToString()
//Client
w.WriteLine("CLIENT" + runInfo["clientid"].ToStri
temp_checkid = runInfo["checkid"].ToStrin
}
w.Flush();
w.Close();
fs.Close();
runInfo.Close();
oCN.Close();
}
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["checkda te"];
string checkDateText = checkDate.ToString("yyyyMM dd");
string temp_scac = runInfo["scac"].ToString() ;
fs = new FileStream("C:\\gen_out." + GenUniqueCode() + "_820_" + runInfo["checkid"].ToStrin g().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["ch eckdate"]. ToString() ).ToShortD ateString( ) + runInfo["checkamt"].ToStri ng().PadLe ft(12) + runInfo["checknum"].ToStri ng().PadLe ft(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?
This block in particular looks suspicious:
if (w != null)
{
w.Flush();
w.Close();
fs.Close();
}
else
{
temp_checkid = temp_checkid2;
DateTime checkDate = (DateTime)runInfo["checkda
string checkDateText = checkDate.ToString("yyyyMM
string temp_scac = runInfo["scac"].ToString()
fs = new FileStream("C:\\gen_out." + GenUniqueCode() + "_820_" + runInfo["checkid"].ToStrin
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
//Check
w.WriteLine("CHECK" + DateTime.Parse(runInfo["ch
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?
ASKER
>>>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. :)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So, one file per unique check id.