Click here to Skip to main content
15,914,066 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am doing a program where I select a document type from the listbox and I got its image in the picturebox, but while running this program when I select a document I got its image in the picturebox,but when Iselect another document in the listbox I dint get its image. Please help.
;;;;;;;;;;;;;;;;;;;--- here's my code-----;;;;;;;;;;;;;;;;;
VB
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient
Public Class hbet_update
    Dim con As New SqlConnection("data source= (local); initial catalog= hbet; user id=sa; password=; multipleactiveresultsets=true")
    Dim sid As String
    Dim dt As DataTable
    Private Sub hbet_update_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            If con.State <> ConnectionState.Open Then
                con.Open()
            End If
            Dim cmd As New SqlCommand("select distinct studentid + ' ' + studentname,studentid from hbet.dbo.studentmaster ", con)

            Dim dr As SqlDataReader

            dr = cmd.ExecuteReader

            ComboBox1.Text = "---Select---"

            While dr.Read

                ComboBox1.Items.Add(dr(0))
            End While
            dr.Close()

            Dim cmd1 As New SqlCommand(" select distinct schoolname from hbet.dbo.studentmaster", con)
            Dim dr1 As SqlDataReader
            dr1 = cmd1.ExecuteReader
            While dr1.Read
                cb_school.Items.Add(dr1(0))
            End While
            dr1.Close()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        bindd()

        ' End If
    End Sub
    Private Sub bindd()
        GroupBox3.Visible = False

        'If Not IsNothing(ListBox1.SelectedItem) Then
        If con.State <> ConnectionState.Open Then
            con.Open()
        End If
        ListBox1.Items.Clear()

        Dim cmd As New SqlCommand
        Dim id As String
        id = ComboBox1.Text
        id = id.Substring(0, 8)

        Dim cmd1 As New SqlCommand("select CONVERT(NVARCHAR(10),SNO) + ' ' + doctype  from hbet.dbo.hbetscan where studentid= '" & id & "'", con)
        cmd1.CommandTimeout = 0
        Dim dr As SqlDataReader
        dr = cmd1.ExecuteReader
        While dr.Read
            ListBox1.Items.Add(dr(0))
        End While
        dr.Close()
    End Sub
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        tb_name.Enabled = False
        cb_school.Enabled = False
        cb_doctype.Enabled = False
        Dim test As String = ListBox1.Text
        tb_sno.Text = ListBox1.Text.Remove(ListBox1.Text.IndexOf(" "))
        If con.State <> ConnectionState.Open Then
            con.Open()
        End If
        GroupBox3.Visible = True
        Dim dr As SqlDataReader
        'Dim sid As String
        sid = ComboBox1.SelectedItem.ToString
        sid = sid.Substring(0, 9)
        ' school = ComboBox2.Text
        'dname = ListBox1.Items.Add(dr(0))
        Dim cmd As New SqlCommand("select * from studentmaster a, hbetscan b where a.studentid='" & sid & "' and a.studentid=b.studentid ", con)
        cmd.CommandTimeout = 0
        cmd.ExecuteNonQuery()
        dr = cmd.ExecuteReader
        If dr.Read Then
            tb_name.Text = dr("studentname")
            cb_school.Text = dr("schoolname")
            cb_doctype.Text = dr("doctype")

            Dim imgdata() As Byte = dr("image")
            Dim img As Image
            Dim ms As MemoryStream = New MemoryStream(imgdata, 0, imgdata.Length)
            ms.Write(imgdata, 0, imgdata.Length)
            img = Image.FromStream(ms, True)
            PictureBox1.Image = img
            PictureBox1.Image.Save("c:\" & "img1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            'dr.Close()
            con.Close()
        End If
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        ViewDoc.PictureBox1.Image = PictureBox1.Image
        ViewDoc.ShowDialog()
    End Sub


[Edit] Added PRE blocks for code[/Edit]
Posted
Updated 20-Mar-12 22:10pm
v2
Comments
Code Master38 21-Mar-12 18:07pm    
1 question what is your ViewDoc at the bottom of the code? I'm assuming its a function your calling that is NOT listed here?

1 solution

Well if it worked once it should work again. However I noticed on your

ListBox1_SelectedIndexChanged you saved the image but did not clear it?


Also I noticed a you did not close the memory stream in ListBox1_SelectedIndexChanged. Just something you might want to think about

So try this code

I'm not sure of the *EXACT* code here but I think you will figure it out.

VB
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
 
        tb_name.Enabled = False
        cb_school.Enabled = False
        cb_doctype.Enabled = False
        Dim test As String = ListBox1.Text
        tb_sno.Text = ListBox1.Text.Remove(ListBox1.Text.IndexOf(" "))
        If con.State <> ConnectionState.Open Then
            con.Open()
        End If
        GroupBox3.Visible = True
        Dim dr As SqlDataReader
        'Dim sid As String
        sid = ComboBox1.SelectedItem.ToString
        sid = sid.Substring(0, 9)
        ' school = ComboBox2.Text
        'dname = ListBox1.Items.Add(dr(0))
        Dim cmd As New SqlCommand("select * from studentmaster a, hbetscan b where a.studentid='" & sid & "' and a.studentid=b.studentid ", con)
        cmd.CommandTimeout = 0
        cmd.ExecuteNonQuery()
        dr = cmd.ExecuteReader
        If dr.Read Then
            tb_name.Text = dr("studentname")
            cb_school.Text = dr("schoolname")
            cb_doctype.Text = dr("doctype")
 
            Dim imgdata() As Byte = dr("image")
            Dim img As Image
            Dim ms As MemoryStream = New MemoryStream(imgdata, 0, imgdata.Length)
            ms.Write(imgdata, 0, imgdata.Length)
            img = Image.FromStream(ms, True)
            PictureBox1.Image = img
            PictureBox1.Image.Save("c:\" & "img1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            'dr.Close()
            con.Close()
'Close the memory stream
ms.Dispose()
        End If
 
Share this answer
 
v2

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