Thursday, March 22, 2012

Strange issue with Environment.NewLine in SQL

First off, apologies if this is in the wrong board.

I'm going to ramble a bit here in an attempt to get my thoughts together...

Background:

So I have this message board I've built using ASP.Net 2.0 and SQL 2005. I'd link you to it, but the particular site in question is a bit of a free for all so I can't guarantee you won't find something offensive =P.

Anyways, the important thing to note is that posts are typed into a multiline textbox, and saved 'as is' in the relevant SQL table as varchar. When a topic is displayed, this text is converted into HTML: Environment.NewLine -> <br />, weird messageboard tags into HTML (bold, italic, etc.), and so on.

Now this is all fine and dandy but lately a weird issue has came up.

Issue:

The odd time, when a particular person makes a post, his NewLine characters get lost somewhere. What I mean is he'll type something with pretty paragraph breaks and nice formatting and then it'll be displayed as a solid block of text.

But here's where things get funky - if I click to edit his post (at which point the text is displayed 'as is' from the SQL table into another textbox), it displays as it should, with the line breaks. If I save the changes without altering the text at all, his post will now display properly. I can also quote his single-block-of-text post and it will display as multiple paragraphs within my own post.

All I can figure is that maybe something strange is happening for different users as they saved their posts.

Some things to note:

I looked through the SQL tables at three identical posts, one posted by user A, who had his post turn into a single block of text, one by me, whose post displayed properly, and another consisting of user A's post copy/pasted by me (just to ensure I didn't miss anything when retyping his post). Again, this last post displayed properly.

The hexadecimal code for the line-breaks in my post was 0x0D 0x0A, which corresponds to the CRLF code for Windows. Makes sense since I'm using Windows. This was again the case for my second post, the copy/pasted version of User A's original post. Though when I look at his post, the line-break characters come up as 0x0A, which is the LF code for users running a Mac. This makes less sense because I know the guy is running Windows.

But maybe I'm overthinking this and I should be using a unicode variable type in the SQL? Any suggestions would be welcome.

Cheers

apedrape83:

The hexadecimal code for the line-breaks in my post was 0x0D 0x0A, which corresponds to the CRLF code for Windows. Makes sense since I'm using Windows. This was again the case for my second post, the copy/pasted version of User A's original post. Though when I look at his post, the line-break characters come up as 0x0A, which is the LF code for users running a Mac. This makes less sense because I know the guy is running Windows.

You know he is using Windows because you have observed this with your own eyes? Or because he told you? Maybe he's running windows on a Mac, and this is a "feature".


Oh snap, just found this:http://forums.asp.net/p/1186555/2027418.aspx

He's running windows (he sent me screenshots while I was requesting his assistance in debugging), so yeah, saw it with my own eyes more or less. BUT, he's also running FireFox and doesn't have a problem in IE7. I overlooked this because it worked fine on my own FireFox, which is a different version than his.

I'm going to have to read more into this issue with FireFox and see if the way it chooses to save NewLines explains the truncated hex values I described.


Anyways, the problem was solved by grabbing all of the '\r\n' breaks out of the string as put in by IE7 and replacing them with the html followed by grabbing the leftover '\n' breaks used by other browsers as the NewLine in a textbox and replacing those as opposed to replacing the Environment.NewLine directly.

0 comments:

Post a Comment