0

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

This corrected the syntax error, but now I get an error saying "serial cannot be null" as it is my primary key in this database. There is a value being passed to pid, but I guess the parameter isn't picking this up. Why not?
@ECrux do you have value for pid? what id the data type in db?
I do have the value in the textbox. The data type is varchar
@ECrux can you generate the create table script for your table and update the question with that? and also debug and see the value you get as pid before ExecuteNonQuery
0

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

1 Comment

Ah, sorry I pasted the wrong param. I had already corrected the param to ?pserial. I still have the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.