As we know in our day to day life how important 'Flush' is :),But that teaches us same thing in our programming life as well
Let me share a scenario where i faced the issue,In our day to day life of deadlines,Sometimes we forget to write the code which is actually needed there,You achieved the best with your code but you do not achieve the better sometimes
The Scenario :
I before few days created export to CSV and export functionality of my one of the list (Grid),It was importing the data very well,But on the close examination of data i found out that its inconsistent data,At some point of time it stopped writing the data and from that point onward there were no data in exported file.
Following is the code.
HSSFRow row = wsheet.GetRow(i); for (short j = 0; j < row.LastCellNum; j++) { HSSFCell cell = row.GetCell(j); string strValue = string.Format("\"{0}\",", Convert.ToString(cell.RichStringCellValue.String)); StreamWriter.Write(strValue); } StreamWriter.WriteLine(); StreamWriter.Flush();
If you see above code you do not find any issue,Same happened with me,I tried debugging it over and over,logging the operation but surprising thing was in log each and everything was coming,And i end up finding the issue was below
Issue The mistake i did was i was not clearing our StreamWriter from memory by flushing the memory and hence it was creating issue,One single statement introduced and woooo...issue got resolved like below
HSSFRow row = wsheet.GetRow(i); for (short j = 0; j < row.LastCellNum; j++) { HSSFCell cell = row.GetCell(j); string strValue = string.Format("\"{0}\",", Convert.ToString(cell.RichStringCellValue.String)); StreamWriter.Write(strValue); StreamWriter.Flush(); } StreamWriter.WriteLine(); StreamWriter.Flush();
Everything started working as it should be,Simple but very important to follow the theories.
Comments
Post a Comment