Solved

VB converting string for use as varbinary in MSSQL

Posted on 2009-07-14
11
1,213 Views
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

        myConn.Open()

        Try

            myCommand.ExecuteNonQuery()

        Finally

            myConn.Close()

        End Try

Open in new window

0
Comment
Question by:racy
  • 3
  • 2
  • 2
  • +1
11 Comments
 
LVL 31

Assisted Solution

by:RiteshShah
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')
0
 
LVL 83

Accepted Solution

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

Expert Comment

by:Vitor Montalvão
ID: 24867616
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
 

Author Comment

by:racy
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.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:racy
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!
0
 
LVL 83

Assisted Solution

by:CodeCruiser
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?
0
 
LVL 46

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.
0
 

Author Closing Comment

by:racy
ID: 31603539
Thanks guys.  

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

racy
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24879992
glad to help!!!

Ritesh Shah

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now