Click here to Skip to main content
15,883,739 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a program with search button, picture box, and textbox for file image path, i want to search invoice number and show the pictures and picture path in textbox but the code won't to process, can you help me with this codes??

What I have tried:

Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search.Click
        command.Connection = con
        Try
            str = "Select * from datagambar Where NoFaktur = @nofaktur"
            command = New OleDbCommand(str, con)
            command.Parameters.Add("@nofaktur", OleDbType.VarChar).Value = nofaktur.Text
            command.ExecuteNonQuery()
            Call showimage1()
            command.Dispose()
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub showimage1()
        PictureBox1.Visible = True
        PictureBox1.Image = Image.FromFile("G:\Aplikasi\Photo\Faktur Invoice")
    End Sub
Posted
Updated 7-Sep-18 3:40am
v2

1 solution

ExecuteNonQuery is for executing commands like INSERT, UPDATE, or DELETE, which don't return any data.

To execute a SELECT query and read the results, use the ExecuteReader method[^] instead.
VB.NET
Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search.Click
    Using command As New OleDbCommand("SELECT * FROM datagambar WHERE NoFaktur = @nofaktur", con)
        command.Parameters.Add("@nofaktur", OleDbType.VarChar).Value = nofaktur.Text
        
        Try
            con.Open()
            
            Using reader As OleDbDataReader = command.ExecuteReader()
                If reader.Read() Then
                    Dim imagePath As String = reader.Field(Of String)("Your image path column name")
                    If String.IsNullOrEmpty(imagePath) Then
                        PictureBox1.Visible = False
                    Else
                        PictureBox1.Visible = True
                        PictureBox1.Image = Image.FromFile(imagePath)
                    End If
                    
                    ... Load other columns into the UI here ...
                    
                Else
                    ... No match: tell the user, and clear the UI here ...
                End If
            End Using
            
        Catch ex As System.Data.Common.DbException
            MsgBox(ex.Message)
        
        Finally
            con.Close()
        End Try
    End Using
End Sub
 
Share this answer
 

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