Insert multi-line text to SQL table

I have multiple textarea fields that upon submit need to keep the carriage returns, tabs, spaces, etc. when it is saved to the SQL table.  The data type I have for the fields is 'ntext'.

What do I need to do so that the carriage returns and tabs, etc. are sent to the table and displayed properly when shown on the page.
Here are examples of the form field and the insert code:
<cftextarea name="strDetail"
    message="You must enter a detailed description of the request." />
<cfquery datasource="mydns" name="insertexample">
     insert into MY_table
          '#DateFormat(Now(), 'mm/dd/yy')# #TimeFormat(Now(), 'hh:mm:ss tt')#', 

Open in new window

Lee R Liddick JrReporting AnalystAsked:
Who is Participating?
Craig WagnerConnect With a Mentor Software ArchitectCommented:
When you open the table in SSMS you are executing a query (click on the little SQL button and you'll be able to edit the query) and displaying it in a grid. You'll never see the CR/LF rendered in that format, it will always appear as a single line.

When you display the queries "on the web" are you displaying them in a textarea or other control, or just writing them to the page? If the latter, you won't see the CR/LF there either, because HTML doesn't honor CR/LF characters. That's just the way HTML works. You'd have to put the text into a multi-line control (textarea) in order to see the effect.

The first thing you have to figure out is whether or not the CR/LF data is actually in the database. So far nothing you've done would seem to conclusively prove it isn't there.

Go into SSMS. Open a new query window. Write your query (select * from whatever). Before executing the query, go to Query > Results To > Results To Text. That will show you definitely whether the data in the database contains the CR/LF or not.

If the data were entered the way you showed above, as multiple lines in the textarea, you should see the results breaking across multiple lines. If you don't, then the CR/LF characters are not making it from the UI into the database, so the next step I'd do is use SQL Profiler to view the traffic between the application and database and see if the data is actually being passed through with the CR/LF characters intact.
Craig WagnerSoftware ArchitectCommented:
You shouldn't have to do anything special. I save text with CR/LF all the time and it is preserved in the database. Or are you talking about the soft wrapping that results in a multi-line text box? That soft wrapping doesn't cause CR/LF characters to be inserted into the text. In that case you're going to have to process the text and do the line-breaking in code before storing the data in the database.
Lee R Liddick JrReporting AnalystAuthor Commented:
When it is displayed on the page it lumps everything right after each other.  In the database field it also displays it as one long text string.

In the table field it displays like this:
Set up new stuff.  When the user activates one of these it will show one of the following: Something 1 Something 2 Something 3 Something 4.  Please do something.

When it should look like this in the table and/or when displayed from a query:

Set up new stuff.  When the user activates one of these it will show one of the following:
     Something 1
     Something 2
     Something 3
     Something 4

You wrote about processing the text and doing the line-breaking in code before storing the data in the database...if that is the case, how exactly is that done as I have never had experience in doing that before.  

I even upped the point value since this is more than a simple answer.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Craig WagnerSoftware ArchitectCommented:
You say, "In the table field it displays like this." Are you talking about in query results from SQL Server Management Studio? Are you using Grid or Text view for the results? Grid view will never show the line breaks, it always puts everything in a single row. If there are CR/LF characters in the string, Text view of the results would show you the actual formatting.
Lee R Liddick JrReporting AnalystAuthor Commented:
No query...just opening up the table in SSMS and looking in the column.  I have queries that are displayed on the web...when it displays on the web, all slammed together like I displayed above.
Craig WagnerConnect With a Mentor Software ArchitectCommented:
Here's an example of what I'm trying to illustrate.

I created a table with two columns, an integer and a varchar(50). I then ran the following SQL:

insert into junk
values(3, 'this is some text
that contains
line breaks')

As you can see from the attached images, when I view the results of a query against the row (i.e. select * from junk where primarykey = 3) I see a single row in grid view but I see the line breaks in text view.

Lee R Liddick JrReporting AnalystAuthor Commented:
I think I found what part of my problem is.  I did what you had above and then looked further into my code.  The form actually goes to a validation page for the end user to validate what they entered...if it's wrong, they return to the form, correct what they had and resubmit it.  If it's okay, then they just submit it.  But I have the form data saved as variables on this page and then submit to the database.  For example:

<cfparam name="variables.strDetail" type="string" default="#form.strDetail#" />
<!--- this is the code that displays the data from the form --->
    <td style="text-align:right; font-weight:bold; width:25%;">Description:</td>
    <td colspan="3">#variables.strDetail#</td>
<!--- then if end user hits submit, this is what gets sent to the database for insert --->
<cfinput type="hidden" name="strDetail" value="#strDetail#" />

Open in new window

Craig WagnerConnect With a Mentor Software ArchitectCommented:
That HTML is definitely not going to show line breaks, because you're basically just inserting the content as text into the HTML document, and as I said before, HTML doesn't honor CR/LF, if you want a line break in HTML you need to use <br>. If you want the validation/confirmation page to show the line breaks, you're going to have to do a substitution on the string to replace any CR and/or LF with <br> to get it to display properly. Either that or put the data into a disabled textarea.
Lee R Liddick JrReporting AnalystAuthor Commented:
I did try to put just that field into a disabled textarea box but I couldn't get the data to come back right if the end user chose to go back to the form instead of submitting.  You know of any other type of validation I can use.  I found this one off the internet but it seems to not be doing what I need.  Any suggestions on that?
Lee R Liddick JrReporting AnalystAuthor Commented:
Is this happening because it is a flash form?  I had to go out to the internet to figure out how to write the code to even get the CR's in.  I got it partly working but there is still something that is not right.  
Lee R Liddick JrReporting AnalystAuthor Commented:
I've tried CR, chr(10, and char(10) to convert the CR's to <br />'s and none of that is working...what is it supposed to be?  This is very frustrating.
Craig WagnerSoftware ArchitectCommented:
I put quite a bit of effort into putting together examples showing that the carriage returns are being stored and retrieved from the database (which was the first part of the question).

The thread then morphed into how to convert the carriage returns into HTML line break tags, which I could not help with because I do not know ColdFusion (this wasn't tagged as a ColdFusion question to begin with).

I think at least part of the points should be awarded for putting the OP on the right track.
Lee R Liddick JrReporting AnalystAuthor Commented:
I have no problem with that Craig...I would have just awarded the points without posting the delete but after my last three posts with no response, I figured I would get your response with the delete.  I appreciate all the assistance with the beginning part of this as it was initially thought it was a SQL issue as to why it wasn't putting the CR's in.  Thanks again and I will be posting the points here shortly.  Pool guys are here now...thanks again.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.