473,326 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Images from SQL Server



Hi

I am trying to display in an aspx page an image from the employees table in
the sql server 2000 Northhwind database

"Select photo from employees where employeeid = 1"

This is as far a I got
================================================== ==========================
=================

Dim myConnection As New SqlConnection(Constants.SQLConnectionString)

Dim myCommand As New SqlCommand("Select photo from Employees where
EmployeeID =1" , myConnection)

Dim bytes() As Byte

Try

myConnection.Open()

Dim dr As SqlDataReader

dr = myCommand.ExecuteReader(CommandBehavior.CloseConne ction)

Do While (dr.Read())

'Response.BinaryWrite(bytes()) myDataReader.Item("Photo"))

bytes = dr("photo")

Loop

myConnection.Close()

Response.Write("Person info successfully retrieved!")

Catch SQLexc As SqlException

lblError.Text = SQLexc.Message

end try


================================================== ==========================
=================

I am at a loss with what to do with the byte array next.

All I could think of was write it to a file then reference that as the URL
for an image control

But I am sure there is a better way

Any help regarding this would be great

Cheers

James




Nov 17 '05 #1
2 2388
Jos
James Lang wrote:
Hi

I am trying to display in an aspx page an image from the employees
table in the sql server 2000 Northwind database

"Select photo from employees where employeeid = 1"

This is as far a I got
<cut>
I am at a loss with what to do with the byte array next.

All I could think of was write it to a file then reference that as
the URL for an image control

But I am sure there is a better way

Any help regarding this would be great


You're almost there.

I removed the bytes array from your code, and I brought the
Response.BinaryWrite back in. You also need to set the
Response.ContentType.

************************************************** ***************
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Public Sub Page_Load(sender As Object, e As EventArgs)
Dim myConnection As New SqlConnection(Constants.SQLConnectionString)
Dim myCommand As New SqlCommand("Select photo from Employees where
EmployeeID =1" , myConnection)
Try
myConnection.Open()
Dim dr As SqlDataReader
dr =
myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
Do While (dr.Read())
Response.ContentType = "image/jpg"
Response.BinaryWrite(myDataReader.Item("Photo"))
Loop
myConnection.Close()
Catch SQLexc As SqlException
lblError.Text = SQLexc.Message
end try
End Sub
</script>
************************************************** ****************

Save this code in a separate file, let's say "photo.aspx".
Note that the file doesn't contain any HTML tags, only code.
This file will now act as a jpg or gif image.

Therefore, just reference this file from your main file:
<img src="photo.aspx">

Later you can extend this to something like:
<img src="photo.aspx?id=5">

--

Jos Branders
Nov 17 '05 #2
A Big Thanks to you Jos

I had lots of problems getting this to work but as it turns out there is a
78bit header to the bitmap images in the employees table

Once I stripped that of it worked great although it was trial and error for
about 4 hours. ( Creating my own table, then adding images to it worked
fine so nothing wrong with the code logic but the Northwind images still did
not work so I created an new windows app and wrote the images to a file
"test.bmp" but they would not open. Light at the end of the tunnel time I
thought there must be something wrong with the binary stream. Opening other
bitmap files and viewing there binary contents indicated there was
additional bytes at the start of the stream. I removed them an hay presto I
had an image

Using and ASPX page as the source of the image was a something I never
thought of and now I know the technique I can use it for all sorts of stuff

Here is the code I used if anyone else wants to play with the Northwind DB

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim id As Integer = Request.QueryString("ID")
Dim myConnection As New SqlConnection(Constants.SQLConnectionString)
Dim myCommand As New SqlCommand("Select photo from Employees where
EmployeeID =" & ID, myConnection)
Try
myConnection.Open()
Dim dr As SqlDataReader
dr = myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
Do While (dr.Read())
bytes = dr("photo")
Dim i As Integer
Dim x(bytes.GetUpperBound(0) - 78) As Byte

For i = 78 To bytes.GetUpperBound(0)
x(i - 78) = (bytes(i))
Next i
Response.BinaryWrite(x)
Loop
myConnection.Close()
Catch SQLexc As SqlException
lblError.Text = SQLexc.Message
End Try

End Sub

Once again thanks Jos

Cheers
James

"Jos" <jo***************@fastmail.fm> wrote in message
news:uB**************@TK2MSFTNGP10.phx.gbl...
James Lang wrote:
Hi

I am trying to display in an aspx page an image from the employees
table in the sql server 2000 Northwind database

"Select photo from employees where employeeid = 1"

This is as far a I got

<cut>

I am at a loss with what to do with the byte array next.

All I could think of was write it to a file then reference that as
the URL for an image control

But I am sure there is a better way

Any help regarding this would be great


You're almost there.

I removed the bytes array from your code, and I brought the
Response.BinaryWrite back in. You also need to set the
Response.ContentType.

************************************************** ***************
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Public Sub Page_Load(sender As Object, e As EventArgs)
Dim myConnection As New SqlConnection(Constants.SQLConnectionString)
Dim myCommand As New SqlCommand("Select photo from Employees where
EmployeeID =1" , myConnection)
Try
myConnection.Open()
Dim dr As SqlDataReader
dr =
myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
Do While (dr.Read())
Response.ContentType = "image/jpg"
Response.BinaryWrite(myDataReader.Item("Photo"))
Loop
myConnection.Close()
Catch SQLexc As SqlException
lblError.Text = SQLexc.Message
end try
End Sub
</script>
************************************************** ****************

Save this code in a separate file, let's say "photo.aspx".
Note that the file doesn't contain any HTML tags, only code.
This file will now act as a jpg or gif image.

Therefore, just reference this file from your main file:
<img src="photo.aspx">

Later you can extend this to something like:
<img src="photo.aspx?id=5">

--

Jos Branders

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Allison | last post by:
Hi, Newbie here, please bear with me. I have a C# project which implements a tree view control. I want to add some images to this tree view control. The tree view control represents the tree...
10
by: Neo Geshel | last post by:
I am seeking to hand-roll my own blog in ASP.NET 2.0 and SQLExpress 2005. Why? Because I can. Because I will gain experience. The one thing that has me stumped at square one is inline images....
8
by: Neo Geshel | last post by:
I am seeking to hand-roll my own blog in ASP.NET 2.0 and SQLExpress 2005. Why? Because I can. Because I will gain experience. The one thing that has me stumped at square one is inline images....
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
12
by: John Kotuby | last post by:
Hi all, Maybe this is a simple problem found in ASP.NET 2.0 course 101, but I must have missed it. When I create a page in Visual Web Developer and use URLs like "/images/picture.gif " or a link...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.