0

I have entered an image in sql server database successfully. Now i want to retrieve this image from database and show it in an Image control. I don't want to put any ListBoxes on the page. I wrote in ASP dot Net..

Dim MS As MemoryStream
Dim IMG As System.Drawing.Image
Dim FS As FileStream
Dim FI As FileInfo
Dim IMGSTRM As Stream
Dim IMGBYTS As Byte()
Dim ImgData() As Byte

    CMD1 = New SqlCommand("select * from IMG where userid=0", CON1)
    If Not IsNothing(RDR1) Then RDR1.Close()
    RDR1 = CMD1.ExecuteReader()
    If RDR1.Read Then
        IMGBYTS = RDR1!img
        MS = New MemoryStream(IMGBYTS, False)
        IMG = Image.FromStream(MS)
        PIC1.ImageUrl = IMG
    End If

The problem is in the bold 2 lines above End If

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Dev
  • 260
  • 4
  • 18
  • 34
  • Possible duplicate: [convert from Binarydata to image control in asp.net](http://stackoverflow.com/q/7390983/55209), [Display image from database in ASP.net with C#](http://stackoverflow.com/q/6987433/55209), [How to show a image in database in the image control of Asp.net?](http://stackoverflow.com/q/2482104/55209) – Artem Koshelev Apr 05 '12 at 06:45
  • You really like your shift/capslock key, don't you? – ThiefMaster Apr 05 '12 at 08:05
  • Yep, I know it's a bad practice but i always declare my variables LOUD AND CLEAR :) – Dev Apr 05 '12 at 14:09

1 Answers1

1

if you are using the desktop application this is the solution

Private Sub SqlBlob2File(ByVal DestFilePath As String)

    Dim PictureCol As Integer = 0 ' the column # of the BLOB field
    Dim cn As New SqlConnection("server=localhost;integrated security=yes;database=NorthWind")
    Dim cmd As New SqlCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn)

    cn.Open()
    Dim dr As SqlDataReader = cmd.ExecuteReader()
    dr.Read()
    Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
    dr.GetBytes(PictureCol, 0, b, 0, b.Length)
    dr.Close()

    cn.Close()

    Dim ms As New System.IO.MemoryStream(b)
    Me.PictureBox1.Image = System.Drawing.Image.FromStream(ms)
End Sub

this is the link where you may want to look at : http://support.microsoft.com/kb/321900/en-us

asp.net solution

in case of web page or aspx page you can only show an image on a webpage from an Url, there is first should be a picture webpage as webform2 with only a picture box should have to be there. This page is the image for the webform1.

In this way you can put as much pictures on a page as you want, depending of course that you make the same amount of pseudo pages (and don't give them the same name).

I hope it will be a nice pages.

\\ There has to be an imagebox, a button and a label on the webform1

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim conn As New SqlConnection(connStr)
Dim cmd As New SqlCommand("SELECT FileName, PictureID FROM Picture", conn)
da = New SqlDataAdapter(cmd)
cbd = New SqlCommandBuilder(da)
dsPictures = New DataSet
da.Fill(dsPictures)
Me.Image1.Visible = False
ListBox1.AutoPostBack = True
Try
ListBox1.DataSource = dsPictures.Tables(0)
ListBox1.DataTextField = "FileName"
ListBox1.DataValueField = "PictureID"
ListBox1.DataBind()
Catch sqlExc As SqlException
Me.Label1.Text = "Database Error" 'sqlExc.ToString
Catch exc As Exception
Me.Label1.Text = "Datbase Connection Failed!"
End Try
conn.Close()
End If
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Session.Item("img") = ListBox1.SelectedItem.Value
Image1.Visible = True
Image1.ImageUrl = "http://localhost/testSQLPlaatjesWeb/WebForm2.aspx"
End Sub

/// \\

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim conn As New SqlConnection(connStr)
Dim sqlstr As String = String.Format("SELECT Picture FROM Picture WHERE
(PictureID = {0})", CInt(Session.Item("img")))
Dim cmd As New SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Response.BinaryWrite(CType(rdr.Item("Picture"), Byte()))
rdr.Close()
conn.Close()
End Sub

///

steave finner
  • 489
  • 1
  • 6
  • 18
  • My dear "Me.PictureBox1.Image" is not present in Asp Dotnet. There is ImageUrl Present in it. How should i assign Image1.ImageUrl to System.Drawing.Image.FromStream(ms) ?? – Dev Apr 05 '12 at 07:06
  • I have updated my answer please check and let me know if this did worked for you.. – steave finner Apr 05 '12 at 09:33