Avatar of Lans-ICT
Lans-ICT
 asked on

Displaying an SQL Server image

Hi,

Im trying to retrieve an image from an SQL Server image field and display it in an image box in VB.Net

Code below, help appreciated!

Andy

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim images As Image
        Dim conn As New SqlConnection
        conn.ConnectionString = "Data Source=ANDY-PC\KILLIKA;Initial Catalog=LDCTT;Integrated Security=True"
        Dim cmd As New System.Data.SqlClient.SqlCommand
        cmd.Connection = conn
        cmd.CommandText = "SELECT StudentPhoto FROM studentsphotos WHERE StudentID='" & Request.QueryString("sid")
        cmd.CommandType = CommandType.Text
        Try
            conn.Open()
            Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader()
            dr.Read()
            images = (DirectCast(dr("image"), Byte()))
        Catch ex As Exception
            '    BtnClearError.Visible = True
            conn.Close()
        End Try
        conn.Dispose()

    End Sub

Open in new window


SQL Table is called StudentsPhotos and has:

pk - StudentID
image - StudentPhoto

Thanks

Andy
.NET ProgrammingASP.NETMicrosoft SQL Server

Avatar of undefined
Last Comment
Roopesh Reddy

8/22/2022 - Mon
Roopesh Reddy

Lans-ICT

ASKER
Tried these three, the all try to make use of:

image.FromStream(ms)

which gives a message:

FromStream not a member of image.
pkbugudai

Dear!!!

you have to do like this.

retrieve the stream
recreate the image from stream
display in Picture Box.


follow this link
http://www.codeguru.com/csharp/.net/net_asp/files/article.php/c14061/Rendering-Images-in-ASPNET-Directly-from-Your-Database.htm
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Lans-ICT

ASKER
I did manage to get the first link on roope's suggestions to "work" but all it does is display:

/ !ÿÿÿÿBitmap ImagePaint.PicturePBrush ŽBMŽ6(ÈèÁ¹Á¹Á¹½À·½À·½À·½À·¿Â¹¿Â¹¿Â¹¿Â¹½¿¹½¿¹½¿¹½¿¹¾¿¶»À·­µ«ž•7+%0&&+',)(168B9?L@FY@Gb?LlBQqGVvIXxH^Ka„Mc‡Mc‡BW„:Ps8I\7CGC?:fa^H>>B88D86C75C75E97D86E97G;9I=;F=:F=:H?LC@NEBJC@LEBLEBHA>OCANB@NB@PDBQECL@>E97B64E97E97H<:MA?K?=I=;F:8C75D86E97MA?K?=NC?OD@PEAJ?;G85G85K<9QB?N?;O@=N?:LA=K@L@>I=;E97E97E71H:4K=7L>8J<6L>8M?9L>8J>8J>8I=7I=7M?9N@:OA;OA;MA=L@<6A?4?=1<:2=;3><4?=5@>3><3><3><3><5@>5@>6A?7B@4A?4A?4A?4A?5@>5@>5@>5@>6A?5@>4?=4?=3><Á¹Á¹Á¹¾Á¸¾Á¸¾Á¸¾Á¸¿Â¹¿Â¹¿Â¹¿Â¹½¾º½¾º½¾º½¾ºÀ¾¶º¿¶­µ«›œ“7,(0'$,'&*%&# "$$*+-826I7AY=G_EOgISkGWtJZwJZwDTq=Mq8Fc1JA>KB?KB?I@=JA>KB?MDAJEBJEBJEBHC@MA?L@>L@>OCAMA?I=;F:8E97E97E97G;9J>J>K?=MA?H<:F75F75I:8M>K<:E64A20D53I:8K?=NB@L@>MA?NB@I=;E97C75C5/J<6PB8I;5J<6L>8M?9N@:N@:OA;PBM><4?=5@>2=;2=;2=;2=;5@>5@>6A?

Open in new window

ASKER CERTIFIED SOLUTION
Roopesh Reddy

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Lans-ICT

ASKER
Here's my code:

details.aspx:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        picimage.ImageUrl = "image.aspx?sid=" & Request.QueryString("sid")
    End Sub

Open in new window


Outputs:
Image 1

image.aspx:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Try
        If Request.QueryString("sid") IsNot Nothing Then
            Dim strQuery As String = "select studentphoto from studentsphotos where studentid=@id"
            Dim cmd As SqlCommand = New SqlCommand(strQuery)
            cmd.Parameters.Add("@id", SqlDbType.Int).Value() = Convert.ToInt32(Request.QueryString("sid"))
            Dim dt As DataTable = GetData(cmd)
            If dt IsNot Nothing Then
                Dim bytes() As Byte = CType(dt.Rows(0)("StudentPhoto"), Byte())
                Response.Buffer = True
                Response.Charset = ""
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.BinaryWrite(bytes)
                Response.Flush()
                Response.End()
            End If
        End If
        ' Catch ex As Exception
        '     MsgBox(ex.Message)
        ' End Try
    End Sub

    Public Function GetData(ByVal cmd As SqlCommand) As DataTable
        Dim dt As New DataTable
        Dim strConnString As String = "Data Source=ANDY-PC\KILLIKA;Initial Catalog=LDCTT;Integrated Security=True"
        Dim con As New SqlConnection(strConnString)
        Dim sda As New SqlDataAdapter
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            sda.SelectCommand = cmd
            sda.Fill(dt)
            Return dt
        Catch ex As Exception
            Response.Write(ex.Message)
            Return Nothing
        Finally
            con.Close()
            sda.Dispose()
            con.Dispose()
        End Try
    End Function

Open in new window


Outputs:
Image 2
Roopesh Reddy

Hi,

Check the below article which exactly explains your requirement!

http://www.codeproject.com/Articles/271590/Show-an-image-saved-in-a-SQL-Table-on-an-ASP-Net-I

The code in it may be in C#, which you can easily convert it to VB - http://converter.telerik.com

Hope it helps u...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.