VB converting string for use as varbinary in MSSQL

I have a long string of data, that is to be stored in a db, with column type of varbinary(max).  Problem is, I can't get an insert statement from within VB that will let me do such.  I tried using the convert command in my InsertQuery string, but it doesn't like the syntax.

THe field that needs to be a varbinary(max) is "ContentData".

The exact error message is "Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query"

Any ideas?
Dim dbConnection = "User Id=xx;Password=xxxxxx;Server=x.x.x.x;Initial Catalog=Content"
        Dim myConn As New SqlConnection(dbConnection)
        Dim myInsertQuery As String = "INSERT INTO FileContent ( ContentData, ContentName, ContentDate) Values('myContent', 'myContentName', 'myContentDate')"
        Dim myCommand As New SqlCommand(myInsertQuery)
        myCommand.Connection = myConn
        End Try

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

can't you use something like this?

INSERT INTO FileContent ( ContentData, ContentName, ContentDate) Values(convert(varbinary(max), 'myContent') , 'myContentName', 'myContentDate')
Try using the following syntax


So your query would be
"NSERT INTO FileContent ( ContentData, ContentName, ContentDate) Values('" & System.Text.Encoding.ASCII.GetBytes("DataContent") & "', 'myContentName', 'myContentDate')

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
racy, I think this article can lead you to a solution:

Good luck
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

racyAuthor Commented:
Okay.  THe above solutions are working.  Now another problem is arising.  Instead of the actual values being inserted into the database, the names of variables are being inserted.

Apologies if this is a stupid question.
racyAuthor Commented:
Here's the error I get upon execution:

The name "mySystemID" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

Funny thing in, I don't have columns with that name, and they are variables that I am passing!
>THe above solutions are working.
Which solution?

> Instead of the actual values being inserted into the database, the names of variables are being inserted.
Can you please show the code you are using?
Vitor MontalvãoMSSQL Senior EngineerCommented:
racy, looks like you are passing variable name instead of variable value.
racyAuthor Commented:
Thanks guys.  

I have more VB questions coming up too.  You are great and thanks again,

glad to help!!!

Ritesh Shah

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.