Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello there am relatively new to vb.net(i am using vb.net 2008 specifically visual studio not c#)
i have been able to save images into an access database but my problem is to be able to retrieve the images from the database(using access 2003)
here is my saving code
VB
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If inc <> -1 Then
            Dim cmd As New OleDb.OleDbCommand(sql, con)
            Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
            Dim breader As New BinaryReader(fsreader)
            Dim imgbuffer(fsreader.Length) As Byte
            Dim ms As New MemoryStream()
            breader.Read(imgbuffer, 0, fsreader.Length)
            fsreader.Close()

            picPhoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)

            con.Open()
            sql = "INSERT INTO CONTACTS(AGENTNUMBER,FIRSTNAME,MIDDLENAME,SURNAME,GENDER,EMAIL,PHONE,ADDRESS,NOTES,PICTURE)"
            sql = sql & "VALUES(@AGENTNUMBER,@FIRSTNAME,@MIDDLENAME,@SURNAME,@GENDER,@EMAIL,@PHONE,@ADDRESS,@NOTES,@PICTURE)"
            cmd.CommandText = sql
            cmd.Connection = con
            cmd.Parameters.AddWithValue("@AGENTNUMBER", txtAgentNumber.Text)
            cmd.Parameters.AddWithValue("@FIRSTNAME", txtFirstName.Text)
            cmd.Parameters.AddWithValue("@MIDDLENAME", txtMiddleName.Text)
            cmd.Parameters.AddWithValue("@SURNAME", txtSurName.Text)
            cmd.Parameters.AddWithValue("@GENDER", cboGender.Text)
            cmd.Parameters.AddWithValue("@EMAIL", txtEmail.Text)
            cmd.Parameters.AddWithValue("@PHONE", txtPhone.Text)
            cmd.Parameters.AddWithValue("@ADDRESS", txtAddress.Text)
            cmd.Parameters.AddWithValue("@NOTES", txtNotes.Text)
            cmd.Parameters.AddWithValue("@PICTURE", ms.ToArray())
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            MsgBox("NEW RECORD ADDED")
            ds.AcceptChanges()
            btnSave.Enabled = False
            btnAdd.Enabled = True
            btnEdit.Enabled = True
            btnDelete.Enabled = True
        End If
    End Sub


here is my code for retrieving the image
Sub retrieve()

        Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PEOPLE.mdb")
            con.Open()
            Using cmd As New OleDbCommand("SELECT PICTURE FROM CONTACTS WHERE AGENTNUMBER='" & txtAgentNumber.Text & "'", con)
                Dim imageobj = cmd.ExecuteScalar
                If Not imageobj Is Nothing AndAlso Not imageobj Is DBNull.Value Then
                    Using ms As New System.IO.MemoryStream
                        Dim bm As Bitmap
                        Dim byteArray = DirectCast(imageobj, Byte())
                        ms.Write(byteArray, 0, byteArray.Length - 1)
                        bm = New Bitmap(ms)
                        picPhoto.Image = bm ---- here is where the problem is ((parameter not valid))

                    End Using
                End If
            End Using
        End Using
    End Sub


i have googled for the solution but i haven't gotten it
i have been stuck here for the past 1 month
Posted
Updated 26-Nov-13 4:26am
v3

1 solution

I'm not convinced you are writing it correctly at all: you read a file from teh disk (in about as complicated way as possible) and then ignore it completely, in favor of whatever happens to be in the picPhoto.Image at present...

If you want to save the disk file into teh DB, throw away this lot:
VB
Dim cmd As New OleDb.OleDbCommand(sql, con)
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim breader As New BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
Dim ms As New MemoryStream()
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()

picPhoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)

And replace it with:
VB
Dim data As Byte() = File.ReadAllBytes(OpenFileDialog1.FileName)
Then save it with:
VB
cmd.Parameters.AddWithValue("@PICTURE", data)

Try that and see if your DB data is any better. Remember that any existing values will be rubbish!
 
Share this answer
 
Comments
kef ngunjiri 26-Nov-13 11:44am    
ok let me try
kef ngunjiri 26-Nov-13 12:41pm    
yes it has worked(it has retrieved the picture)
i have a little problem i have saved several data and pictures(like several records)
and i want to navigate through records (that is move from one record to another(move to next,previous )including pictures),i can navigate through data but it brings the same problem that is 'parameter is invalid'
here is the code for navigation
Private Sub NavigateRecords()
Dim da As New OleDb.OleDbDataAdapter("SELECT*FROM CONTACTS", con)
Dim dt As New DataTable
da.Fill(dt)
If ds.Tables("PEOPLE").Rows.Count > 0 Then
txtAgentNumber.Text = ds.Tables("PEOPLE").Rows(inc).Item(1)
txtFirstName.Text = ds.Tables("PEOPLE").Rows(inc).Item(2)
txtMiddleName.Text = ds.Tables("PEOPLE").Rows(inc).Item(3)
txtSurName.Text = ds.Tables("PEOPLE").Rows(inc).Item(4)
cboGender.Text = ds.Tables("PEOPLE").Rows(inc).Item(5)
txtAddress.Text = ds.Tables("PEOPLE").Rows(inc).Item(6)
txtPhone.Text = ds.Tables("PEOPLE").Rows(inc).Item(7)
txtEmail.Text = ds.Tables("PEOPLE").Rows(inc).Item(8)
txtNotes.Text = ds.Tables("PEOPLE").Rows(inc).Item(9)
retrieve()
ds.AcceptChanges()
End If
End Sub
when i click next button or previous button
the invalid parameter error
Sub retrieve()

Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PEOPLE.mdb")
con.Open()
Using cmd As New OleDbCommand("SELECT PICTURE FROM CONTACTS WHERE AGENTNUMBER='" & txtAgentNumber.Text & "'", con)
Dim imageobj = cmd.ExecuteScalar
If Not imageobj Is Nothing AndAlso Not imageobj Is DBNull.Value Then
Using ms As New System.IO.MemoryStream
Dim bm As Bitmap
Dim byteArray = DirectCast(imageobj, Byte())
ms.Write(byteArray, 0, byteArray.Length - 1)
bm = New Bitmap(ms)
picPhoto.Image = bm------ the error appears here
End Using
End If
End Using
End Using
End Sub
OriginalGriff 26-Nov-13 13:25pm    
That's why I said " Remember that any existing values will be rubbish!"
You have to dump the existing images, and reload all of them - you can't recover the rubbish images that were written badly!
kef ngunjiri 26-Nov-13 14:34pm    
yap i did that i deleted all the original data that was there. that's and it brought or is there another way of navigating pictures? that i can apply i would really appreciate if u even gave me a hint
OriginalGriff 27-Nov-13 4:01am    
When you deleted them, did you replace them? If so, what with? Because if you replaced them with an empty byte array you will still get the same problem.
Have a look at the data your read back - log it to a file under a temporary file name - and check it to make sure it looks ok. That will at leats give yoiu an idea what is now causing your problem!

And please, stop concatenating strings to form an SQL command: it leave you wide open to SQL Injection attacks, which can damage of destroy your database...

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


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