Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to Load image from Access Dbase using ASP

Status
Not open for further replies.

pkuti

IS-IT--Management
Jan 10, 2003
36
KE
Hi,
I'm a very very new to ASP. I have an access dbase and I am trying to use ASP to display the fields in the dbase. One of the fields contains a path to the folder containing the images(got this idea after reading some previous threads).The problem I am having is the images don't show.
I have two ASP files; the first one is named plantdbase.asp and it is:
...
<%
Set cn = Server.CreateObject(&quot;ADODB.Connection&quot;)
Constr = &quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot; & _
&quot;Data Source= &quot; & Server.MapPath(&quot;./Data/plantdbase.mdb&quot;) & &quot;;Persist Security Info=False&quot;
cn.Connectionstring = Constr
cn.Open
strSQL = &quot;SELECT PlantPic FROM Plants WHERE ID =&quot; & requesT(&quot;ID&quot;)
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

rs.open strSQL, cn, adOpenStatic, adLockOptimistic, adCmdText
Response.ContentType =&quot;image/jpg&quot;
Response.BinaryWrite rs(&quot;PlantPic&quot;)
rs.Close
Set rs = Nothing

cn.Close
Set cn = Nothing
%>



The second one which is supposed to diplay the reults is:

...
Const adOpenForwardOnly = 0
Const adOpenKeyset = 1
Const adOpenStatic = 3

Const adLockReadOnly = 1
Const adLockOptimistic = 3

strSQL = &quot;SELECT * FROM Plants ORDER BY PID&quot;
Set cn = Server.CreateObject(&quot;ADODB.Connection&quot;)
Constr = &quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot; & _
&quot;Data Source= &quot; & Server.MapPath(&quot;./Data/plantdbase.mdb&quot;) & &quot;;Persist Security Info=False&quot;
cn.Connectionstring = Constr
cn.Open
%>

<%
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
set rs = cn.Execute(strSQL)

Do While NOT rs.EOF
%>
<ul>
<li> <IMG SRC=&quot;PlantDbase.asp?ID=<%=RS(&quot;PID&quot;)%> width=&quot; border=&quot;0&quot; 200&quot;> <%=RS(&quot;ScientificName&quot;)%></a>
<%=RS(&quot;OtherNames&quot;)%>
<%
RS.Movenext
loop

%>



Please tell me what's wrong with my code. Need help. Thanks.

Pkuti
 
Ok, it seems to me you have mixed the two common methods of image storage. Your code is attempting to pull the binary image data out of a field in your database, but you stated in your explanation that you are only storing the path of the image, therefore you should only need to set the src of the image like this:
Code:
<IMG SRC=&quot;<%=RS(&quot;PlantPic&quot;)%>&quot; border=&quot;0&quot; width=&quot;200&quot;>
This will display the picture if the stored path assumes you are starting from the web directory. If you post an example parth I may be able to help you determine how to set it if your using another method.

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Hi Tarwn,
Thanks very much for your assistance. As stated in my first message I'm now a biginner in this ASP thing. So you are perfectly right when you said I've missed the codes up.
So kindly help me with me with another method.
This is what I want to do...I have a dbase in access, it should contain a plant's scientific name, the uses and a picture of the plant. And I would like to use ASP to display these info., including the pictures.

I replaced the line of code you pasted and it didn't work. Obviosuly I am not doing something right.
Need your assistance badly. Thanks.

Pkuti
 
Could you explain what folder the web page is in and what folder the images are stored in, then show some sample paths from the database for the pictures? I am willing to be we just need to alter the paths in order to make the pictures work.

On change I would make to your html is this:
Code:
'Move the ul tag out of the loop so it doesn't get written with every loop
Response.Write &quot;<ul>&quot;
Do While NOT rs.EOF 
   %> 
   <li> <IMG SRC=&quot;PlantDbase.asp?ID=<%=RS(&quot;PID&quot;)%> width=&quot; border=&quot;0&quot; 200&quot;> 
   <%
   'Switched these to Response.Write, explained below
   Response.Write RS(&quot;ScientificName&quot;)
   Response.Write RS(&quot;OtherNames&quot;)
   RS.Movenext 
loop 
%>
I made three changes to your code,
1) The
Code:
</a>
tag was unnecessary because you didn't have a start tag for it
2) The
Code:
<ul>
tag was moved out of the loop so it would only be written one time instead of once for ecery record
3) The printouts were switched to Response.Write's so that they could be in the same block of code. Otherwise the server has to switch back and forth between html processing and VBScript procesing, which can cause a slowdown. It probably wouldn't be noticeable here, but it is good to get into these habits early.

If you supply the information from the beginning of the post I think we can get the pictures to show up, the rest of your code looks fine,
-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Hi Tarwn,
The webpage is in -C:\Inetpub\and the image folder is in - C:\Inetpub\
This is an image path in my dbase:
i.e. agronomy_main.jpg is the picture.

I hope these information are enough to hlp you sort me out. Thanks also for the corrections that you made for me so far.

Hope to read your response ASAP.

Pkuti
 
Ok, if you want to store the image path with an http in front of it, you will need to change the links to:

When a request comes in for something on your webser, it automatically starts at the directory as the default directory, so you only need to specify your domain address or ip number and then the path starting at The problem with this is if the address ever changes you have to go into your database and change everything to use the new address.
A better way to store the images would be to simply store just the image name, ie whatever.jpg. Then what we can do is alter the img tag slightly:
Code:
<IMG SRC=&quot;images/<%=RS(&quot;PlantPic&quot;)%>&quot; border=&quot;0&quot; width=&quot;200&quot;>

I apologize in my previous posting that I forgot to correct the img tag back to what I had in the first post. Now what we are doing is saying that every image is located at images/image_name from the current directory. This means if you ever move your site you won't have to change the code or database as long as you keep the images folder in the same directory as your ASP script.

Hope this helps,
-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Thanks so much Tawrn for your assistance. I've taken note of all the tips and will let you know the outcome of the changes. Sorry for my delay responses, seems we have problem with time difference:)

Thanks.

-Pkuti
 
No problem, glad to be of assistance :)

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Hi Tarwn,
It worked!!:) Thanks so much for the help. I have the pics showing as I wanted.
I am very grateful. God bless.

-Pkuti
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top