Active Server Pages Programming (ASP)/Retrieves images from SQL in ASP.NET

Advertisement


Question



Hi,

I have been trying to retrieves images from a SQL SERVER database and show them in a ASP.NET website…


I succeed using Response.BinaryWrite but it is displayed throught the whole page and everything else just disappear… and that’s not what I need….

------------------------------------------------
   Public Sub carga()
       Dim strImageID As String = Request.QueryString("id")
       Dim myConnection As New SqlConnection("Data Source=INTEGRA-TS;Initial Catalog=Integra;User ID=sa;Password=Gessa2009")
       Dim sSQL As String = "SELECT Fotografia FROM Integra.Personas WHERE Cedula ='105250392'"
       Dim myCommand As New SqlCommand(sSQL, myConnection)
       Try
           myConnection.Open()
           Dim myDataReader As SqlDataReader
           myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
           Do While (myDataReader.Read())
               Response.ContentType = "image/JPEG"
               Response.BinaryWrite(myDataReader.Item("Fotografia"))
           Loop
           myConnection.Close()
       Catch SQLexc As SqlException
       End Try
   End Sub
---------------------------------------------------


This is a webpage for human resources department and they want to see the image and also the rest of the details about the persons..

Please help

I did a search in some ASP.NET forums they just shows ”photo albums” or “Slide Shows” and that’s not what I need..

I need to fecth images and place them into a asp control like IMAGE or IMAGE MAP

GOD BLESS YOU !!!!!!  

Answer
Hi,

Sorry in the delay in getting back to you, been very busy at work. I came across this problem before. Will try and explain. Here we go. You can't display the image and data in the same page at the same time like you think you can.


The code you have above works fine. So in another page, do you look up and display your data how you want. Lets call this employee_details.aspx

Right, now for your image section, create a file say image.aspx put all your image code in here.


So in your employee_details.aspx page, put this where you want your image:
<img src="image.aspx" height="100" width="100" /> this will then load the image.

Now if you want to do this dynamically all you would have to do is use a query string like so:-
<img src="image.aspx?employeeid=1234" height="100" width="100" />

So in you image.aspx file you would use Request.QueryString("employeeid") in your select statement to get the corresponding image.


Hope that makes sense, if you need any further help. Let me know

Active Server Pages Programming (ASP)

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ashley Brazier

Expertise

Main focus is ASP, SQL Server 2000 & 2005

Experience

Over 6 years experience.

Education/Credentials
BCs Honours Software Design and Networks

©2012 About.com, a part of The New York Times Company. All rights reserved.