Link to home
Start Free TrialLog in
Avatar of md0333
md0333Flag for United States of America

asked on

How do I get an image from DB into an asp.net control?

Below is my code that works the way it is written... it pulls the blob image out of the database and displays it on a webpage. But what if I want it to display in a control on that webpage? Right now I have 2 aspx pages.

The first one just gives the second on the ID.
<a href="WebForm1.aspx?ID=70">Go</a>


The second one displays the image. But I need to put other information on this page so I need the image to go into a placeholder. I just put an <img> tag on this page. How do I get the output from the below code to go into that img tag?

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<% @Import Namespace="System.IO" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        WebForm1

    </div>
    <div>
        <script runat="server">
           
            Private Overloads Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

                Dim sSQL As String = "SELECT ThumbNailPhoto FROM Production.ProductPhoto WHERE ProductPhotoID='" & Request.QueryString("ID") & "'"
                Dim myReader2 As SqlDataReader
                Dim myConnection2 As SqlConnection = New SqlConnection
                myConnection2.ConnectionString = ConfigurationManager.ConnectionStrings("FT_Data").ConnectionString
                Dim myCommand2 As SqlCommand = New SqlCommand()
               
                With myCommand2
                    .CommandText = sSQL
                    .CommandType = CommandType.Text
                    .Connection = myConnection2
                    .Connection.Open()
                    myReader2 = .ExecuteReader(CommandBehavior.CloseConnection)
                End With

                While myReader2.Read
                    Response.BinaryWrite(myReader2("ThumbNailPhoto"))
                End While

                myCommand2.Dispose()
                myConnection2.Dispose()

            End Sub

        </script>

    <div style="background-color: #000000; border: medium solid #C0C0C0">
        <img alt="" id="myimage" name="myimage" src="" runat="server"/>
    </div>
       

    </div>
    </form>
</body>
</html>
Avatar of SAMIR BHOGAYTA
SAMIR BHOGAYTA
Flag of India image

Hello,

You have to put one literal control for getting image, and into coding side, you have to write like this.

Get the data into DataReader

While Reader.Read

literal1.Text = "<img src='../Images/' " & Reader.Item("ImageFiledName") & " height='' width='' />"

End While
ASKER CERTIFIED SOLUTION
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial