4

I have a multiline text box. User will give brief details in that text box.

Like this:

user entering data in multiline textbox

After I saved this in SQL Server and retrieve it again, it is showing like the following screenshot:

without line break

What can I do to keep the line breaks? Do I need to encrypt something before I save the data in SQL Server?

Thanks.

This is my code:

<div id="Description_id" runat="server">
</div>

C# code:

string description = rd["job_description"].ToString();
Description_id.InnerText = description.Replace("\r\n", "<br/>");
1
  • 1
    You aren't showing how you're retrieving/viewing it. And you should look up what encrypt means before you ask if you should do it, because it's not applicable at all here. Commented Oct 4, 2016 at 21:47

2 Answers 2

8

When you get the text contents form a <textarea> or <input> the line breaks are stored as newline characters - basically \n for Unix based systems and \r\n for Windows.

You will need to convert these newline characters to <br /> tags, so that they are displayed properly in input and the line breaks are preserved. As an example, you could do something like this:

var outputHtml = textFromDb.Replace( "\r\n", "<br />" ).Replace( "\n", "<br />" );

This will ensure that the replace will be applied for both Unix and Windows newlines. Windows format replace comes first, because the Unix format is its substring.

Alternatively, you can also wrap the text from the database into a <pre>, which will preserve the whitespace characters (including newlines).

Sign up to request clarification or add additional context in comments.

4 Comments

string description = rd["job_description"].ToString().Replace("\r\n", "<br/>"); Description_id.InnerText = description; output is still same. Output This is a beautiful world,<br/><br/>at the same time it is not easy to survive. We should work hard.at the same time it is not easy to survive. We should work hard.<br/><br/><br/><br/><br/><br/>at the same time it is not easy to survive.
You need to set the InnerHtml property of the Description_id div element. InnerText automatically converts unsafe characters, so it will turn < into &lt; in the output.
I got it, oh my friend, the way you explain every line and everything fantastic. Thanks
Thank you very much, that is very nice of you :-) ! Happy coding!
0

I think your problem is not saving/retrieving the data from SQL server, but from then browser to the server. Can you please make sure that you are sending the text with all these line breaks? Use fiddler or the the browser console to check what are you actually sending.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.