VB converting string for use as varbinary in MSSQL

Posted on 2009-07-14
Last Modified: 2013-11-26
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

Question by:racy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 31

Assisted Solution

RiteshShah earned 25 total points
ID: 24867260
can't you use something like this?

INSERT INTO FileContent ( ContentData, ContentName, ContentDate) Values(convert(varbinary(max), 'myContent') , 'myContentName', 'myContentDate')
LVL 83

Accepted Solution

CodeCruiser earned 125 total points
ID: 24867328
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

LVL 51

Expert Comment

by:Vitor Montalvão
ID: 24867616
racy, I think this article can lead you to a solution:

Good luck
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.


Author Comment

ID: 24871703
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.

Author Comment

ID: 24871740
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!
LVL 83

Assisted Solution

CodeCruiser earned 125 total points
ID: 24876560
>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?
LVL 51

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 50 total points
ID: 24876822
racy, looks like you are passing variable name instead of variable value.

Author Closing Comment

ID: 31603539
Thanks guys.  

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

LVL 31

Expert Comment

ID: 24879992
glad to help!!!

Ritesh Shah


Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question