Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

I have a template rich text box where I am trying to put the formatted text (Like bolding, Italicizing and changing the colour of the text). I am saving the formatted text from this box to a column in the database which is of type 'image'

The reason behind me saving this as an Hexadecimal value is because I feel saving it as html will create a lot of coding to decode each and every html tags and show the data.

Now when I am trying to retrieve the same value and convert to string, It is unable to connect to string. all it brings back is string.byte[].

How can I convert this hexstring to proper string so that I can show it in the rich text box again?


Code to save Text

VB
Private Sub SignSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SignSave.Click
        Dim sqladd As New SqlCommand
        Dim signChecked As Integer
        If SignDefaultCheckBox.Checked Then
            signChecked = 1
        Else
            signChecked = 0
        End If
        Sqlcon.ConnectionString = "Data Source=KRISHI-PC\SQLEXPRESS;Initial Catalog=gmail;Persist Security Info=True;User ID=sa;Password=manager"
        Try
            Dim RTBXML As String = Sample_Mail.ConvertToHTML(SignTemplateRichTextBox)
            Sqlcon.Open()
            sqladd.Connection = Sqlcon
            sqladd.CommandText = "insert into dbo.signatures values (' " & SignatureTemplateNameTextBox.Text & " ' , '" & RTBXML & "'," & signChecked & ",0)"
            sqladd.ExecuteNonQuery()
            MsgBox("Data Inserted Successfully.")
            SignDefaultCheckBox.Checked = False
            SignatureTemplateNameTextBox.Text = ""
            SignTemplateRichTextBox.Text = ""
            SignatureListBox.Update()
        Catch ex As Exception
            MsgBox(" Error:" & ex.Message)
        Finally
            Sqlcon.Close()
            DT.Rows.Clear()
            Signature_Template_Load(Nothing, Nothing)
        End Try
    End Sub


Code to load data

VB
Private Sub SignatureListBox_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SignatureListBox.SelectedValueChanged
        SignSave.Enabled = True
        SignDelete.Enabled = True
        SignatureTemplateNameTextBox.Clear()
        Dim DTLoad As New DataTable
        Dim bytestring As String
        SignatureTemplateNameTextBox.Text = SignatureListBox.Text
        Sqlcon.ConnectionString = "Data Source=KRISHI-PC\SQLEXPRESS;Initial Catalog=gmail;Persist Security Info=True;User ID=sa;Password=manager"
        Dim SearchQuery As String = "Select sigtemplate from dbo.signatures where sigtemplatename = '" & SignatureTemplateNameTextBox.Text & "'"
        Dim sqlda As New SqlDataAdapter(SearchQuery, Sqlcon)
        sqlda.Fill(DS, "LoadTable")
        DTLoad = DS.Tables("LoadTable")
        If DTLoad.Rows.Count > 0 Then
            bytestring = DTLoad.Rows(0).Item(0).ToString
        End If
        'DS.Tables("LoadTable").Clear()

    End Sub


I have put everything now.
Posted
Updated 20-Nov-11 4:37am
v3

If you are using Solution1 in VB.Net Use Following Code :
VB
'byte() to string:
   Dim bytes As Byte()= ...
   Dim s As String= System.Text.Encoding.ASCII.GetString(bytes)

'string to byte():
   Dim s As String= ...
   Dim bytes As Byte()= System.Text.Encoding.ASCII.GetBytes(s)

Another Way to Convert Value From Hexadecimal to String :
VB
'Format to Convert Type
Convert.ChangeType(Your Value As Object,Type to Convert)
'Example
Convert.ChangeType("Your String", TypeCode.Byte)
 
Share this answer
 
Comments
Vamshi Krishna Naidu 20-Nov-11 10:30am    
After doing this I get,

"Conversion from string "System.Byte[]" to type 'Byte' is not valid."
Manoj K Bhoir 21-Nov-11 9:31am    
Don't Use [] because it is used in C#, In Vb.Net you have to used () insted of [].
All you have to do is reverse the conversion from string to byte[] which you did in order to save it as an Image data field.
C#
//byte[] to string:
   byte[] bytes = ...
   string s = System.Text.Encoding.ASCII.GetString(bytes);

//string to byte[]:
   string s = ...
   byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);
 
Share this answer
 
Comments
Vamshi Krishna Naidu 20-Nov-11 10:05am    
It says 'value of type byte cannot be converted to 1-dimensional array of Byte'

Also I am using VB.Net not C#
OriginalGriff 20-Nov-11 10:33am    
VB / C# doesn't matter - the principle is the same. (Look at DeveloperFusion.com - it does online translation betweeen the two)
We need to see the code fragments - both for the write and the read. For some reason when you retrieve your data you are getting it as a single byte, which is silly, if you saved a string or byte array.
Vamshi Krishna Naidu 20-Nov-11 10:38am    
I have updated the question with save and load code. Please help me out.
OriginalGriff 20-Nov-11 11:21am    
It's going to hinge on a couple of things: firstly what does Sample_Mail.ConvertToHTML do when passed a rich text box? Becasue that is what you are passing to it - not the content, but the control itself.
Vamshi Krishna Naidu 20-Nov-11 11:35am    
That will covert the text to html but I removed that functionality here. coz i am trying to save the formatted text directly in the form of hex's.
Without reading much of this post (which does not look reasonable): the title of the question does not make sense at all. What convert to what? No numeric type can be "hexadecimal" or "decimal" or "octal" or something like that — they all are "binary". You can only talk about "hexadecimal" only if this is some string which content is supposed to represent a hexadecimal presentation of a number. So, what should be converted: this string to string?

Please don't say that you explain it in the body of the question: first, you don't, second, a title should be reasonable enough, otherwise there is no enough reason to read the body.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900