aamir55
January 5th, 2009, 11:35 PM
i have written small program to store image in database.
i m using a button for save and a listbox
code for saving:
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.
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
i m using a button for save and a listbox
code for saving:
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.
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