Private Sub LVlist_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LVlist.SelectedIndexChanged
        If LVlist.SelectedItems.Count > 0 Then

            With LVlist.SelectedItems(0)
                VarIdentifier = .SubItems(0).Text
                lblname.Text = .SubItems(0).Text
            End With
            pix()

        End If

end sub

Sub pix()
        sql = "select pic from tbldishlist where nod = '" & lblname.Text & "'"

        Dim acscmd = New OleDb.OleDbCommand(sql, Con)
        acsdr = acscmd.ExecuteReader()

        acsdr.Read()

        If acsdr!pic <> "" Then
            PictureBox1.Load(imgPath & acsdr!pic)
        Else : PictureBox1.Load(imgPath & "add_user.jpg")

        End If
        acsdr.Close()


    End Sub

Recommended Answers

All 20 Replies

I suggest the following method to display a picture in a PictureBox from a file

Dim fs As New System.IO.FileStream(FileList(indx), IO.FileMode.Open, IO.FileAccess.Read)
pbxPicture.Image = System.Drawing.Image.FromStream(fs)

If you load the picture directly (without using a FileStream) you end up locking the file. Using a FileStream releases the file once it has been loaded and the fs goes out of scope.

I'm in the process of rebuilding my system and after I get SQL Express loaded up I'll try to post some code to load the iimage from a database.

@Reverend Jim,ok,thank you so much.
the picture is save in database and what i want is to when i click the lvlist, the pic need to display in the picturebox using the identifier 'nod'.

You need to use Stream as @Jim has said. Check out this good example I found it here:

 net-informations.com/vbprj/dataset/retrieve-image.htm

And here is a step by step guide too:

 support.microsoft.com/kb/321900

@Mr.M ,thank you , i'll check it out :)

I don't know what kind of database you are using. If it is MS SQL then you can use SQLDB as follows to retrieve and display the image

Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Private Sub btnRetrieve_Click(sender As System.Object, e As System.EventArgs) Handles btnRetrieve.Click

    Dim con As New SqlConnection(ConnStr)
    Dim cmd As New SqlCommand("SELECT picdata FROM test WHERE picname = '" & imgname & "'", con)

    con.Open()
    Dim rdr As SqlDataReader = cmd.ExecuteReader

    If rdr.Read Then
        Dim imgdata() As Byte = rdr("picdata")
        Dim str As New MemoryStream(imgdata)
        pbxImage.Image = Image.FromStream(str)
        str.Close()
    End If

    rdr.Close()
    con.Close()

End Sub

For this example I am using a test database created as follows

CREATE TABLE [dbo].[test](
    [picname] [varchar](50) NOT NULL,
    [picdata] [image]       NOT NULL,
CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED 

Here is the OleDB version

Private Sub btnOleDb_Click(sender As System.Object, e As System.EventArgs) Handles btnOleDb.Click

    Dim con As New OleDbConnection("Provider=SQLNCLI10;Server=.\SQLEXPRESS;Database=mydb;Trusted_Connection=Yes;Connect Timeout=15;")
    Dim cmd As New OleDbCommand("SELECT picdata FROM test WHERE picname = '" & imgname & "'", con)

    con.Open()
    Dim rdr As OleDbDataReader = cmd.ExecuteReader

    If rdr.Read Then
        Dim imgdata() As Byte = rdr("picdata")
        Dim str As New MemoryStream(imgdata)
        pbxImage.Image = Image.FromStream(str)
        str.Close()
    End If

    rdr.Close()
    con.Close()

End Sub

@Reverend Jim. thank you so much. I will try your code, i really appreciate it. by the way, i'm usin oledb database since i'm a starter, its easy for me understand te oledb more than the Ms sql.Again, thank you very much :D

If you are using MS SQL as your database then you should be using SQLDB rather than OLEDB. Both are built on top of ADODB (which you could also use), however, SQLDB is optimized for MS SQL. OLEDB is used for other data sources. In terms of complexity I don't see any difference between SQLDB and OLEDB. If you are using MS SQL, but plan to (possibly) use other data sources then stick with OLEDB and just change the connection string as required.

commented: Mind-blowing +5

@Reverend Jim .wow, im speechless, i thought it was really hard, but in terms of large data storing , is sqldb is better than in oledb?

No difference. SQLDB and OLEDB are merely interfaces. The actual data storage is a function of the underlying database. Consider the database like a huge parking lot and SQLDB and OLEDB the highway you use to get vehicles to/from the parking lot. For small to medium databases (I can't actually define what constitutes small or medium), something like Access is sufficient.

Before I retired, one of our developers built an application in Access. Unfortunately, once the amount of data got to a certain size (don't recall what that was) I would get a 3:00 AM call about once a week from the control centre saying that the application was broken. For some unknown reason, it required a database repair/rebuild about once a week. Also, unfortunately, the control centre staff required 24 hour availability. Fortunately a 3:00 AM call meant two hours double time pay for me. We never had this problem with our SQL databases even though they were magnitudes bigger.

Microsoft sunk a few billion dollars into developing MS SQL and a few hundred million into Access. Which do you think is more robust?

Please don't forget to mark this as solved if you got what you needed.

@Reverend Jim, i've got error, it says parameter is not valid
here's my code:

'code when i click the listview

    Private Sub lvdlist_Click(sender As Object, e As System.EventArgs) Handles lvdlist.Click
        With lvdlist.SelectedItems(0)
            VarIdentifier = .SubItems(0).Text
            txtnod.Text = .SubItems(0).Text
            mtnum.Text = .SubItems(1).Text
            cbcountry.Text = .SubItems(2).Text
            cbtype.Text = .SubItems(3).Text
            Label7.Text = .SubItems(0).Text

        End With
        picx()

    End Sub

'code for picture
   Sub picx()
        Dim cmd As New OleDbCommand("SELECT pic FROM tbldishlist WHERE nod = '" & lvdlist.Items(0).SubItems(0).Text & "'", con)
        'con.Open()
        Dim rdr As OleDbDataReader = cmd.ExecuteReader
        If rdr.Read Then
            Dim imgdata() As Byte = rdr("pic")
            Dim str As New MemoryStream(imgdata)
            PictureBox1.Image = Image.FromStream(str)
            str.Close()
        End If
        rdr.Close()
        'con.Close()
    End Sub

'code when i browse the pic
 Private Sub btnbrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnbrowse.Click
        Dim dlgImage As FileDialog = New OpenFileDialog()
        With (dlgImage)
            '.InitialDirectory = "C:\"
            .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
            .FilterIndex = 4
        End With

        If dlgImage.ShowDialog() = DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(dlgImage.FileName)
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            PictureBox1.BorderStyle = BorderStyle.Fixed3D

            fileinfor = New FileInfo(dlgImage.FileName)
            filenames = fileinfor.Name
        End If
    End Sub

'code for save
Private Sub btnsave_Click(sender As System.Object, e As System.EventArgs) Handles btnsave.Click
        sql = "insert into tbldishlist (nod,price,dfrom,type,pic) values ('" _
           & txtnod.Text & "','" _
           & txtprice.Text & "','" _
           & cbdfrom.Text & "','" _
           & cbtype.Text & "','" _
           & filenames & "')"
        Dim cmd = New OleDb.OleDbCommand(sql, con)
        cmd.ExecuteNonQuery()
        MsgBox("save")
        Me.Refresh()
    End Sub

i declare this in public class

Dim fileinfo As FileInfo, filenamexls As String, msginsert As String = "Record has been Added"
    Dim currntschyr As String
    Dim imgPath As String = System.IO.Directory.GetCurrentDirectory & "\..\..\images\"
    Dim fileinfor As FileInfo, filenames As String = "add_user.jpg"

error in line 25

Do you mean this line 25

PictureBox1.Image = Image.FromStream(str)

If the error is in

cmd.ExecuteNonQuery()

then please post the value of the string sql.

Dim cmd As New OleDbCommand("SELECT pic FROM tbldishlist WHERE nod = '" & lvdlist.Items(0).SubItems(0).Text & "'", con)

the error is in line 25 [ PictureBox1.Image = Image.FromStream(str)]

It's possible that imgdata contains no data (or invalid data). Put a breakpoint at

PictureBox1.Image = Image.FromStream(str)

and check the length of imgdata

If the database contains valid image data then this should work. I tested it locally before I posted the code.

how do i check the length of imgdata?

Two ways

UBound(imgdata)

gives you the index of the last byte and

imgdata.Length

gives you the number of bytes. The first value will be one less than the second value.

Its ok now, thank you guys so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.