Saving and Retrieving richtext to sql server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GridCode
    New Member
    • Feb 2013
    • 1

    #1

    Saving and Retrieving richtext to sql server

    Hi There

    trying to save richtext to sql server. code below:


    Code:
      SQLCat = "Catalog"
            SQLDBTab = "RichTextData"
            ConnStr = "Data Source=" & SQLDatSrc & ";Initial Catalog=" & SQLCat & ";User ID=" & SQLUser & ";Password=" & SQLUserPW & ";"
    
            QryStr = "INSERT INTO " & SQLDBTab & " (DataSheetID ,RichText) VALUES (1,@RichText)"
            Dim cmd As SqlCommand = New SqlCommand(QryStr)
            Dim params As SqlParameterCollection = cmd.Parameters
            'params.Add("@RichText", System.Data.SqlDbType.Text)
            params.Add("@RichText", SqlDbType.Text).Value = rtbDoc.Rtf.Normalize
    
            Dim conn As New SqlConnection(ConnStr)
            cmd.CommandType = CommandType.Text
            cmd.Connection = conn
            Try
                conn.Open()
                cmd.ExecuteNonQuery()
                MsgBox("RichText Saved to SQL")
            Catch ex As Exception
                Console.Write(ex.Message)
            Finally
                conn.Close()
                conn.Dispose()
            End Try
        End Sub
    Unable to get this back to richtext format.
    data save into sql text field :{\rtf1\ansi\an sicpg1252\deff0 \deflang1033{\f onttbl{\f0\fnil \fcharset0 Microsoft Sans . . .

    Code:
    when i try retrieve this with code beneath - not correct?
    
     Dim Dt As New DataTable
            SQLCat = "Catalog"
            SQLDBTab = "RichTextData"
            ConnStr = "Data Source=" & SQLDatSrc & ";Initial Catalog=" & SQLCat & ";User ID=" & SQLUser & ";Password=" & SQLUserPW & ";"
            QryStr = "SELECT RichText FROM " & SQLDBTab ' & " WHERE ImageID=" & 1
    
            Dim conn As New SqlConnection(ConnStr)
            Dim Dap As New SqlClient.SqlDataAdapter(QryStr, conn)
            Try
                conn.Open()
                Dap.Fill(Dt)
            Catch ex As Exception
                Console.Write(ex.Message)
            Finally
                conn.Close()
                conn.Dispose()
            End Try
    
            Dim Dr As DataRow
            For Each Dr In Dt.Rows
                rtbDoc.Rtf = (Dr.Item("RichText")) 'Does NOt Work? Format error?
                rtbDoc.Text = (Dr.Item("RichText")) 'Just returns the native text, no settings, fonts applied ?
            Next
    Last edited by Rabbit; Feb 15 '13, 05:41 PM. Reason: Please use code tags when posting code.
Working...