Stuart Elcocks
Stuart Elcocks

Reputation:

c# returning a string from sql 2005

Ok,

I have a string in a sql table like this

hello /r/n this is a test /r/n new line.

When i retrieve this string using c# for a textbox using multiline i expect the escape chars to be newlines.

But they are not and what happens is i get the line exactly as it is above.

It seems that the string returned from the table is taken as literal but i want the newlines!

How do i get this to work?

Thanks in advance..

Upvotes: 1

Views: 1073

Answers (6)

Jon Skeet
Jon Skeet

Reputation: 1500535

Things like "\n" (not "/n" by the way) are escape characters in programming languages, not inherently in text. (In particular, they're programming-language dependent.)

If you want to make

hello\r\nthis is a test\r\nnew line

format as

hello
this is a test
new line

you'll need to do the parsing yourself, replacing "\r" with carriage return, "\n" with newline etc, handling "\" as a literal backslash etc. I've typically found that a simple parser which just remembers whether or not the previous character was a backslash is good enough for most purposes. Something like this:

static string Unescape(string text)
{
    StringBuilder builder = new StringBuilder(text.Length);
    bool escaping = false;
    foreach (char c in text)
    {
        if (escaping)
        {
           // We're not handling \uxxxx etc
           escaping = false;
           switch(c)
           {
               case 'r': builder.Append('\r'); break;
               case 'n': builder.Append('\n'); break;
               case 't': builder.Append('\t'); break;
               case '\\': builder.Append('\\'); break;
               default:
                   throw new ArgumentException("Unhandled escape: " + c);
           }
        }
        else
        {
           if (c == '\\')
           {
               escaping = true;
           }
           else
           {
               builder.Append(c);
           }
        }
    }
    if (escaping)
    {
        throw new ArgumentException("Unterminated escape sequence");
    }
    return builder.ToString();
}

There are more efficient ways of doing it (skipping from backslash to backslash and appending whole substrings of non-escaped text, basically) but this is simple.

Upvotes: 1

stevehipwell
stevehipwell

Reputation: 57488

I think the issue here is that the string as seen by the user is

hello \r\n this is a test \r\n new line.

while the C# code sees it as

"hello \\r\\n this is a test \\r\\n new line."

A much simpler solution would be the following

s = Regex.Replace(s, @"\\\\", @"\");

which replaces @"\\" with @"\". Don't be confused by the regex match pattern, the \ has to be escaped in the match but not the replacement.

Upvotes: 0

Cerebrus
Cerebrus

Reputation: 25775

I do not see why a Parser is required in this instance, given that the language in question is C# and I assume that the OS is 32-bit Windows which treats \r\n as the NewLine character.

The following simple code (Windows app) works for me:

string s = "hello \r\n this is a test \r\n new line.";

// Set Multiline to True.
textBox1.Multiline = true;

// Expand the height of the textbox to be able to view the full text.
textBox1.Height = 100;
textBox1.Text = s;

The textbox shows text as:

hello
 this is a test 
 new line.

Upvotes: 0

Konstantin Tarkus
Konstantin Tarkus

Reputation: 38378

Looks like you need to use System.Web.HttpUtility.HtmlDecode on the string before putting it in the TextBox.

 textBox.Value = HttpUtility.HtmlDecode(myString);

Upvotes: 0

Jared Harley
Jared Harley

Reputation: 8337

I had a similar problem with my SQLite database the other day. Users are able to enter multi-line text into a textbox, and when it's inserted into the database, it goes in like:

"This is \r\n a multiline test"

It was coming out of System.Data.SQLite as:

"This is \\r\\n a multiline test"

I ended up using:

String.Replace("\\r","\r").Replace("\\n","\n") 

to replace each of the escaped characters, which formatted the text properly when displayed in either a multi-line textbox or label.

Upvotes: 0

Nick Whaley
Nick Whaley

Reputation: 2799

/ is not the escape character, \ is. What you need is:

hello \r\n this is a test \r\n new line.

Upvotes: 3

Related Questions