none
Retrieve a String with original formatting from SQL Table and Show in C# Windows form RRS feed

  • Question

  • multiple line's (and tabs/spaces etc.) string stored in a Database table column. Example,

    I've executed the following SQL and I know SQL correctly preserves the formatting here (Tab/Newline etc.) and we have verified that formatting SSMS with Results in 'textview' (We use use default Result to Gridview then it will still appear as one single line statement on, but 'textview' or 'fileview' shows that formatting was retained by SQL). 

    But I've no clue how I can display it in this similar format in C# form/textbox when I'll read it from database. Seems over here in the textbox it is automatically converting everything as one line and all escape characters are ignored. Any help please how can I retain the formatting?

    Insert Into myTable(longtext)
    Select 
    '*****************
       CS-Firm
            SS DB DS
       New Firm -- N
    ******************'


    Saturday, March 28, 2020 6:34 PM

Answers

  • Hi AMJEE,

    You can use a  RichTextBox control instead of textbox.

     

     RichTextBox1.Text = "
    CS-Firm
            SS DB DS
       New Firm -- N";


    Please remember to mark the replies as answers if they helped you :) ~

    • Proposed as answer by Naomi NModerator Sunday, March 29, 2020 2:55 AM
    • Marked as answer by AMJEE Sunday, March 29, 2020 7:40 PM
    Saturday, March 28, 2020 7:51 PM
  • Hello,

    Here is an in depth article with examples. 

    https://www.sqlshack.com/sql-carriage-return-or-tab-in-sql-server-string/ 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by AMJEE Sunday, March 29, 2020 7:44 PM
    Saturday, March 28, 2020 8:22 PM
    Moderator

All replies

  • Hi AMJEE,

    You can use a  RichTextBox control instead of textbox.

     

     RichTextBox1.Text = "
    CS-Firm
            SS DB DS
       New Firm -- N";


    Please remember to mark the replies as answers if they helped you :) ~

    • Proposed as answer by Naomi NModerator Sunday, March 29, 2020 2:55 AM
    • Marked as answer by AMJEE Sunday, March 29, 2020 7:40 PM
    Saturday, March 28, 2020 7:51 PM
  • Hello,

    Here is an in depth article with examples. 

    https://www.sqlshack.com/sql-carriage-return-or-tab-in-sql-server-string/ 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by AMJEE Sunday, March 29, 2020 7:44 PM
    Saturday, March 28, 2020 8:22 PM
    Moderator
  • Check that your code, which reads the value from database, returns the expected string (use the Debugger to check the value and line separators). Make sure that the textbox has the Multiline property.

    Maybe also use a monospaced font like “Courier New”.

    Saturday, March 28, 2020 8:24 PM
  • That was so simple.. thank a lot for your help.
    Sunday, March 29, 2020 7:41 PM
  • Thanks for sharing this article. Though I just used RichTextBox in my form as @Rabin Suggested above, but this article was indeed helpful. Thanks
    Sunday, March 29, 2020 7:44 PM