Solved

Must declare the scalar variable  Error

Posted on 2009-05-11
4
1,345 Views
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?

Thanks
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 & "'," + _
                                                                  "@BLOB)"
 
        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.Close()
 
        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
 
        cmd.Parameters.Add(P)
 
        dbConnection.Open()
        cmd.ExecuteNonQuery()
        dbConnection.Close()
 
    End Sub

Open in new window

0
Comment
Question by:FINDERSOL
[X]
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
4 Comments
 
LVL 9

Expert Comment

by:ezraa
ID: 24356683
instead of OleDbParameter use SQLParameter
0
 
LVL 9

Expert Comment

by:ezraa
ID: 24356686
0
 
LVL 41

Expert Comment

by:ralmada
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 & "'," + _
                                                                  "@BLOB)"

 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+ "'')"
                                                             
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24357423
with oledb, you don't write @var in the sql:
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx
  cmd.CommandText = _
        "INSERT INTO tbl_Object(VersionId,Component,Blob) VALUES (" + CStr(lVersionNo) + "," + _
                                                                  "'" & strFile & "', ? )"
 
        cmd.Connection = dbConnection
 

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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