Solved

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

Posted on 2011-09-13
3
270 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 12

Expert Comment

by:jagssidurala
ID: 36533806
0
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now