dcsimg
Results 1 to 2 of 2

Thread: Load images from database

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    1

    Post Load images from database

    Hello guys,

    I was hoping you could help me out with an issue i have in vb.net. I am new to this and i'm still learning as i go. I am trying to create a simple UI which can show various items when the user is searching. So far i can connect and query the database, search for the desired item, and it will load it into a LV.
    The hard part that i haven't figured out yet, and this is where I would appreciate some help is with the following:

    When the user searches for a specific item, I want to show the name of that item or similar name with a picture next to it and some additional details that can be found in the database. My current code is this:

    Code:
    Imports MySql.Data.MySqlClient
    
    
    
    Public Class Form1
        Dim mysqlConn As MySqlConnection
    
    
        Private Sub buttonConn_Click(sender As Object, e As EventArgs) Handles buttonConn.Click
            mysqlConn = New MySqlConnection With {
                .ConnectionString = ("Server=localhost;userid=*;password=*;database=*")
            }
    
            Try
                mysqlConn.Open()
                MessageBox.Show("Connection Successful")
                mysqlConn.Close()
            Catch ex As MySqlException
                MessageBox.Show(ex.Message)
    
            Finally
                mysqlConn.Dispose()
            End Try
    
        End Sub
    
    
        Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles buttonsearch.Click
            mysqlConn = New MySqlConnection
    
    
            Dim strAddressSearch As String
    
            strAddressSearch = TextBox1.Text
    
    
            mysqlConn.ConnectionString = ("Server=localhost;userid=*;password=*;database=*")
    
            Dim search_command As New MySqlCommand("SELECT `fruit` FROM `items` WHERE `name` LIKE  ""%" & strAddressSearch & "%""", mysqlConn)
    
            search_command.Parameters.Add(strAddressSearch, MySqlDbType.Text).Value = TextBox1.Text
    
            Dim adapter As New MySqlDataAdapter(search_command)
    
            Dim table As New DataTable()
    
            Try
    
                adapter.Fill(table)
    
    
                If table.Rows.Count > 0 Then
    
                    With ListView1
                        .View = View.Details
                        .GridLines = True
                        .Width = 400
                        .Columns.Clear()
                        .Items.Clear()
                    End With
    
                    For Each col As DataColumn In table.Columns
                        ListView1.Columns.Add(col.ToString)
                    Next
    
                    For Each row As DataRow In table.Rows
                        Dim lst As ListViewItem = ListView1.Items.Add(If(row(0) IsNot Nothing, row(0).ToString, String.Empty))
    
                        For i As Integer = 1 To table.Columns.Count - 1
                            lst.SubItems.Add(If(row(i) IsNot Nothing, row(i).ToString, String.Empty))
                        Next
                    Next
                Else
                    MsgBox("The item you are searching for doesn't exist in the database. Please check if your spelling is correct and try again.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Title:="Item not found.")
    
                End If
    
            Catch ex As Exception
    
                MessageBox.Show(ex.Message)
    
            End Try
    
        End Sub
    
    
        Function ExecCommand(ByVal cmd As MySqlCommand) As Boolean
    
            If mysqlConn.State = ConnectionState.Closed Then
                mysqlConn.Open()
            End If
    
            Try
                If cmd.ExecuteNonQuery() = 1 Then
                    Return True
    
                Else
                    Return False
                End If
            Catch ex As Exception
    
                MessageBox.Show(ex.Message)
                Return False
    
            End Try
    
            If mysqlConn.State = ConnectionState.Open Then
                mysqlConn.Close()
            End If
    
        End Function
        Private Sub Search()
            mysqlConn = New MySqlConnection
    
    
            Dim strAddressSearch As String
    
            strAddressSearch = TextBox1.Text
    
            mysqlConn.ConnectionString = ("Server=localhost;userid=*;password=*;database=*")
    
            Dim search_command As New MySqlCommand("SELECT `fruit` FROM `items` WHERE `name` LIKE  ""%" & strAddressSearch & "%""", mysqlConn)
    
            search_command.Parameters.Add(strAddressSearch, MySqlDbType.Text).Value = TextBox1.Text
    
            Dim adapter As New MySqlDataAdapter(search_command)
    
            Dim table As New DataTable()
    
            Try
    
                adapter.Fill(table)
    
    
                If table.Rows.Count > 0 Then
    
    
                    With ListView1
                        .View = View.Details
                        .GridLines = True
    
                        .Columns.Clear()
                        .Items.Clear()
                    End With
    
                    For Each col As DataColumn In table.Columns
                        ListView1.Columns.Add(col.ToString)
                    Next
    
                    For Each row As DataRow In table.Rows
                        Dim lst As ListViewItem = ListView1.Items.Add(If(row(0) IsNot Nothing, row(0).ToString, String.Empty))
    
                        For i As Integer = 1 To table.Columns.Count - 1
                            lst.SubItems.Add(If(row(i) IsNot Nothing, row(i).ToString, String.Empty))
                        Next
                    Next
                Else
                    MsgBox("The item you are searching for doesn't exist in the database. Please check if your spelling is correct and try again.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Title:="Item not found.")
    
                End If
    
            Catch ex As Exception
    
                MessageBox.Show(ex.Message)
    
            End Try
        End Sub
    
    
        Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    
            If e.KeyCode = Keys.Enter Then
                e.SuppressKeyPress = True
                Call Search()
    
            End If
    
        End Sub
    And i am looking for something like this, i have found on the internet:
    Name:  searchitem.jpg
Views: 129
Size:  35.9 KB

    And for your information, i am storing the pictures on my local drive, and in the database stored as url/paths to the files. Ex: "G:\Images\Apple_Red_01"
    I thank you all in advance for any hints or small code sharing you could offer. Maybe there is a simple solution to this, that i haven't figured yet out.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,754

    Re: Load images from database

    Quote Originally Posted by stalker989 View Post
    i am storing the pictures on my local drive, and in the database stored as url/paths to the files.
    If that's the case then the title of this thread is false. You're not loading images from a database. You're loading strings from a database and then loading images from the file system. Neither of those things is difficult. You're already loading strings from the database to populate your ListView so you don't need help with that. It would take a few seconds to find information on loading an image from a file in VB.NET with a web search so that's not a problem either. What is the actual problem?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width