I don't think the DataReader is the problem here. If you look at the data in the reader itself, you should see that your data with the comma is still a single field. In other words, if you print the data to the screen instead of to the file, you should see that the comma stays inside of the data and doesn't cause the field to split into two.
However, if you look at the logic you're using to create the dataLine value, that appears to be where the problem is.
If you have a row with Field1="sarah", Field2="1, fieldValue", and Field3="parra", your dataLine string is going to end up like this: "sarah, 1, fieldValue, parra". There is no distinction between a comma that's part of the data, and one that's the delimiter.
You need to surround your field values with quotes so Excel can tell the difference. Change this line:
dataLine.Append(dr[fieldCount]);
to this:
dataLine.Append("\"" + dr[fieldCount] + "\"");
Thanks, Sarah
Please Mark as Answer if this answers your question, or Unmark as Answer if it is marked and you feel it is not answered.
|