0

i'm a beginner in vb.net

i just want to know if it is possible to store the formatting of a text to ms sql so that the next time i retrieve a record that i've already formatted, it will display the format that i did for that record.

if possible, how would i implement it in mssql?

thanks

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sknake
0

Here is an example. This form has 2 RTF boxes and a button:

Imports System.Data.SqlClient
Imports System.Text

Public Class frmRTF

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Const RECORD_ID As Integer = 1000

    Dim Sql As New List(Of String)
    Sql.Add("IF OBJECT_ID('tempdb..##RTF', 'U') IS NOT NULL DROP TABLE ##RTF")
    Sql.Add("Create Table ##RTF")
    Sql.Add("(")
    Sql.Add("  RecordId int PRIMARY KEY,")
    Sql.Add("  RTF nvarchar(max)")
    Sql.Add(")")
    Sql.Add("Insert Into ##RTF (RecordId, RTF) Values (@RecordId, @RTF)")
    Dim query As String = GetText(Sql)

    Using conn As New SqlConnection("Data Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;")
      conn.Open()
      Using cmd As New SqlClient.SqlCommand(query, conn)
        cmd.Parameters.Add(New SqlParameter("@RecordId", SqlDbType.Int)).Value = RECORD_ID
        cmd.Parameters.Add(New SqlParameter("@RTF", SqlDbType.NVarChar)).Value = RichTextBox1.Rtf
        cmd.ExecuteNonQuery()
      End Using

      'I'm using a temp table so the connection has to be left open

      query = "Select * From ##RTF Where RecordId = @RecordId"
      Using cmd As New SqlClient.SqlCommand(query, conn)
        cmd.Parameters.Add(New SqlParameter("@RecordId", SqlDbType.Int)).Value = RECORD_ID
        Using dr As SqlDataReader = cmd.ExecuteReader()
          Using dt As New DataTable()
            dt.Load(dr)
            RichTextBox2.Rtf = Convert.ToString(dt.Rows(0)("RTF"))
          End Using
        End Using
      End Using
      conn.Close()
    End Using

  End Sub

  Private Shared Function GetText(ByRef lst As List(Of String)) As String
    Dim sb As New StringBuilder()
    For Each s As String In lst
      sb.AppendLine(s)
    Next
    Return sb.ToString()
  End Function

End Class
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.