Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey guys. So I'm trying to have a program which saves images to the database and also retrieves and displays the ones that are in the database. Here is my layout if it helps:

http://imageshack.us/photo/my-images/207/guiq.png/[^]

Following a tutorial, my program correctly browses, deletes and saves to the database. It also displays the first image correctly. Now the only part missing is moving through the database with the Next/Previous buttons and I also can't figure out how to load the name associated with the image to the textbox name... =(
Please help me with this?

Here's my code:

Imports System.IO
Imports System.Data.OleDb

Public Class Form1
    Dim dbConnection As New OleDbConnection
    Dim dbDA As New OleDbDataAdapter
    Dim dbCMD As New OleDbCommand
    Dim SQL As String

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

        dbConnection.ConnectionString = "provider=microsoft.ace.oledb.12.0; data source = |datadirectory|\database1.accdb;"
        dbConnection.Open()

        SQL = "select * from table1"

        Dim dt_images As New DataTable

        dbCMD.Connection = dbConnection
        acscmd.CommandText = CommandType.Text
        dbCMD.CommandText = SQL
        dbDA.SelectCommand = dbCMD
        dbDA.Fill(dt_images)

        For Each dr As DataRow In dt_images.Rows
            Dim img_buffer() As Byte
            img_buffer = CType(dr("picture"), Byte())
            Dim img_stream As New MemoryStream(img_buffer, True)

            img_stream.Write(img_buffer, 0, img_buffer.Length)

            PictureBox1.Image = New Bitmap(img_stream)
        Next

    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        If OpenFileDialog1.FileName <> Nothing Or OpenFileDialog1.FileName <> "" Then
            txtname.Text = OpenFileDialog1.FileName.Substring( _
            OpenFileDialog1.FileName.LastIndexOf("\") + 1, _
            (OpenFileDialog1.FileName.IndexOf(".", 0) - (OpenFileDialog1.FileName.LastIndexOf("\") + 1)))

        End If
    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click

        OpenFileDialog1.Filter = "image file (*.jpg, *.bmp, *.png) | *.jpg; *.bmp; *.png| all files (*.*) | *.* "
        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)

        Dim breader As New BinaryReader(fsreader)

        Dim imgbuffer(fsreader.Length) As Byte
        breader.Read(imgbuffer, 0, fsreader.Length)
        fsreader.Close()

        SQL = "insert into table1 (name, picture) values (@dtaname, @dtapic)"
        dbCMD.CommandText = SQL
        dbCMD.Connection = dbConnection
        dbCMD.Parameters.AddWithValue("@dtaname", txtname.Text)
        dbCMD.Parameters.AddWithValue("@dtapic", imgbuffer)
        dbCMD.ExecuteNonQuery()
        dbCMD.Dispose()
        MsgBox("Saved")
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        SQL = "delete * from table1 where name ='" & txtname.Text & "'"
        dbCMD.CommandText = SQL
        dbCMD.Connection = dbConnection
        dbCMD.ExecuteNonQuery()
        dbCMD.Dispose()
        MsgBox("deleted")
    End Sub

End Class
Posted
Updated 11-Dec-11 3:52am
v3

1 solution

For Navigation in DataBase you can use DataView and CurrencyManager objects.DataView gives you faster accessing of Data from DataBase and CurrencyManager helps you to Navigate from one record to other.To understand how to use this See :
Using ADO.NET for beginners[^]
http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager(v=vs.71).aspx[^]
http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager.aspx[^]
 
Share this answer
 
Comments
Member 8175631 11-Dec-11 9:51am    
Sir, that didn't really help much... =(

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