views:

191

answers:

2

Hello,

I am completely stumped. I have a program that pulls information stored in a database, assigns the database values to a corresponding class. The class has a print method that prints the values of it's fields. The users type in either an order id or order date range and then it finds those orders and prints them out to a text file. The problem I am having is that once in a awhile, depending on what id's and date's you pick, the text file will print out garbage like this. "〢Ⱒⰱ潋瑲刬瑵慨湮圬扥†′慍歲瑥㔬㈶‸⁗㈱琰⁨瑓敲瑥Ⱜ汁楳Ɒ䱉㘬㠰㌰" I am completely clueless here because when I debug it, all the values show correctly in the class, the print method, and result set. What could be the problem? Has anyone had this happen to them?

The OS is Windows Vista, the language I'm using is C#. I am using a StreamWriter class with a FileStream as a parameter.

This only happens with some orders. Like if I pick order 7 or order 1-6 it works fine, but if I pick 1-7, I get the weird language. ????

Thanks

+2  A: 

that looks like a bad conversion to/from Unicode, or a culture variable being set and not applied properly.

Stephen Wrighton
+4  A: 

Based on your comment what happens is that Notepad try to guess the encoding of the file. Look here for how it does so.

You should be explicit about the encoding of the file.

From the documentation:

StreamWriter defaults to using an instance of UTF8Encoding unless specified otherwise. This instance of UTF8Encoding is constructed without a Byte-Order Mark (BOM), so its GetPreamble method returns an empty byte array. To create a StreamWriter using UTF-8 encoding and a BOM, consider using a constructor that specifies encoding, such as StreamWriter(String, Boolean, Encoding).

Igal Serban