1

I have a database with almost 2000 code examples that I've collected over the last 10 years that I want to be able to access with a textarea field so I can copy and/or update easily. I'm trying to migrate from PHP to C# on a new domain, but I'm having problems getting it to display correctly.

This is how it looks currently: http://nunyabiz.freeiz.com/csharp/index.html

This is how it's supposed to look: http://nunyabiz.freeiz.com/csharp/index2.html

This is the code that I'm using to display it: Code.Text = rs["Code"].ToString().Replace("\r\n", "\n");

The "\r\n" characters aren't getting recognized, so I'm guessing that I have to convert, encode or decode it, but I haven't had any luck trying to find something that will work. online.

Oleg Dok
  • 21,109
  • 4
  • 45
  • 54
Realto619
  • 301
  • 3
  • 13
  • 1
    duplicate http://stackoverflow.com/questions/3944410/textbox-with-new-line –  Jan 25 '12 at 05:16
  • Could it be that `rs["Code"]` contains the *literal* escape values seen? (That is a two-char sequence of \,r or \,n). Remember that "\r" and "\n" only work in *string literals*: not if the escapes are stored as such in the DB. (What is the result of a simple select from the DB CLI?) –  Jan 25 '12 at 05:27
  • It's quite possible that they could be the literal escape values. How would I convert them back to string literals? – Realto619 Jan 25 '12 at 05:55

3 Answers3

0

you can try Environment.NewLine for Linebreaks Environment.Newline

strYourCode = string.Format("Name: {0}  LineBreak {1}", yournameVariable, Environment.NewLine);
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
  • I'm not completely sure how to implement that with my code. I just tried: [code] Code.Text = rs["Code"].ToString().Replace("\r\n", Environment.NewLine);[/code] without any success... – Realto619 Jan 25 '12 at 05:29
0

Try to figure out WHAT line breaks you have in your code samples - there are many variants including html tag br, and then change it to what you need (i think it is Environment.Newline)

And check that your Code control is Multiline

Oleg Dok
  • 21,109
  • 4
  • 45
  • 54
  • the majority of them are "\r\n" but the replace function doesn't seem to be catching them – Realto619 Jan 25 '12 at 05:31
  • it is Multiline, btw: – Realto619 Jan 25 '12 at 05:33
  • I just tested the following and it works perfectly: String CodeToTest = "\r\n"; Code.Text = CodeToTest.Replace(Environment.NewLine, "\n"); However, it doesn't work when I try to replace it in the code that's stored in the database. It seems to not recognize "\r\n" as newline characters. Any idea how I can get it to recognize them appropriately? – Realto619 Jan 25 '12 at 05:42
0

Give this a try .Replace("\r", "").Replace("\n", "<br/>");

rsapru
  • 688
  • 14
  • 30