By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

Saving and Retrieving richtext to sql server

GridCode
P: 1
Hi There

trying to save richtext to sql server. code below:


Expand|Select|Wrap|Line Numbers
  1.   SQLCat = "Catalog"
  2.         SQLDBTab = "RichTextData"
  3.         ConnStr = "Data Source=" & SQLDatSrc & ";Initial Catalog=" & SQLCat & ";User ID=" & SQLUser & ";Password=" & SQLUserPW & ";"
  4.  
  5.         QryStr = "INSERT INTO " & SQLDBTab & " (DataSheetID ,RichText) VALUES (1,@RichText)"
  6.         Dim cmd As SqlCommand = New SqlCommand(QryStr)
  7.         Dim params As SqlParameterCollection = cmd.Parameters
  8.         'params.Add("@RichText", System.Data.SqlDbType.Text)
  9.         params.Add("@RichText", SqlDbType.Text).Value = rtbDoc.Rtf.Normalize
  10.  
  11.         Dim conn As New SqlConnection(ConnStr)
  12.         cmd.CommandType = CommandType.Text
  13.         cmd.Connection = conn
  14.         Try
  15.             conn.Open()
  16.             cmd.ExecuteNonQuery()
  17.             MsgBox("RichText Saved to SQL")
  18.         Catch ex As Exception
  19.             Console.Write(ex.Message)
  20.         Finally
  21.             conn.Close()
  22.             conn.Dispose()
  23.         End Try
  24.     End Sub
Unable to get this back to richtext format.
data save into sql text field :{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttb l{\f0\fnil\fcharset0 Microsoft Sans . . .

Expand|Select|Wrap|Line Numbers
  1. when i try retrieve this with code beneath - not correct?
  2.  
  3.  Dim Dt As New DataTable
  4.         SQLCat = "Catalog"
  5.         SQLDBTab = "RichTextData"
  6.         ConnStr = "Data Source=" & SQLDatSrc & ";Initial Catalog=" & SQLCat & ";User ID=" & SQLUser & ";Password=" & SQLUserPW & ";"
  7.         QryStr = "SELECT RichText FROM " & SQLDBTab ' & " WHERE ImageID=" & 1
  8.  
  9.         Dim conn As New SqlConnection(ConnStr)
  10.         Dim Dap As New SqlClient.SqlDataAdapter(QryStr, conn)
  11.         Try
  12.             conn.Open()
  13.             Dap.Fill(Dt)
  14.         Catch ex As Exception
  15.             Console.Write(ex.Message)
  16.         Finally
  17.             conn.Close()
  18.             conn.Dispose()
  19.         End Try
  20.  
  21.         Dim Dr As DataRow
  22.         For Each Dr In Dt.Rows
  23.             rtbDoc.Rtf = (Dr.Item("RichText")) 'Does NOt Work? Format error?
  24.             rtbDoc.Text = (Dr.Item("RichText")) 'Just returns the native text, no settings, fonts applied ?
  25.         Next
Feb 15 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.