Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1438
  • Last Modified:

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
        myConn.Open()
        Try
            myCommand.ExecuteNonQuery()
        Finally
            myConn.Close()
        End Try

Open in new window

0
racy
Asked:
racy
  • 3
  • 2
  • 2
  • +1
4 Solutions
 
RiteshShahCommented:
can't you use something like this?


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

        System.Text.Encoding.ASCII.GetBytes("DataContent")

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

Open in new window

0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
racy, I think this article can lead you to a solution:
http://stackoverflow.com/questions/976761/dealing-with-a-varbinary-field-in-vb-net

Good luck
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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.
0
 
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!
0
 
CodeCruiserCommented:
>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?
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
racy, looks like you are passing variable name instead of variable value.
0
 
racyAuthor Commented:
Thanks guys.  

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

racy
0
 
RiteshShahCommented:
glad to help!!!

Ritesh Shah

0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now