שתף באמצעות


Saving RichTextBox Formatted Text To A SQL Server 2014 Database

Question

Sunday, April 17, 2016 9:32 PM

I am writing code for a small windows 7 VB 2013 application.

The program works very sporadically, yielding a message that the parameter @CO_R_Date has not been supplied. If I forget about formatting and change the bindingsource property from "Rtf" to "Text", everything works fine. The CO_DETAIL column in my database has a type of NVarChar(Max). I would really appreciate some help.

Thanks,

John

'Part Of The Bindingsource .

rtbCO_DETAIL.DataBindings.Add("Rtf", bsCO, "CO_DETAIL", True)

'Insert Command.

'Create A SqlCommand To Insert Component Data.
        CO_InsertCommand.Connection = cnEIS
        CO_InsertCommand.CommandType = CommandType.Text

        strCO_InsertCommand = "INSERT INTO [dbo].[CO] ([CO_KEY], [CO_ID], " & _
                                                    "[CO_DESC], [CO_PC_KEY], [CO_DETAIL], [CO_E_DATE], " & _
                                                    "[CO_R_DATE]) " & _
                                                    "VALUES (@CO_KEY, @CO_ID, @CO_DESC, @CO_PC_KEY, " & _
                                                    "@CO_DETAIL, @CO_E_DATE, @CO_R_DATE); " & _
                                                    "SELECT * FROM CO " & _
                                                    "WHERE (CO_KEY = @CO_KEY)"

        CO_InsertCommand.CommandText = strCO_InsertCommand

        CO_InsertCommand.Parameters.Add("@CO_KEY", SqlDbType.NVarChar, 14, "CO_KEY")
        CO_InsertCommand.Parameters.Add("@CO_ID", SqlDbType.NVarChar, 20, "CO_ID")
        CO_InsertCommand.Parameters.Add("@CO_DESC", SqlDbType.NVarChar, 50, "CO_DESC")
        CO_InsertCommand.Parameters.Add("@CO_PC_KEY", SqlDbType.NVarChar, 14, "CO_PC_KEY")
        CO_InsertCommand.Parameters.Add("@CO_DETAIL", SqlDbType.NVarChar, 10000, "CO_DETAIL")
        CO_InsertCommand.Parameters.Add("@CO_E_DATE", SqlDbType.NVarChar, 11)
        CO_InsertCommand.Parameters.Add("@CO_R_DATE", SqlDbType.NVarChar, 11, "CO_R_DATE")

'Delete Command.

 'Create A SqlCommand To Delete Components.
        CO_DeleteCommand.Connection = cnEIS
        CO_DeleteCommand.CommandType = CommandType.Text

        strCO_DeleteCommand = "DELETE FROM CO " & _
                                                      "WHERE (CO_KEY = @Original_CO_KEY)"

        CO_DeleteCommand.CommandText = strCO_DeleteCommand

        Dim DeleteParameter0 As SqlParameter = CO_DeleteCommand.Parameters.Add _
        ("@Original_CO_KEY", SqlDbType.NVarChar, 14, "CO_KEY")
        DeleteParameter0.SourceVersion = DataRowVersion.Original

Update Command.

 'Create A SqlCommand To Update Component Data.
        CO_UpdateCommand.Connection = cnEIS
        CO_UpdateCommand.CommandType = CommandType.Text

        strCO_UpdateCommand = "UPDATE [dbo].[CO] SET [CO_KEY] = @CO_KEY, " & _
                                                       "[CO_ID] = @CO_ID, " & _
                                                       "[CO_DESC] = @CO_DESC, " & _
                                                       "[CO_PC_KEY] = @CO_PC_KEY, " & _
                                                       "[CO_DETAIL] = @CO_DETAIL, " & _
                                                       "[CO_R_DATE] = @CO_R_DATE " & _
                                                       "WHERE ([CO_KEY] = @Original1_CO_KEY);" & _
                                                       "SELECT * FROM CO " & _
                                                       "WHERE (CO_KEY = @CO_KEY)"

        CO_UpdateCommand.CommandText = strCO_UpdateCommand

        CO_UpdateCommand.Parameters.Add("@CO_KEY", SqlDbType.NVarChar, 14, "CO_KEY")
        CO_UpdateCommand.Parameters.Add("@CO_ID", SqlDbType.NVarChar, 20, "CO_ID")
        CO_UpdateCommand.Parameters.Add("@CO_DESC", SqlDbType.NVarChar, 50, "CO_DESC")
        CO_UpdateCommand.Parameters.Add("@CO_PC_KEY", SqlDbType.NVarChar, 14, "CO_PC_KEY")
        CO_UpdateCommand.Parameters.Add("@CO_DETAIL", SqlDbType.NVarChar, 10000, "CO_DETAIL")
        CO_UpdateCommand.Parameters.Add("@CO_R_DATE", SqlDbType.NVarChar, 11)

        Dim UpdateParameter0 As SqlParameter = CO_UpdateCommand.Parameters.Add _
            ("@Original1_CO_KEY", SqlDbType.NVarChar, 14, "CO_KEY")
        UpdateParameter0.SourceVersion = DataRowVersion.Original

All replies (6)

Monday, April 18, 2016 3:43 AM ✅Answered

I don't know.

However RTF is string format so it shouldn't need to be saved to a file and loaded from a file IMO. I should think you could just save the string (for Hello the below string is from RTB's Rtf) as saving RTF to a file is just a text file even if it contains images. Or convert it to byte array and save as BLOB or however SQL works with BLOB.

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17 Hello\par
}

La vida loca


Monday, April 18, 2016 3:16 AM

Hi Developer Dude,

First of all, could you clarify what type your project is? A Windows form application or a WPF project others? Here, I assume it’s a windows form application. If I’m wrong, please let me know.

If you want to save the RTF value with formatted text to SQL Server, you’d better first save it two RTF file and then save this file to the database. When retrieving it from database, you could load the file to the richtextbox. For this, you could have a look at below article that provides a demo to achieve it.

http://www.codeguru.com/columns/dotnettips/article.php/c7529/Saving-Rich-Edit-Control-Text-to-SQL-Server.htm

If your project is a WPF project, the following thread would help you achieve your goal. As it says, you could use XAML format to do it.

http://stackoverflow.com/questions/15983278/storing-data-of-rich-text-box-to-database-with-formatting

This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

Best Regards,

Albert Zhang


Monday, April 18, 2016 1:42 PM

I cannot tell what your data access layer is binding to, but you need to have an edit check on the Rich Text object (RichTextBox?) in order to make sure it contains data prior to update.

Paul ~~~~ Microsoft MVP (Visual Basic)


Monday, April 18, 2016 2:34 PM

I am binding to a SQL 2014 database.


Monday, April 18, 2016 4:08 PM

I am binding to a SQL 2014 database.

Binding from SQL 2014 to what? Is this information being displayed?

Paul ~~~~ Microsoft MVP (Visual Basic)


Wednesday, August 1, 2018 8:54 AM

I know it's old forum but try this

http://simplevbnet.blogspot.com/2018/07/complete-how-to-saveload-richtextbox.html