Storing and retrieving an image from a database file
(Visual Basic 2008 Express Edition)

One of the frequent problems encountered by most newbie database developers and designers is the difficulty in storing or retrieving an image data from a database file. Though there are a number of ways to store an image, the most comfortable way (for me) is to create a fieldname and store the URL (Uniform Resource Locator) or location and filename of an image as its value. One of the advantages of this approach is it allows you to access an image from a database file painlessly and swiftly compare to using ole objects. Prior to the image retrieval process, I want you to make a database file named “dbicon” containing a table named “tblicons” . This table will store the name and icon of a particular application. Use the following specifications:

Field Name
Data Type
Description
chrdesc
Text
This will store the name or descriptionof an application
chrimage
Text
This will store the icon of an application.I suggest that you use the maximum value for this field which is 255 because most image URLs contain multiple characters. Alternatively, you can use the memo data type.

To retrieve the image from the database file and display it into a picture box on your client application, use the ImageLocation property of the picture box then assign the value of your image fieldname as the ImageLocation’s value.

After designing the structure of your table, you can enter appropriate values for each field. For instance:

Chrdesc
chrimage
Apache Web Server
C:\Program Files\Apache Group\Apache\manual\images\favicon.ico
PHP
C:\Program Files\PHP\php.gif


Now that you are done designing your table, it’s time to retrieve the image from your database file and display it in a picture box on your client application. To achieve this, the idea is to make use of the ImageLocation property of the Picturebox and assign the value of your image fieldname to it. The following steps demonstrate how:

1. Click Start>Select All Programs then Click Visual Basic 2008 Express Edition. The Visual Basic 2008 Express Edition IDE should then appear.

2. Click the File menu then Select New Project.

3. The New Project dialog box should then come into view >Double-click Windows Forms Application from the available templates.

4. A new form will appear. Before adding appropriate controls to our form let us first establish a connection to our dbicons database file. To do this we will use a database access tool called OleDbDataAdapter. OleDbDataAdapter will enable us to set the filename of a database file and the name of the table that we wanted to be made available in our project. It also permits us to set-up how the record values will be displayed on our form by using an appropriate SQL statement.

5. By default, OleDbDataAdapter is not shown on the VB 2008 control toolbox, to add it, click Tools>Choose control toolbox>Type “ole” (no quotes ) on the filter textbox then check all the items that starts with “ole” and finally click the Ok button. OleDbDataAdapter should now appear on your toolbox.

6. Expand the All Windows Forms toolbox category then double-click OleDbDataAdapter.

7. A Data Adapter configuration wizard will then appear. Click the new connection button>Select Microsoft Access Database File from the data source list.

8. Click the browse button then locate your dbicons.mdb file then click the Ok button.

9. Click Next>A message box containing the following prompt will appear:
“The connection you selected uses a local data file that is not in the current project would you like to your project for the connection? If you copy the data file to your project, it will be copied to the project’s output directory”> just click the Ok button then click next.

10. A Generate SQL statement, type “SELECT * FROM tblicons” (no quotes) this will export all the record value of our table to our project then. Click Next after typing the SQL statement and finally click the Finish Button.

11. After specifying the table name, right-click OleDbDataAdapter1 from the bottom portion of VB 2008 IDE then click Generate Data Set then click the Ok button. If you are wondering what a dataset is, a Dataset is an imaginary box the holds the field names of your table. It is use a temporary storage box for table data.

12. At this point, you will need to design the interface of your application. Double-click the label tool. In the properties window, change its text to “Name:” (no quotes).

13. Double-click the textbox tool and put it in the right-side of your label. Change its name to Nametextbox.

14. Add another label and change its text property to “Icon” (no quotes).

15. Add a PictureBox and Position it below the Icon label. Change its name to IconPictureBox .

16. Now, double-click your form, the load procedure event should then come into view:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    
End Sub
End Class

17. Type the following codes between the Private Sub and End Sub:

'This will fill our dataset with record 
'values specified in our OleDbDataAdapter1
OleDbDataAdapter1.Fill(DataSet11)
'Displays the first record of the 
'first fieldname from our tblicons     table
NameTextBox.Text =DataSet11.Tables("tblicons").Rows(0).Item("chrname")
'Extract the URL of our image stored 
'on the first record value of our   chrimage field and
'store it as a value of our picturebox ImageLocation property
PictureBox1.ImageLocation =DataSet11.Tables("tblicons").Rows(0).Item("chrimage")
18. Your code will now look like this:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'This will fill our dataset with 
'record values specified in our OleDbDataAdapter1
   OleDbDataAdapter1.Fill(DataSet11)
'Displays the first record of the 
'first fieldname from our tblicons table
NameTextBox.Text = DataSet11.Tables("tblicons").Rows(0).Item("chrname")
'Extract the URL of our image stored 
'on the first record value of our   chrimage field and
'store it as a value of our 
'picturebox ImageLocation property
   PictureBox1.ImageLocation = DataSet11.Tables("tblicons").Rows(0).Item("chrimage")

    End Sub
End Class

18. Press F5 to see the culmination of your effort.

19. You should now see “Apache” beside the Name label and a quill icon beside the Icon label.