Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problem of "Syntax error in INSERT into Statement"

What I have tried:

Private Sub saveImage(sql As String)

      Try

          Dim arrImage() As Byte
          Dim mstream As New System.IO.MemoryStream()


          PictureBoxphoto.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)


          arrImage = mstream.GetBuffer()


          Dim FileSize As UInt32
          FileSize = mstream.Length

          mstream.Close()

          con.Open()

          cmd = New OleDbCommand
          With cmd
              .Connection = con
              .CommandText = sql
              .Parameters.AddWithValue("@img", arrImage)
              .ExecuteNonQuery()
          End With
      Catch ex As Exception
          MsgBox(ex.Message)
      Finally
          con.Close()
      End Try

  End Sub

Private Sub btnsavephoto_Click(sender As Object, e As EventArgs) Handles btnsavephoto.Click
       sql = "Insert into image(ID,img) Values('" & TextBoxpassport.Text & "',@img)"
       saveImage(sql)
       MsgBox("Image has been saved in the database")
   End Sub
Posted
Updated 25-Apr-20 3:29am

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Comments
Maciej Los 25-Apr-20 10:23am    
5ed!
Besides the SQL Injection vulnerability above... I don't believe that the OleDB class supports named parameters in the query; they each should be replaced with a ? (question mark) and then they need to be added in order.

I hodgepodged your code into one block, you can rearrange as needed
VB
	qry = "Insert into image(ID,img) Values(?, ?)"

	con.Open()
	cmd = New OleDbCommand(qry, con)
	With cmd
		.Parameters.AddWithValue("@img", TextBoxpassport.Text)
		.Parameters.AddWithValue("@img", arrImage)
		.ExecuteNonQuery()
	End With

Catch ex As Exception
	MsgBox(ex.Message)
Finally
	con.Close()
End Try
 
Share this answer
 
Comments
Maciej Los 25-Apr-20 10:23am    
5ed!
MadMyche 25-Apr-20 10:28am    
Thank you
Further to Solution 2 - there is an absolute maximum size for MS Access databases which is around 2GB. Storing actual photos in those databases is going to hit the max very, very quickly.

One alternative is to have a specific folder in which to store the images. Give each a unique name (e.g. a GUID) and store the relative location of the image in the database rather than the image itself.
 
Share this answer
 
Comments
Maciej Los 25-Apr-20 10:23am    
5ed!

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