Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-13
3
Medium Priority
?
292 Views
Last Modified: 2012-05-12
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>
0
Comment
Question by:md0333
3 Comments
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 36533771
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
0
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 2000 total points
ID: 36535666
Place asp:image control in your aspx page and call a HTTPHandler by passing id of the image as a paramerter. Implement database accessing business logic in  the HTTP handler.

http://stackoverflow.com/questions/46788/how-to-bind-a-memorystream-to-aspimage-control gives clear idea. Please go through it.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question