Link to home
Start Free TrialLog in
Avatar of kmoloney
kmoloneyFlag for United States of America

asked on

S/B EZ: Losing Carriage Returns and Line Feeds in SQL Server

Hopefully this isn't too difficult, but after searching I haven't really found a solution.

My VB.NET Windows App stores information that is input into a rich text box (a text box would be fine too) into a field of an SQL Server database.  The important thing is KEEPING carriage returns and line feeds.  When I pull the data back out, Carriage Returns and Line Feeds are lost.  E.g.

Hello

How Are you

Have you been alright.

Comes back as:

HelloHow Are youHave you been alright.

So I need to preserve the CR's and LF's.  I've read articles where they suggest replacing the CR's and LF's with the <br /> tag, but this isn't a web app, and doesn't appear to work for an Windows application.

I tried configuring the SQL field as varchar(), nvarchar(), and ntext, but there's no difference in the result.

Any help?
ASKER CERTIFIED SOLUTION
Avatar of dqmq
dqmq
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kmoloney

ASKER

SORRY I've been so long in getting back to this question.  Muy busy these days.

Okay, well, when retreiving the rtf property, I'm getting something like the following:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17 Now is the time for all men to come to aid of their country.\par
\par
However, I have some reservations.\par
\par
I am a coward.\par
\par
Thank you---\par
\par
Kevin\par
}

Which I imagine could be parsed...however...

NOW -- for a bit more intriguing problem...turns out the application DOES have to be a web app, which means...no rich text box control.  So if I'm just using the textbox control (multiline), all CR's and LF's are being removed when I retrieve the info using the .text property of the text box.

Grrrrrrr....
OK, I'm half way there --

Converting the the text to have CRLF's isn't that bad; I just do a replace before I save the text in the textbox:

e.g., strVar = TextBox1.Text.Replace(Environment.Newline,Microsoft.VisualBasic.VbCrLf)

That seems to store it okay in SQL.  Now only problem remaining is getting that out to display in web-based controls, like a repeater.  Replacing with "<BR />" doesn't seem to work.
It turned out that my question wasn't properly phrased, so I awarded points equally.  The issue is going between VB or VS Studio 2005 controls bound to SQL Server.  Multilines DO store crlf's as characters (Microsoft.VisualBasic.VBCRLF), and that the basic approach to solve the problem was in my last comment dated 5/3/2007.  However, flipping back and forth between display and input states of a one of the VS2005 databound controls (e.g., a formview) should (IMHO) take into account the transition ahead of time.

Manually replacing <BR />'s with Microsoft.VisualBasic.vbCrLf's, and vice versa, in any situation where a view might change from edit to view, is silly.  The control should recognize the context in which it is being viewed in, and developer shouldn't have to piddle with details like this.

This question is in no way solved.  I'll have to post a new one specific to the VS controls I'm interested in.

Thanks.