Click here to Skip to main content
13,663,987 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I have an error when I want load image from access.

Error: "System.ArgumentException: 'Parameter is not valid.'"

What I have tried:

My code:
Dim RetVal As Long
Dim FieldLen As Int32

Dim AccessConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & Application.StartupPath & "\mochis.mdb;User Id=admin;Password=;")
Dim AccessCommand As New OleDb.OleDbCommand("SELECT image FROM catalogo WHERE codigo = '" & TextBox1.Text & "'", AccessConnection)

AccessConnection.Open()
Dim AccessDataReader As OleDb.OleDbDataReader = AccessCommand.ExecuteReader(CommandBehavior.SequentialAccess)
AccessDataReader.Read()
FieldLen = AccessDataReader.Item(0).Length
Dim PictureByteArray(FieldLen - 1) As Byte
Dim startIndex As Integer = 0
RetVal = AccessDataReader.GetBytes(0, startIndex, PictureByteArray, 0, PictureByteArray.Length)
Dim BLOBDataStream As New MemoryStream(PictureByteArray)
Me.PictureBox1.Image = Image.FromStream(BLOBDataStream)

AccessDataReader.Close()
AccessConnection.Close()
Posted 14-Jun-18 12:05pm
Updated 14-Jun-18 23:21pm
Comments
Richard MacCutchan 15-Jun-18 4:14am
   
You have an invalid parameter somewhere in your code, but you have not told us where. You are also leaving your SQL open to SQL injection errors.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

First of all, you have to use parameterized queries, instead of concatenated string, because your code is SQL Injection[^] vulnerable

If you would like only image, i'd do that this way:

Dim sDbPath As String = Application.StartupPath & "\mochis.mdb"
Dim sConn As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;", sDbPath)
Dim ms As MemoryStream = New MemoryStream()
Dim sSql As String = "SELECT image FROM catalogo WHERE codigo=@codigo"
Using oConn As OleDbConnection = New OleDbConnection(sConn)
    oConn.Open()
    Using oComm As OleDbCommand = New OleDbCommand(sSql, oConn)
        oComm.Parameters.Add("@codigo", OleDbType.VarChar).Value = TextBox1.Text;
        Dim imagedata As Byte() = oComm.ExecuteScalar()
        ms.Write(imagedata, 0, imagedata.Length)
        ''load image from stream!
        Me.PictureBox1.Image = Image.FromStream(ms)
        oComm.Dispose()
    End Using
    oConn.Close()
    oConn.Dispose()
End Using


Note: to be able to use this code, you have to install MS Access database 2010 driver[^]

For further details about connection, please see: Access connection strings - ConnectionStrings.com[^]

Good luck!
  Permalink  
v4
Comments
Member 12429318 15-Jun-18 17:17pm
   
Hi, I have the same error: "System.ArgumentException: 'Parameter is not valid.'" in this line: "Me.PictureBox1.Image = Image.FromStream(ms)"
0x01AA 16-Jun-18 9:38am
   
Hi Maciej
I'm not sure but I think you forgot something like oComm.Parameters["@codigo"].Value = theCodigoValue; ?
And most probably also a check whether ExecuteScalar Returns a result...
Maciej Los 18-Jun-18 2:10am
   
Good point, Bruno!

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 | Cookies | Terms of Service
Web06-2016 | 2.8.180810.1 | Last Updated 18 Jun 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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