Solved

Insert multi-line text to SQL table

Posted on 2009-04-09
13
2,050 Views
Last Modified: 2012-05-06
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"

    label="Description:"

    cols="75"

    rows="15"

    wrap="virtual"

    required="true"

    validate="noblanks"

    message="You must enter a detailed description of the request." />
 

<cfquery datasource="mydns" name="insertexample">

     insert into MY_table

          (intID, 

          strDetail, 

          dtiModified, 

          strModifiedBy)

     values

          (#variables.varintID#, 

          '#form.strDetail#', 

          '#DateFormat(Now(), 'mm/dd/yy')# #TimeFormat(Now(), 'hh:mm:ss tt')#', 

          '#Variables.User#')	

</cfquery>

Open in new window

0
Comment
Question by:Lee R Liddick Jr
  • 7
  • 6
13 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
Comment Utility
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.
0
 

Author Comment

by:Lee R Liddick Jr
Comment Utility
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.
0
 
LVL 21

Expert Comment

by:Craig Wagner
Comment Utility
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.
0
 

Author Comment

by:Lee R Liddick Jr
Comment Utility
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.
0
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
Comment Utility
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.
0
 
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 500 total points
Comment Utility
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.


2009-04-09-11.30.29.jpg
2009-04-09-11.30.45.jpg
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Lee R Liddick Jr
Comment Utility
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 --->

<tr>

    <td style="text-align:right; font-weight:bold; width:25%;">Description:</td>

    <td colspan="3">#variables.strDetail#</td>

</tr>

<!--- 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

0
 
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 500 total points
Comment Utility
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.
0
 

Author Comment

by:Lee R Liddick Jr
Comment Utility
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?
0
 

Author Comment

by:Lee R Liddick Jr
Comment Utility
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.  
0
 

Author Comment

by:Lee R Liddick Jr
Comment Utility
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.
0
 
LVL 21

Expert Comment

by:Craig Wagner
Comment Utility
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.
0
 

Author Comment

by:Lee R Liddick Jr
Comment Utility
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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now