Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I am trying to fix this bug. Please help.

I am getting below error and no data inserted :
"Must declare the scalar variable "@bytImage"."

VB
Public Sub InsertFile()
    Dim sMainFolder As String = "D:\Project"
    Dim conAccess As Data.OleDb.OleDbConnection
    Dim commSQL As Data.OleDb.OleDbCommand = New Data.OleDb.OleDbCommand()
    Dim bytImage As Byte()
    Dim QueryParameter As New Data.OleDb.OleDbParameter
    QueryParameter.DbType = Data.DbType.Binary

    Try
        If Me.fulFileUpload.HasFile = True Then
            bytImage = Me.fulFileUpload.FileBytes
            QueryParameter.Value = bytImage

            conAccess = New Data.OleDb.OleDbConnection
            conAccess.ConnectionString = "File Name=" & sMainFolder & "\" & "Project.udl"
            conAccess.Open()
            commSQL.Connection = conAccess
            commSQL.CommandText = "INSERT INTO [PICS] ([IMAGE]) VALUES (@bytImage))"

            ' ''QueryParameter = commSQL.Parameters.Add("@bytImage", Data.OleDb.OleDbType.Binary)
            ' ''QueryParameter.Direction = Data.ParameterDirection.Input
            ' ''commSQL.Parameters.Add(QueryParameter)
            commSQL.Parameters.AddWithValue("@bytImage", bytImage)

            commSQL.ExecuteNonQuery()
            conAccess.Close()
            commSQL.Dispose()
        End If
    Catch ex As Exception
        'ex.Message = "Must declare the scalar variable "@bytImage"."
    End Try
End Sub


Thank you in advance,
~ Goutam
Posted

When using OleDb the parameters in the SQL are to be set to ? and not @bytImage.
try this
VB
commSQL.CommandText = "INSERT INTO [PICS] ([IMAGE]) VALUES (?)"
 
Share this answer
 
v2
Comments
ggoutam7 4-Oct-12 1:40am    
Thank you.
I removed that extra bracket and still have the error.
Santhosh Kumar Jayaraman 4-Oct-12 1:44am    
updated the solution.Check now
ggoutam7 4-Oct-12 2:17am    
Hello,

Thank you. It can work now.
Modified code as below :

<pre lang="vb"> Public Sub InsertFile()
Dim sMainFolder As String = "D:\Project"
Dim conAccess As Data.OleDb.OleDbConnection
Dim commSQL As Data.OleDb.OleDbCommand = New Data.OleDb.OleDbCommand()
Dim bytImage As Byte()
Dim QueryParameter As New Data.OleDb.OleDbParameter
QueryParameter.DbType = Data.DbType.Binary

Try
If Me.fulFileUpload.HasFile = True Then
bytImage = Me.fulFileUpload.FileBytes
QueryParameter.Value = bytImage

conAccess = New Data.OleDb.OleDbConnection
conAccess.ConnectionString = "File Name=" & sMainFolder & "\" & "Project.udl"
conAccess.Open()
commSQL.Connection = conAccess
commSQL.CommandText = "INSERT INTO [PICS] ([IMAGE]) VALUES (?)"

QueryParameter = commSQL.Parameters.Add("@bytImage", Data.OleDb.OleDbType.Binary)

commSQL.ExecuteNonQuery()
conAccess.Close()
commSQL.Dispose()
End If
Catch ex As Exception

End Try
End Sub
</pre>

Thank you,
~ Goutam
Santhosh Kumar Jayaraman 4-Oct-12 2:18am    
is that working?
ggoutam7 4-Oct-12 2:59am    
Yes. Thank you.
SQL
DECLARE @DateFormat INT

SET @DateFormat = 0
WHILE @DateFormat < 15
BEGIN
    PRINT CONVERT(VARCHAR(30), GETDATE(), @DateFormat)
    SET @DateFormat = @DateFormat + 1
END

SET @DateFormat = 100
WHILE @DateFormat < 115
BEGIN
    PRINT CONVERT(VARCHAR(30), GETDATE(), @DateFormat)
    SET @DateFormat = @DateFormat + 1
END
GO
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900