swankyprogramming
swankyprogramming

Reputation: 107

Storing text in SQL with formatting (breaklines etc.)

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

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,

Upvotes: 0

Views: 6030

Answers (3)

Kash
Kash

Reputation: 9039

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/>")

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

you can use this code

var reader = new StringReader(Commentary.Text);

string line;
while(null != (line = reader.ReadLine()) 
{
 ...
}

Upvotes: 0

Viktor S.
Viktor S.

Reputation: 12815

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.

Upvotes: 3

Related Questions