CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2008
    Posts
    142

    displaying image

    i have written small program to store image in database.
    i m using a button for save and a listbox
    code for saving:
    Code:
      Dim dlg As OpenFileDialog = New OpenFileDialog()
            dlg.Filter = "All Pictures|*.bmp;*.gif;*.jpg|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
            If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
    
                PictureBox1.Image = New Bitmap(dlg.FileName)
                Dim name As String = dlg.FileName.ToString
                'Substring(dlg.FileName.LastIndexOf("\") + 1, dlg.FileName.Length - dlg.FileName.LastIndexOf("\") - 1)
    
            End If
            Dim mstr As IO.MemoryStream = New IO.MemoryStream()
            PictureBox1.Image.Save(mstr, PictureBox1.Image.RawFormat)
            Dim arrImage As Byte() = mstr.GetBuffer()
            Dim cmd As String = "insert into tblImgData (ID,Name, Picture) values (@ID,@Name, @Pic)"
            Dim c As SqlConnection = New SqlConnection(c_string)
            Dim comm As SqlCommand = New SqlCommand(cmd, c)
            comm.Parameters.Add(New SqlParameter("@ID", SqlDbType.Int, 4)).Value = CInt(TextBox1.Text)
            comm.Parameters.Add(New SqlParameter("@Name", SqlDbType.VarChar, 40)).Value = Name
            comm.Parameters.Add(New SqlParameter("@Pic", SqlDbType.Image)).Value = arrImage
            Try
    
                c.Open()
                comm.ExecuteNonQuery()
    
            Catch err As SqlException
    
                MessageBox.Show(err.Message)
    
            Finally
    
                c.Close()
    
            End Try
    
            listBox1.Items.Add(Name)
        End Sub
    now when a image is saved in database ,this image is also added in listbox
    so when i click on an item in listbox then corresponding image does not displayed in picture box
    i m using selected index change.
    Code:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            If ListBox1.SelectedIndex < 0 Then
    
                MessageBox.Show("Please select a picture from the ListBox")
    
            Else
                
                Dim cmd As String = "select Picture from tblImgData where ID=" & ListBox1.SelectedIndex & ";"
                Dim cc As SqlConnection = New SqlConnection(c_string)
                Dim com As SqlCommand = New SqlCommand(cmd, cc)
               
                Try
    
                    cc.Open()
                    Dim b As Byte() = CType(com.ExecuteScalar(), Byte())
                    Dim mem As IO.MemoryStream = New IO.MemoryStream(b)
                    com.Parameters.Add("@ID", SqlDbType.Int, 4)
                    com.Parameters.Add("@Name", SqlDbType.VarChar, 40).Value = Name
                    com.Parameters.Add("@Pic", SqlDbType.Image).Value = b
                    PictureBox1.Image = Image.FromStream(mem)
    
                Catch ee As Exception
    
                    MessageBox.Show(ee.Message)
    
                Finally
    
                    cc.Close()
    
                End Try
            End If
        End Sub
    can any body tell me what is problem here??
    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: displaying image

    This:
    Code:
    TextBox1.Text
    is probably not the same as the ListBox1.SelectedIndex that you are searching for.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured