Microsoft KB Archive/299320

= How To Display an Image on a Web Page When the Path Is Stored in a Database =

Article ID: 299320

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.1
 * Microsoft ActiveX Data Objects 2.1 Service Pack 1
 * Microsoft ActiveX Data Objects 2.1 Service Pack 2
 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6

-



This article was previously published under Q299320



SUMMARY
It is not always practical to store images that are needed in a Web site in a database; it may be more practical to store a path to the image. Even though the path is stored, you can still use that image in an Active Server Pages (ASP) page. This article illustrates how to show images when just the path is stored in the database.



Step-by-Step Example
 In Microsoft SQL Server, open the PUBS sample database, and create a new table named ImgTable. Create the following fields in ImgTable:

Name    DataType     Size imgID    Char         10 imgPath  Char         30

 Add a record to the database with the following values: an imgID with a value of 1, and an imgPath with a value that is the full path of a .gif or .bmp file on your Microsoft Internet Information Server (IIS) computer. You will most likely find a .bmp file in the Windows or WINNT folder. The value in the imgPath column should look similar to the following:

C:\Windows\circles.bmp

  In Notepad, create a new ASP page named ImagePath.asp, and paste the following code:

Note You must change User ID= and password= to the correct values before you run this code. Make sure that  has the appropriate permissions to perform this operation on the database. <%@ language=&quot;vbscript&quot; %>  <BODY> <% dim cn dim rs

set cn=server.createobject(&quot;ADODB.Connection&quot;) set rs=server.createobject(&quot;ADODB.Recordset&quot;)

cn.open &quot;Provider=SQLOLEDB;data source=servername;&quot; & _ &quot;initial catalog=pubs;user id=<UID>;password= &quot; set rs.activeconnection = cn

rs.source = &quot;select imgPath from ImgTable where imgID=1&quot; rs.open %> <img src=<%= trim(rs.fields(0))%>> <% rs.close set rs=nothing cn.close set cn = nothing %> </BODY> </HTML> </li> Modify the connection string so that it points to the correct server and uses the correct user name and password.</li> Save ImagePath.asp in your InetPub\Wwwroot folder on the IIS server.</li> In your Web browser, open the page. Notice that the image appears on the page.</li></ol>

Keywords: kbhowto kbgraphic KB299320

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.