Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 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:
VB
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
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

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:

VB.NET
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!
 
Share this answer
 
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)"
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900