Windows Develop Bookmark and Share   
 index > Windows Forms General > Converting sql field type diary to textbox display
 

Converting sql field type diary to textbox display

I have a database I am querying that I don't own. The field type is a diary. If I do a ToString() on it, and set the textbox.text to it, I have strangely some but not all of the line breaks(not specifically line breaks, I don't know what they are specifically) are showing up...
Any idea how I can get all the line breaks to make it into the textbox...

attempt #1
myDataSet.mytableRow CurRow = myDataSet.mtable.FindByid(idNum);
this.textbox1.Text = CurRow.description.ToString();

This gets a couple of the newlines in the diary text... but only a 3-4 out of 10-20

attempt #2
myDataSet.mytableRow CurRow = myDataSet.mtable.FindByid(idNum);
char[] aStr = CurRow.description.ToCharArray();
this.textbox1.Text = "";
foreach (char it in aStr) {
this.textbox1.AppendText(it.ToString());
}

This actually got a few more, and in a few cases all, but in several key cases it still only got a few.

Any help on this mystery is appreciated... thanks in advance...

Randell
RandellP  Tuesday, May 05, 2009 11:48 PM
> What isn't working is \r or \n independently

That's expected. Windows uses \r\n (ASCII 13 ASCII 10) together as the end of line sequence.

You could try this function to do the conversion.

        public static string StandardizeNewLines(string s)
        {
            // Standardizes \r, \n, or \r\n to \r\n.

            System.Text.StringBuilder sb = new System.Text.StringBuilder(s.Length);
            char prevChar = '\0';
            foreach (char c in s)
            {
                if (c == '\r')
                    sb.Append("\r\n");
                else if (c == '\n')
                {
                    if (prevChar != '\r')
                        sb.Append("\r\n");
                }
                else
                    sb.Append(c);

                prevChar = c;
            }

            return sb.ToString();
        }

  • Marked As Answer byRandellP Wednesday, May 06, 2009 1:58 AM
  •  
BinaryCoder  Wednesday, May 06, 2009 12:51 AM
Please breakpoint on the line whereyou call ToString in the first example. Use the immediate window to print out CurRow.description.ToString(). This should display text that contains \r (carriage return) and/or \n (line feed)placeholders in string in the immediate window. Post what you see here.
BinaryCoder  Tuesday, May 05, 2009 11:53 PM
What isn't working is \r or \n independently... But where \r\n exist in a pair that seems to be working...

hmmm, so I can break it into characters and find \r or \n but not \r\n and change it to Environment.newline...
is there an easier way?

Randell
RandellP  Wednesday, May 06, 2009 12:37 AM
> What isn't working is \r or \n independently

That's expected. Windows uses \r\n (ASCII 13 ASCII 10) together as the end of line sequence.

You could try this function to do the conversion.

        public static string StandardizeNewLines(string s)
        {
            // Standardizes \r, \n, or \r\n to \r\n.

            System.Text.StringBuilder sb = new System.Text.StringBuilder(s.Length);
            char prevChar = '\0';
            foreach (char c in s)
            {
                if (c == '\r')
                    sb.Append("\r\n");
                else if (c == '\n')
                {
                    if (prevChar != '\r')
                        sb.Append("\r\n");
                }
                else
                    sb.Append(c);

                prevChar = c;
            }

            return sb.ToString();
        }

  • Marked As Answer byRandellP Wednesday, May 06, 2009 1:58 AM
  •  
BinaryCoder  Wednesday, May 06, 2009 12:51 AM
Awesome... that worked, thanks...

Randell
RandellP  Wednesday, May 06, 2009 2:08 AM

You can use google to search for other answers

Custom Search

More Threads

• Resize the Webbrowser control with the hosted window.
• ToolTip IsBalloon
• Varible By Address
• C# windowless RichTextBox drawn on Graphics
• Progress Bar Help!
• GDI exact measure at the screen
• Having to change class type continually during development
• Memory problems... leaks maybe.. weird.
• create a moveable panel in vb.net?
• MDI child as Modal ?