Search code examples
htmlasp.netsql-servervb.netline-breaks

Convert linebreaks from multiline textbox value stored in MS SQL Server to HTML


Ok, I'm storing the value of a multiline textbox in SQL Server in column [txt] of type nvarchar(1000).

The multiline text in this case is:

Dear sir,

Welcome to our platform.

Best, Us

After saving the results to MSSQL I run a query to select the values. When I copy the value of column [txt] directly from the query results in SQL Server Management Studio and paste this into Notepad++, I see the original text again. Also the loading the text again from the database and assigning that to the multiline textbox is working fine.

However, when in my code behind I assign the value of database column [txt] to a string variable (so not from Textbox to the variable):

Dim mailContent as String = row.txt

and then send this value in an email, the email recipient sees the text without the line breaks, so:

Dear sir, Welcome to our platform. Best, Us

I tried all different scenerios below:

Dim registryTA As New registryTableAdapters.registryTableAdapter
Dim registryDT As registry.registryDataTable = registryTA.GetDataByUserid(_userid)
Dim mailcontent As String

mailcontent = registryDT(0).invitetext.Replace(vbLf, "<br/>").Replace(vbCrLf, "<br/>")

mailContent = mailContent.Replace(Environment.NewLine, "<br/>")
mailContent = mailContent.Replace("\r\n", "<br/>")
mailContent = Server.HTMLEncode(mailContent)
mailContent = Server.HTMLDecode(mailContent)
mailContent = mailContent.Replace(vbCrlf, "<br/>")

Preferably I don't want to alter the way I store or retrieve the data in/from MSSQL (like here), so how can I convert the current value of [txt] to HTML format and get the linebreaks?


Solution

  • Seems like the text might only have \n in your content. Try replacing \n with </br> as well. Try this,

    mailContent = mailContent.Replace(vbLF, "<br/>").Replace(vbCrlf, "<br/>")