Question

I currently have

<textarea id="Commentary" rows="5" cols="40" runat="server"></textarea>

Then in the event

insertCommand.Parameters["@Commentary"].Value = "<pre>" + Commentary.Value + "</pre>";

If I entered into the HTML input box, for example

  • new line 1
  • new line 2
  • New line 3

It puts that into the SQL field as

 - new line 1  - new line 2  - new line 3

How do I keep things like line breaks? I will be displaying the data in a table, so I want to keep things such as line breaks.

Thanks,

Was it helpful?

Solution

It does not changes formatting. All required symbols are there. But if you are looking at it in MS Sql Server Management studio it won't show you formatting. At the same time you should be able to select it from database later and put on your page. And formatting will be kept.

OTHER TIPS

Besides this, you may also run into html encoding issues.
So you may wanna check the HttpUtility.HtmlEncode class.
Though this does not encode whitespace, you can do a simple String.Replace on top of the encoding like this:

HttpUtility.HtmlEncode(Commentary.Value).Replace("\n", "<br/>")

you can use this code

var reader = new StringReader(Commentary.Text);

string line;
while(null != (line = reader.ReadLine()) 
{
 ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top