2

I'm getting some text from a database, so I want to view them in a TextBox object. The text in the DB has \n character to represent a new line, but when I bind the text to the TextBox, the \n still appear without being transformed to new lines!

I've tried the "\r\n" instead with no results!

What's wrong!

sarvesh
  • 2,743
  • 1
  • 20
  • 30

1 Answers1

3

If you look at the string in the debugger, I'm willing to bet that it has an \\n in it, not an \n. \\n will be displayed as \n, but \n will be displayed as a newline

string s = "Hello\\nWorld";  // Displays 'Hello\nWorld'

s = "Hello\nWorld";          // Displays 'Hello
                             //           World'
Phil
  • 6,561
  • 4
  • 44
  • 69
  • I don't see the difference with "\n" vs. "\n". Typo or something? – Carls Jr. Apr 14 '11 at 02:07
  • Yeah, the formatting automatically translated my "\\n" to an "\n" for some reason. Fixed. – Phil Apr 14 '11 at 02:11
  • The double-slash is .NET's way of representing the \ character, whereas the \n is .NET's way of representing the newline character. So if a user entered the text "\n" into the database, the database actually thinks he/she meant to enter a \ character with an n character. The database won't recognize it as a newline and will instead interpret it as "\\n". – Phil Apr 14 '11 at 02:13
  • How could I stop that transformation. I want to have a new line. – Hashem AL-Rifai Apr 14 '11 at 02:15
  • Depends on the software you're using to edit the database. What software are you using? Can you hold down shift and press enter? Sometimes that does it. – Phil Apr 14 '11 at 02:16
  • Or you could explicitly do a string.Replace("\\n", "\n"), but that would prevent your users from being able to enter text the way they want. Like if they entered text such as "prev\next". – Phil Apr 14 '11 at 02:18
  • There is NO software, There is a stored procedure that append some text with others putting '\n' between them. And the ADO.NET bind them to a TextBox in a C# WinForm, Thanks for your help @Phil – Hashem AL-Rifai Apr 14 '11 at 02:20
  • OK, I'll Try to use the Replace solution now, Thanks again – Hashem AL-Rifai Apr 14 '11 at 02:21
  • Worked Perfect ;) Thanks @Phil – Hashem AL-Rifai Apr 14 '11 at 02:22