Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Text with LineBreak from Multi-Line-TextBox to save in a database

I have PrivateMessage-System in my new Community-Page.

I have a Multiline-Textbox for the Text of a private message. I save this text in a string, this string in a ntext SQL-Coloumn. I read this text from the ntext SQL-Coloum in a string and show it in a label.

When I have 5 linebreaks with the "Enter"-Key in my Multi-line-textbox, the linebreaks will disappeared in the label.

I don't know where they gone lost and what I am doing wrong. any idea?

like image 458
PassionateDeveloper Avatar asked Feb 03 '11 07:02

PassionateDeveloper


People also ask

How do you store line breaks in database?

Insert SQL carriage return and line feed in a string In SQL Server, we can use the CHAR function with ASCII number code. We can use the following ASCII codes in SQL Server: Char(10) – New Line / Line Break. Char(13) – Carriage Return.

What is the use of multi line property of text box control?

A multiline text box allows you to display more than one line of text in the control.

What is multiline in textbox?

A multiline text box control is a large text box that can display several lines of text or accept this text from user input. Text boxes are often linked to select value lookups and detailed menus. You can place a multiline text box control within a section control.


3 Answers

You can solve this with CSS. Assign this CSS to your label:

white-space: pre-wrap;

like image 92
CyberHawk Avatar answered Sep 26 '22 17:09

CyberHawk


First of all is it actually saving the line breaks to your table? If so, what does it save them as, eg \r\n or CRLF or what?

Your label outputs html, so the only thing that will create a break is a <br /> tag. So you need to find and replace whatever is saved in the database:

Label1.Text = someDatabaseText.Replace("\r\n", "<br />");

Or even better, do the .Replace() before you save it to the database:

someDatabaseField = TextBox1.Text.Replace("\r\n", "<br />");
like image 25
JK. Avatar answered Sep 26 '22 17:09

JK.


this also works:

lbl_PostContent.Text = lbl_PostContent.Text.Replace(vbCrLf, "<br />")
like image 25
RpG Avatar answered Sep 24 '22 17:09

RpG