Storing and retreiving rich text from MySQL in VB.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evilson
    New Member
    • May 2009
    • 6

    Storing and retreiving rich text from MySQL in VB.net

    Hi,

    I have a question, i need to store the value of richtextbox to MySQL and retrive the value from MYSQL to richtextbox. can you give me a code in vb.net? and will the formating be affected like the font and alignment?

    Can you please respond quicky?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Have you tried to do this on your own first?
    Could you provide the code you've written thus far so the volunteers here can help you find why your code doesn't work?

    Comment

    • evilson
      New Member
      • May 2009
      • 6

      #3
      Originally posted by tlhintoq
      Have you tried to do this on your own first?
      Could you provide the code you've written thus far so the volunteers here can help you find why your code doesn't work?
      Code:
          Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              conn = New MySqlConnection()
              'connection 
              myConnString = "conection to server"
              conn.ConnectionString = myConnString
              conn.Open()
              If conn.State = ConnectionState.Closed Then conn.Open()
              massDataTable.Clear()
              massCommand = New MySqlCommand
              massCommand = conn.CreateCommand
              massCommand.CommandText = "SELECT * FROM `test` where `mark-1` = 1"
              massAdapter.SelectCommand = massCommand
              massAdapter.Fill(massDataTable)
              With Me
                  .RichTextBox1.Text = (massDataTable.Rows(0)(1))
              End With
          End Sub
      Last edited by tlhintoq; Jul 15 '09, 12:34 PM. Reason: [CODE] ... your code here ... [/CODE] tags added

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        It shouldn't be a game of 20 questions to try to help you. Common sense should tell you that the more information you provide the better someone can assist you.

        There is no mention in your post of what/why your own code isn't working. Do you get an error? Do you get behavior different than you expected? Is your data going into the database but you are having trouble getting it back out, or can you retreive data that was already in the db but you can put your own in? Have you put breakpoints in your code and walked through it line-by-line? Is there a point where something is not as expected and you don't know why?

        Comment

        • evilson
          New Member
          • May 2009
          • 6

          #5
          I have no problem on saving a text in mysql but yes i did, the error i have was when retriving it from Mysql, an error appears that the conversion from type 'Byte()' to type 'String' is not valid.

          Comment

          • JamieHowarth0
            Recognized Expert Contributor
            • May 2007
            • 537

            #6
            evilson,

            Does that error give a line number and an excerpt of code? This will help us isolate down the problem.
            My suspicions are that you're trying to get the wrong column from your database into the richtextbox control - however, if you post the full error message up and stack trace then we'll be able to help you isolate the problem further.

            codegecko

            Comment

            • evilson
              New Member
              • May 2009
              • 6

              #7
              I think it's a runtime error. But dont worry, I do some research and i develope a code. I'll post the full code

              Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
              With Me
              Try
              Dim ms As MemoryStream = New MemoryStream
              'initializing picture
              .RichTextBox1.S aveFile(ms, RichTextBoxStre amType.RichText )
              Dim bytBlobData(ms. Length - 1) As Byte
              ms.Position = 0
              ms.Read(bytBlob Data, 0, ms.Length)

              Dim DatabaseParamet er As New MySql.Data.MySq lClient.MySqlPa rameter("@BlobD ata", MySql.Data.MySq lClient.MySqlDb Type.Blob, bytBlobData.Len gth, ParameterDirect ion.Input, False, 0, 0, Nothing, DataRowVersion. Current, bytBlobData)
              If conn.State = ConnectionState .Closed Then conn.Open()
              massCommand = New MySqlCommand
              massCommand = conn.CreateComm and
              massCommand.Com mandText = "insert into `test`" & _
              "(`mark-2` ) " & _
              "values(@BlobDa ta)"
              massCommand.Par ameters.Add(Dat abaseParameter)
              massCommand.Exe cuteNonQuery()
              Catch ex As Exception
              writelog("0001" , userid, Now.Date.ToStri ng("MM/dd/yyyy"), Microsoft.Visua lBasic.Left(Now .TimeOfDay.ToSt ring, 8), "TEST")
              Exit Sub
              End Try
              End With
              End Sub

              Private Sub frmTest_Load(By Val sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
              conn = New MySqlConnection ()
              'connection
              myConnString = "connection to server"
              conn.Connection String = myConnString
              conn.Open()
              If conn.State = ConnectionState .Closed Then conn.Open()
              massDataTable.C lear()
              massCommand = New MySqlCommand
              massCommand = conn.CreateComm and
              massCommand.Com mandText = "SELECT * FROM `test` where `mark-1` = 3"
              massAdapter.Sel ectCommand = massCommand
              massAdapter.Fil l(massDataTable )
              With Me
              Dim rtfcontent As Byte() = DirectCast(mass DataTable.Rows( 0)(1), Byte())
              If Not rtfcontent Is Nothing Then
              Using stream As New IO.MemoryStream (rtfcontent)
              .RichTextBox1.L oadFile(stream, RichTextBoxStre amType.RichText )
              End Using
              End If
              End With
              End Sub

              Comment

              Working...