Saving image path to access database and retrieving images [Solved/Closed]

Posts
25
Registration date
Thursday November 1, 2012
Last seen
March 27, 2014
- Nov 9, 2012 at 04:46 AM - Latest reply:
Posts
1
Registration date
Sunday November 30, 2014
Last seen
November 30, 2014
- Nov 30, 2014 at 04:11 AM
Hello,

Hello guys please I am stock with how to save an image path to my database please could anyone help. I have a picture box and a filedailogue, I select a picture which appears on the picture box the I like to save its path to my database while saving the picture to a folder

See more 

5 replies

Best answer
Posts
2421
Registration date
Sunday September 23, 2012
Last seen
July 16, 2018
- Nov 13, 2012 at 12:42 AM
5
Thank you
Hi mikeoe2003,

I have designed this small application for you that might suite your need. This application has an Open Button which will help you open any picture file to a PictureBox on the form using OpenFileDialog. You will see the path of the picture file in a disabled TextBox. When you click the update button the Picture's path is saved to an Access Database.

Follow the steps below to create a similar project for yourself:

1. Create a new Visual Basic .net project. Select Windows Forms Application from New Project Dialog Box. Name this application as mikeoe2003PictureApplication.
2. Create the following with below mentioned properties:
a. Form - (Name): mikeoe2003PictureApplication, Text: FormPictureApplication
b. PictureBox - (Name): PictureBox1, SizeMode: StretchImage
c. Button - (Name): ButtonUpdate, Text: &Update
d. Button - (Name): ButtonOpen, Text: &Open
e. TextBoxt - (Name): TextBoxPictureFilePath, Enabled: False
3. Double Click the Form, insert the following code right above Public Class mikeoe2003PictureApplication:

Imports System.Data.OleDb
Imports System.IO
Imports Microsoft.Win32

Double Click ButtonOpen and insert the following code:

Dim img As String

Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()

openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = Nothing
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
openFileDialog1.FileName = ""

If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then

TextBoxPictureFilePath.Text = ""

img = openFileDialog1.FileName
PictureBox1.Image = System.Drawing.Bitmap.FromFile(img)

TextBoxPictureFilePath.Text = openFileDialog1.FileName

End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If

4. Create a Microsoft Access Database in your convenient location and name it as Databasemikeoe2003PictureApplication.mdb
5. Create a table with the name Tablemikeoe2003PictureApplication and add following Columns to it:

Id - Datatype: Autonumber
PicturePath - DataType: Memo (as file paths can be considerably long at times)

6. Double Click the UpdateButton and insert the following code:

Try
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim mySQLString As String
myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Databasemikeoe2003PictureApplication.mdb;")
myConnection.Open()
mySQLString = "INSERT INTO Tablemikeoe2003PictureApplication (PicturePath) VALUES('" & Replace$(TextBoxPictureFilePath.Text, "'", "''") & "')"
myCommand = New OleDbCommand(mySQLString, myConnection)
myCommand.ExecuteNonQuery()

PictureBox1.Image = Nothing
TextBoxPictureFilePath.Text = ""

Catch ex As Exception
MessageBox.Show(ex.Message & " - " & ex.Source)
End Try

7. Run the application, it should work as desired.

Please revert for clarification.

Thank you, Zohaib R 5

Something to say? Add comment

CCM has helped 1691 users this month

0
Thank you
thank you very much this was realy useful
Posts
25
Registration date
Thursday November 1, 2012
Last seen
March 27, 2014
- Jan 11, 2013 at 04:17 AM
0
Thank you
solved
Posts
1
Registration date
Thursday September 5, 2013
Last seen
September 5, 2013
- Sep 5, 2013 at 04:57 PM
0
Thank you
You may use access databases by the medium of access mdb repair tool

http://www.access.repairtoolboxx.com/
Posts
1
Registration date
Sunday November 30, 2014
Last seen
November 30, 2014
- Nov 30, 2014 at 04:11 AM
0
Thank you
its useful but can you help me how to view image from ms access database I have try your codes and its working...but how about viewing it?