RichTextBox format to SQL

Why can't i add this row into SQL from a C# application, i also tried to do it in mysql query browser and it went perfectly. I supose that i have problems with the "\r\n" part and i don't what to do now. I know that \r\n means next line, but i can't undestand why it will work from mysql query browser and not from the application.

this is the INSERT command

"INSERT INTO `Problem` ( `Problem` ) VALUES('{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1053{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17 Testring writing text.\\par\r\n}\r\n\0')"



Answer this question

RichTextBox format to SQL

  • Andy Wilbourn

    I realy can't undestand why it won't work. Is it another way to copy RichTextBox, insert it into SQL and then read it from SQL and put it in a RichTextBox
  • GrandpaB

    After doing the Debug.WriteLine i can se that some text is missing. Im gonna try to figure out what the problem is.
  • Toan

    I thought i should take some time to dive into my code and find the problem, and i found it =). I can't past it right now becuse im on another computer. But this is what causes the problem. When i take all the RichTextBox contens to a string, the following letters "a, a, o" is translated to " 'et, 'fd, 'hs " or something like that, and thats a problem for the SQL query. And this ' <-- character is causing me problem. So, for some reason a RichTextBox can't convert this letters into string.
  • Bruce Bukovics

    why dont you post some code here so I could point you at the error (if I can find it)

  • Jassim Rahma

    what do you mean with It doesn't work!

    try to add 2 in front of text as you C# compiler could try to understand \ as escape symbol

    @"INSERT INTO `Problem` ( `Problem` ) VALUES('{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1053{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17 Testring writing text.\\par\r\n}\r\n\0')"

    I would use this code


            SqlConnection conn = new SqlConnection("connection string here");
            SqlCommand cmd = new SqlCommand("INSERT INTO [Problem] ( [Problem] ) VALUES(@RTF)", conn);
            cmd.Parameters.Add(new SqlParameter("@RTF", @"your RTF \r\nr here");
           
            conn.Open();

            int rowsAffected = cmd.ExecuteNonQuery();

            conn.Close();



  • DocMARs

    you do not have to replace this symbols... RTF needs them. do not confuse what VS shows you and what are they. use Debug.WriteLine().. to display actual content

  • Michael_317

    >>I allready tried with .Replace(@"\", @"\\") but for some reason it wount take the last "\r\n}\r\n\0" section...

    That's because the "\r\n" exists only in your source code. When the assembly is generated, the compiler replaces them with a line feed & carraige return character. By the time your code runs, there are no slashes in that section.



  • HiredKiller

    I allready tried with .Replace(@"\", @"\\") but for some reason it wount take the last "\r\n}\r\n\0" section...
  • RichTextBox format to SQL