Click here to Skip to main content
12,998,363 members (55,012 online)
Rate this:
 
Please Sign up or sign in to vote.
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"."

    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 3-Oct-12 19:04pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

When using OleDb the parameters in the SQL are to be set to ? and not @bytImage.
try this
commSQL.CommandText = "INSERT INTO [PICS] ([IMAGE]) VALUES (?)"
  Permalink  
v2
Comments
ggoutam7 4-Oct-12 1:40am
   
Thank you.
I removed that extra bracket and still have the error.
Santhosh Kumar J 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 J 4-Oct-12 2:18am
   
is that working?
ggoutam7 4-Oct-12 2:59am
   
Yes. Thank you.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170622.1 | Last Updated 16 Apr 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100