0

I know this has been frequently asked by many here, but I wouldn't have asked if I haven't tried most of the solutions given on this website.

I'm having trouble reading images of receipts from ms-access to a picturebox in vb.net.

Here's the code I currently have.

    Dim i = DataGridView1.CurrentRow.Index
    Dim Dir As String = Path.GetDirectoryName(Application.ExecutablePath) & "\"
    Dim Connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Dir & "DB1.accdb")
    Dim SQL As String = "SELECT Receipt FROM [Inventory] WHERE ID=@DID"
    Dim command As New OleDbCommand(SQL, Connection)
    command.Parameters.AddWithValue("@DID", DataGridView1.Item(0, i).Value)

    Connection.Open()
    Using oleDBCmd As OleDb.OleDbCommand = Connection.CreateCommand()
        oleDBCmd.CommandType = CommandType.Text
        oleDBCmd.CommandText = SQL
        Using oleDbReader As OleDb.OleDbDataReader = oleDBCmd.ExecuteReader()
            oleDbReader.Read()
            Dim ImageBytes As Byte() = CType(oleDbReader(0), Byte())
            Dim ms As New MemoryStream(ImageBytes)
            FormReciept.Show()
            FormReceipt.PictureBox1.Image = Image.FromStream(ms)
        End Using
    End Using
    Connection.Close()

Based on the selected field of a specific item, it will retrieve its receipt based on the ID number, and preview it in another form where the picture box is.

Here's the error I'm getting.

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred 
in System.Data.dll
Additional information: No value given for one or more required parameters.

Any help would be appreciated. Thank you.

1 Answer 1

1

You are creating a command "command" with parameters, and then not using it...and instead creating a new command "oleDBCmd" which has no parameters added to it, which is the one you're actually executing. That's why you get the "No value given" error.

Try the following changes and see if you still get the error, or get a new error:

    Dim i = DataGridView1.CurrentRow.Index
    Dim Dir As String = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) & "\"
    Dim Connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Dir & "DB1.accdb")
    Dim SQL As String = "SELECT Receipt FROM [Inventory] WHERE ID=@DID"
    'Dim command As New OleDb.OleDbCommand(SQL, Connection)
    'command.Parameters.AddWithValue("@DID", DataGridView1.Item(0, i).Value)

    Connection.Open()
    Using oleDBCmd As OleDb.OleDbCommand = Connection.CreateCommand()
        oleDBCmd.CommandType = CommandType.Text
        oleDBCmd.CommandText = SQL
        'add parameter to the command you will actually be executing
        oleDBCmd.Parameters.AddWithValue("@DID", DataGridView1.Item(0, i).Value)
        Using oleDbReader As OleDb.OleDbDataReader = oleDBCmd.ExecuteReader()
            oleDbReader.Read()
            Dim ImageBytes As Byte() = CType(oleDbReader(0), Byte())
            Dim ms As New MemoryStream(ImageBytes)
            FormReciept.Show()
            FormReceipt.PictureBox1.Image = Image.FromStream(ms)
        End Using
    End Using
    Connection.Close()

Just commented out the "command" oledb command since it doesn't get executed, and added the parameter to "oleDBCmd".

Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm! Thank you so much.

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.