troubleshooting Question

Displaying an SQL Server image

Avatar of Lans-ICT
Lans-ICT asked on
Microsoft SQL Server.NET ProgrammingASP.NET
7 Comments1 Solution527 ViewsLast Modified:
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

SQL Table is called StudentsPhotos and has:

pk - StudentID
image - StudentPhoto

Thanks

Andy
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros