New lines unexpectedly slipped away in C # / ASP.NET 1.1 code

Can someone explain to me why my code is:

string messageBody = "abc\n" + stringFromDatabaseProcedure;

      

where valueFromDatabaseProcedure is not a value from SQL database entered as

'line1\nline2'

      

results in the line:

"abc\nline1\\nline2"

      

This caused me to scratch my head a little.

I am using ASP.NET 1.1.

To clarify,

I am creating a string that I need to enter the body of an email message on a form submit. I mention ASP.NET 1.1 as I am not getting the same result with .NET 2.0 in a console application.

All I do is add lines, and when I look at the messageBody line I see that it was escaping the value line.

Update What doesn't help me at all is that Outlook doesn't show \ n in the text email correctly (unless you reply to it). The online email viewer (even Exchange webmail) shows \ n as a newline, as it should.

+1


source to share


5 answers


I just did a quick test against the NorthwindDb benchmark and put some unwanted data with \ n in the middle. Then I queried the data back using ADO.NET straight, and what you know, it actually speeds up the backslash for you automatically. It has nothing to do with n, it just sees the backslash and escapes it for you. In fact, I also put this in the db: foo panel, and when it came back to C # it was foo \ bar, it escaped for me too. What I mean is that it tries to keep the data as is on the SQL side, so it avoids what it thinks needs to be escaped. I haven't found a setting yet to turn this off, but if I do, I'll let you know ...



+1


source


ASP.NET uses <br />

to create strings. \n

will work with console or Windows Forms applications. Are you displaying it on a web page?

Method # 1

string value = "line1<br />line2";
string messageBody = "abc<br />" + value;

      

If that doesn't work, try:

string value = "line1<br>line2";
string messageBody = "abc<br>" + value;

      



Method # 2

Use System.Environment.NewLine :

 string value = "line1"+ System.Environment.NewLine + "line2";
 string messageBody = "abc" System.Environment.NewLine + value;

      

One of these methods is guaranteed to work. If you are outputting a string to a webpage (or email, or submit a form), you will have to use one of the ways I mentioned. \n

will never work there.

0


source


You need to set the clock and see exactly where your database result string gets double escaped.

Adding two lines together will never double the jump lines, so this either happens before or after.

0


source


When I get a string from the database, .NET will elude it automatically. However, a small @ symbol is appended to the line that I didn't notice.

So he didn't seem to be saved from my "about to go on vacation" inside.

So when an unescaped \ n was added to a string (since the entire string is no longer escaped), it will remove the @ and show the database portion of the string that was able to escape.

Yes, it was just an illusion.

Perhaps this holiday is overdue.

Thanks for your input.

0


source


If the actual string stored in the database (space added for emphasis) is "l i ne 1 \ nl i ne 2" then everything stored there probably has an error. But assuming it's the exact line, the line "abc \ nline1 \ nline2" is what happens when you look at the line to be printed as "abc
line1 \ nline2" in the debugger, which eludes it (this is a convenience, allowing you to copy-paste from the debugger directly into the code without errors).

Short answer: .NET doesn't escape the line, your debugger. Code that writes literal "\ n" in the database has an error.

0


source







All Articles