473,795 Members | 3,151 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(C onstants.SQLCon nectionString)

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

Dim bytes() As Byte

Try

myConnection.Op en()

Dim dr As SqlDataReader

dr = myCommand.Execu teReader(Comman dBehavior.Close Connection)

Do While (dr.Read())

'Response.Binar yWrite(bytes()) myDataReader.It em("Photo"))

bytes = dr("photo")

Loop

myConnection.Cl ose()

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 2404
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.Binary Write back in. You also need to set the
Response.Conten tType.

*************** *************** *************** *************** *****
<%@ Page Language="VB" %>
<%@ import Namespace="Syst em.Data" %>
<%@ import Namespace="Syst em.Data.OleDb" %>
<script runat="server">
Public Sub Page_Load(sende r As Object, e As EventArgs)
Dim myConnection As New SqlConnection(C onstants.SQLCon nectionString)
Dim myCommand As New SqlCommand("Sel ect photo from Employees where
EmployeeID =1" , myConnection)
Try
myConnection.Op en()
Dim dr As SqlDataReader
dr =
myCommand.Execu teReader(Comman dBehavior.Close Connection)
Do While (dr.Read())
Response.Conten tType = "image/jpg"
Response.Binary Write(myDataRea der.Item("Photo "))
Loop
myConnection.Cl ose()
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.EventArg s) Handles MyBase.Load
Dim id As Integer = Request.QuerySt ring("ID")
Dim myConnection As New SqlConnection(C onstants.SQLCon nectionString)
Dim myCommand As New SqlCommand("Sel ect photo from Employees where
EmployeeID =" & ID, myConnection)
Try
myConnection.Op en()
Dim dr As SqlDataReader
dr = myCommand.Execu teReader(Comman dBehavior.Close Connection)
Do While (dr.Read())
bytes = dr("photo")
Dim i As Integer
Dim x(bytes.GetUppe rBound(0) - 78) As Byte

For i = 78 To bytes.GetUpperB ound(0)
x(i - 78) = (bytes(i))
Next i
Response.Binary Write(x)
Loop
myConnection.Cl ose()
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******** ******@TK2MSFTN GP10.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.Binary Write back in. You also need to set the
Response.Conten tType.

*************** *************** *************** *************** *****
<%@ Page Language="VB" %>
<%@ import Namespace="Syst em.Data" %>
<%@ import Namespace="Syst em.Data.OleDb" %>
<script runat="server">
Public Sub Page_Load(sende r As Object, e As EventArgs)
Dim myConnection As New SqlConnection(C onstants.SQLCon nectionString)
Dim myCommand As New SqlCommand("Sel ect photo from Employees where
EmployeeID =1" , myConnection)
Try
myConnection.Op en()
Dim dr As SqlDataReader
dr =
myCommand.Execu teReader(Comman dBehavior.Close Connection)
Do While (dr.Read())
Response.Conten tType = "image/jpg"
Response.Binary Write(myDataRea der.Item("Photo "))
Loop
myConnection.Cl ose()
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
12238
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 view found in Microsoft SQL Server Enterprise Manager. So we have Server Groups and Servers, in the tree view. I want to create my own similar tree view and have an image representing
10
2369
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. That is, images inline with the actual content of the blog itself. Is there an example that I can be pointed to, where I can examine some code and figure out how to do this? Frankly I haven't got a clue, aside from breaking the content up into...
8
1277
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. That is, images inline with the actual content of the blog itself. Is there an example that I can be pointed to, where I can examine some code and figure out how to do this? Frankly I haven't got a clue, aside from breaking the content up into...
9
3843
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 site for a small club I belong to and one of the features I would like to include is the ability to allow users to upload image files. unfortunately the servers web root www folder only allows READ and EXECUTE permissions, which makes it...
12
3869
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 like <a href="../../Search/page.aspx">, everything works fine as long as I publish the site to a root web like http://localhost. However, I am developing on my local C drive in c:\development\project. I
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.