md0333
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.Sql Client" %>
<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.ConnectionSt ring = ConfigurationManager.Conne ctionStrin gs("FT_Dat a").Connec tionString
Dim myCommand2 As SqlCommand = New SqlCommand()
With myCommand2
.CommandText = sSQL
.CommandType = CommandType.Text
.Connection = myConnection2
.Connection.Open()
myReader2 = .ExecuteReader(CommandBeha vior.Close Connection )
End With
While myReader2.Read
Response.BinaryWrite(myRea der2("Thum bNailPhoto "))
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>
The first one just gives the second on the ID.
<a href="WebForm1.aspx?ID=70"
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.Sql
<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.ConnectionSt
Dim myCommand2 As SqlCommand = New SqlCommand()
With myCommand2
.CommandText = sSQL
.CommandType = CommandType.Text
.Connection = myConnection2
.Connection.Open()
myReader2 = .ExecuteReader(CommandBeha
End With
While myReader2.Read
Response.BinaryWrite(myRea
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("ImageFiledNam
End While