.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

displaying image

Posted By: aamir     Posted Date: January 05, 2009    Points:2   Category :VB.Net
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

Responses
Author: Praveen             
Posted Date: January 06, 2009     Points: 5   

Hi,
modify your code like this and see


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 New IO.MemoryStream(b)

PictureBox1.Image = Image.FromStream(mem)

Catch ee As Exception

MessageBox.Show(ee.Message)

Finally

cc.Close()

End Try
End If
End Sub



Also check that are you getting byte data or not.

Happy Coding!!!



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend