Must declare the scalar variable  Error

Posted on 2009-05-11
Last Modified: 2012-05-06
I am getting an error 'Must declare the scalar variable @Blob" when I run the following code VB >net 2005 against MS SQL Server 2008.  This worked fine when run against a MS Access database.

Can someone tell me how to fix the problem?

Friend Sub SendToBLOBNew(ByRef dbConnection As System.Data.IDbConnection, _
                             ByRef lVersionNo As Long, _
                             ByRef strFile As String, _
                             ByRef strFolder As String)
        Dim cmd As New OleDbCommand()
        cmd.CommandText = _
        "INSERT INTO tbl_Object(VersionId,Component,Blob) VALUES (" + CStr(lVersionNo) + "," + _
                                                                  "'" & strFile & "'," + _
        cmd.Connection = dbConnection
        Dim fs As New System.IO.FileStream(strFolder + strFile, IO.FileMode.Open, IO.FileAccess.Read)
        Dim b(fs.Length() - 1) As Byte
        fs.Read(b, 0, b.Length)
        fs = Nothing
        ' ***** Compress file before submitting to source control *****
        Dim byteCompressedByteArray As Byte()
        byteCompressedByteArray = mobjCompression.ZipFile(, b)
        Dim P As New OleDbParameter()
        With P
            .ParameterName = "@BLOB"
            .OleDbType = OleDbType.LongVarBinary
            .Size = byteCompressedByteArray.Length
            .Direction = ParameterDirection.Input
            .IsNullable = False
            .Precision = 0
            .Scale = 0
            .SourceColumn = Nothing
            .SourceVersion = DataRowVersion.Current
            .Value = byteCompressedByteArray
        End With
    End Sub

Open in new window

Question by:FINDERSOL
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
  • 2

Expert Comment

ID: 24356683
instead of OleDbParameter use SQLParameter

Expert Comment

ID: 24356686
LVL 41

Expert Comment

ID: 24357006

Trust the message, @Blob is not defined. Your problem seems to be here:
 cmd.CommandText = _
        "INSERT INTO tbl_Object(VersionId,Component,Blob) VALUES (" + CStr(lVersionNo) + "," + _
                                                                  "'" & strFile & "'," + _

 Try defining the blob variable, and then change the insert statement. (Because blob will be a string, then you need to make sure you have in between quotes.

DIM blob as string

cmd.CommandText = _
        "INSERT INTO tbl_Object(VersionId,Component,Blob) VALUES (" + CStr(lVersionNo) + "," + _
                                                                  "'" & strFile & "'," + "''" + blob+ "'')"
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 24357423
with oledb, you don't write @var in the sql:
  cmd.CommandText = _
        "INSERT INTO tbl_Object(VersionId,Component,Blob) VALUES (" + CStr(lVersionNo) + "," + _
                                                                  "'" & strFile & "', ? )"
        cmd.Connection = dbConnection

Open in new window


Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

636 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