0

This is my problem.

A user can enter text into a text area in the browser. Which is then emailed out to users. What I want to know is that how do I handle carriage return? If I enter \r\n for carriage return, the email (which is a plain text email) has actual \r\n in it.

In other words:

On the SQL server end

Case 1: if I do this before the email gets sent (notice the line break after line 1)

update emails
set
body='line 1
line 2'
where
id=100

the email goes out correctly

Case 2:

 update emails
 set
 body='line 1'+char(13) + char(10) +'line 2'
 where
 id=100

This email also goes out correctly

Case 3: However if I do this

update emails
set
body='line 1 \r\n line 2',
where
id=100

the email would have the actual text \r\n in it.

How do I simulate case 1/2 through c# ?

developer747
  • 15,419
  • 26
  • 93
  • 147
  • 7
    Why in the world would you use UrlDecode? Are you decoding a URL? – RobIII May 31 '13 at 23:26
  • You confused me when you said "replacing \r\n with enter". When you press enter, \r\n is generated. – Kendall Frey May 31 '13 at 23:28
  • As others have said, what are you expecting to get as a result? `\n` and/or `\r` IS a carriage return. – gunr2171 May 31 '13 at 23:29
  • 2
    Can you explain better what you see in the emails? Do you literally see the text \r\n? If so, the problem isn't related to UrlDecode. – Kendall Frey May 31 '13 at 23:33
  • Why do you need to remove `%20`? Are you sending form data via `GET` rather than `POST`? – Yuck May 31 '13 at 23:40
  • You're asking how to insert a carriage return into a string, but all the information you've given us indicates that it should be putting them in correctly. – Kendall Frey May 31 '13 at 23:42
  • Sorry guys, looks like I didn't explain correctly the first time. I edited my question, hopefully its clearer now. – developer747 May 31 '13 at 23:48
  • How does the \r\n get into SQL in the first place? What exactly do you mean by "If I enter \r\n for carriage return"? – Kendall Frey Jun 01 '13 at 00:02
  • This question is very unclear even after edits. Based on the information provided it's very unlikely to ever help future visitors. As it is I'm voting close this as **too localized**. – Yuck Jun 01 '13 at 01:40

2 Answers2

7

SQL literals (at least those in SQL Server) do not support such escape sequences (although you can just hit enter within the string literal so that it spans multiple lines). See this answer for some alternatives if writing it as an SQL string is a requirement.

If running the SQL programmatically from C#, use parameters which will handle this just fine:

sqlCommand.CommandText = "update emails set body=@body where id=@id"
sqlCommand.Parameters.AddWithValue("@body", "line 1 \r\n line2");

Note that the handling of the string literal (and conversion of the \r and \n character escape sequences) happens in C# and the value (with CR and LF characters) is passed to SQL.

If the above didn't address the problem, keep reading.


4.10.13 The textarea element:

For historical reasons, the element's value is normalised in three different ways for three different purposes. The raw value is the value as it was originally set. It is not normalized. The API value is the value used in the value IDL attribute. It is normalized so that line breaks use "LF" (U+000A) characters. Finally, there is the form submission value. [Upon form submission the textarea] is normalized so that line breaks use U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pairs, and in addition, if necessary given the element's wrap attribute, additional line breaks are inserted to wrap the text at the given width.

Note that CR and LF represent characters and not the two-character sequence of \ followed by either the r or n characters - this form is often found in string literals. If it appears as such then something is doing the incorrect conversion and putting (or leaving) the \ there. Or, perhaps there is some misguided "add slashes" hack somewhere?

As pointed out, while URL decode is likely wrong, it won't directly do this conversion. However, if the conversion happened previously before being "URL Encoded", then it will (correctly) decode to (incorrect) values.

In either case, it's a bug. So find out where the incorrect data conversion is introduced and fix it (attach a debugger and/or monitor the network traffic for clues) - the required information to isolate where is simply not present in the post.

Community
  • 1
  • 1
user2246674
  • 7,621
  • 25
  • 28
2

Use whatever c#'s string replace method is to replace "\\r\\n" with "\r\n" and that should fix it.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Johm Don
  • 609
  • 2
  • 5
  • 15