Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to convert the 1-dimension byte array to varbinary(max) in vb.net and also the coverted value should be display in message box. When i try this i got "1-dimension array cannot be converted to string" error. what can i do?

Best Post will be Well Appreciated
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jan-13 23:45pm    
Try this... what "this"? Please show informative code sample, indicate where is the error...
—SA

1 solution

There are a couple of things here: VB.NET has no concept of varbinary(max) - that is an SQL datatype, not .NET, and depending on your byte content it may or may not be readable as a string anyway, so displaying it in a message box may be more complex than you think.

So I will assume you want to save an array of bytes to an SQL database in a varbinary field, and show you how to convert a byte array to a string (for whatever good it will do you - it may not be any real use as far as human readability is concerned)
Output byte array to SQL:
VB
Dim bytes as Byte() = File.ReadAllBytes(path)
Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("INSERT INTO myTable (myColumn) VALUES (@COL)", con)
        com.Parameters.AddWithValue("@COL", bytes)
        com.ExecuteNonQuery()
    End Using
End Using

Convert bytes to string:
VB
Dim bytes As Byte() = File.ReadAllBytes(path)
Dim s As String = System.Text.Encoding.ASCII.GetString(bytes)
Depending on what is in your byte array, you might need to use a different Encoding value than ASCII
 
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