Connecting Tech Pros Worldwide Forums | Help | Site Map

Storing and retreiving rich text from MySQL in VB.net

Newbie
 
Join Date: May 2009
Posts: 6
#1: Jul 15 '09
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's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#2: Jul 15 '09

re: Storing and retreiving rich text from MySQL in VB.net


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?
Newbie
 
Join Date: May 2009
Posts: 6
#3: Jul 15 '09

re: Storing and retreiving rich text from MySQL in VB.net


Quote:

Originally Posted by tlhintoq View Post

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?

Expand|Select|Wrap|Line Numbers
  1.     Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         conn = New MySqlConnection()
  3.         'connection 
  4.         myConnString = "conection to server"
  5.         conn.ConnectionString = myConnString
  6.         conn.Open()
  7.         If conn.State = ConnectionState.Closed Then conn.Open()
  8.         massDataTable.Clear()
  9.         massCommand = New MySqlCommand
  10.         massCommand = conn.CreateCommand
  11.         massCommand.CommandText = "SELECT * FROM `test` where `mark-1` = 1"
  12.         massAdapter.SelectCommand = massCommand
  13.         massAdapter.Fill(massDataTable)
  14.         With Me
  15.             .RichTextBox1.Text = (massDataTable.Rows(0)(1))
  16.         End With
  17.     End Sub
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#4: Jul 15 '09

re: Storing and retreiving rich text from MySQL in VB.net


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?
Newbie
 
Join Date: May 2009
Posts: 6
#5: Jul 15 '09

re: Storing and retreiving rich text from MySQL in VB.net


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.
codegecko's Avatar
Moderator
 
Join Date: May 2007
Location: United Kingdom
Posts: 395
#6: Jul 16 '09

re: Storing and retreiving rich text from MySQL in VB.net


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
Newbie
 
Join Date: May 2009
Posts: 6
#7: Jul 16 '09

re: Storing and retreiving rich text from MySQL in VB.net


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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
With Me
Try
Dim ms As MemoryStream = New MemoryStream
'initializing picture
.RichTextBox1.SaveFile(ms, RichTextBoxStreamType.RichText)
Dim bytBlobData(ms.Length - 1) As Byte
ms.Position = 0
ms.Read(bytBlobData, 0, ms.Length)

Dim DatabaseParameter As New MySql.Data.MySqlClient.MySqlParameter("@BlobData", MySql.Data.MySqlClient.MySqlDbType.Blob, bytBlobData.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, bytBlobData)
If conn.State = ConnectionState.Closed Then conn.Open()
massCommand = New MySqlCommand
massCommand = conn.CreateCommand
massCommand.CommandText = "insert into `test`" & _
"(`mark-2` ) " & _
"values(@BlobData)"
massCommand.Parameters.Add(DatabaseParameter)
massCommand.ExecuteNonQuery()
Catch ex As Exception
writelog("0001", userid, Now.Date.ToString("MM/dd/yyyy"), Microsoft.VisualBasic.Left(Now.TimeOfDay.ToString, 8), "TEST")
Exit Sub
End Try
End With
End Sub

Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New MySqlConnection()
'connection
myConnString = "connection 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` = 3"
massAdapter.SelectCommand = massCommand
massAdapter.Fill(massDataTable)
With Me
Dim rtfcontent As Byte() = DirectCast(massDataTable.Rows(0)(1), Byte())
If Not rtfcontent Is Nothing Then
Using stream As New IO.MemoryStream(rtfcontent)
.RichTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText)
End Using
End If
End With
End Sub
Reply