E Crux
E Crux

Reputation: 83

MySQL syntax error on image upload

I have a small file upload on a website that is supposed to upload a selected image and place it in a mysql database. The image is stored as type a varbinary (8000 length). My code is:

 Public Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If fileupload1.HasFile Then

        Dim pimage As Byte() = fileupload1.FileBytes
        Dim pid As String = partnum.Text.ToString
        Dim sConnection As String = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=inteva; UID=root; PASSWORD=root; OPTION=3"
        Dim connectme As New OdbcConnection(sConnection)
        Dim sInsertInto As String = "INSERT INTO images(serial, image1) VALUES(?pserial, ?ppimage)"
        Dim com As New OdbcCommand(sInsertInto, connectme)

        com.Parameters.Add("pserial", OdbcType.VarChar).Value = pid
        com.Parameters.Add("?ppimage", OdbcType.VarBinary).Value = pimage

        connectme.Open()
        Dim result As Integer = com.ExecuteNonQuery()
        connectme.Close()

        If result > 0 Then
            lblimageup.Text = "Image saved."
        End If

    Else
        lblimageup.Text = "Please select an image file"

    End If
End Sub

When I execute this, I get a sytax error as follows:

ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.6.11]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'pserial, 'ÿØÿá/þExif\0\0MM\0*\0\0\0\0\0\0\0\0\0\0\0’\0\0\0\0   \0\' at line 1

I can't seem to find any syntax error. Any ideas? Could it be the image file have something in it breaking the syntax?

Upvotes: 0

Views: 124

Answers (2)

Damith
Damith

Reputation: 63095

try with below code, use @paramName in both SQL and Patameters.Add

Dim sInsertInto As String = "INSERT INTO images(serial, image1) VALUES (@pserial, @ppimage)"
Dim com As New OdbcCommand(sInsertInto, connectme)

com.Parameters.Add("@pserial", OdbcType.VarChar).Value = pid
com.Parameters.Add("@ppimage", OdbcType.VarBinary).Value = pimage

Upvotes: 1

keaton_fu
keaton_fu

Reputation: 442

This doesn't look consistent; I think you wanted to do "?pserial".

com.Parameters.Add("pserial", OdbcType.VarChar).Value = pid
com.Parameters.Add("?ppimage", OdbcType.VarBinary).Value = pimage

Upvotes: 0

Related Questions