Saturday, March 24, 2012

Ole object

I want to display pictures from access database in repeater but instead i receive some
windows with red crosses.
It should be a good solution but it dont work.
This is my .aspx code (well its not main i copied it from another site for my learnigs):

<%@dotnet.itags.org. Import Namespace="System.Data.OleDb" %><script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath("/photolib.mdb"))

dbconn.Open()
sql="SELECT smallimage FROM photo"
--smallimage is an OLE object column connected with .jpg files in my database
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
photo.DataSource=dbread
photo.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script
<form runat="server">
<asp:Repeater id="photo" runat="server"><HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Small Image</th>
</tr>
</HeaderTemplate><ItemTemplate>
<tr>
<td><img src="http://pics.10026.com/?src=<%#Container.DataItem("smallimage")%>"></td
</tr>
</ItemTemplate><FooterTemplate>
</table>
</FooterTemplate></asp:Repeater>
</form
Thanks for any tips or ideas you can give me.The img src attribute should be specified with a location of the file, not the file data itself. One possible way is to have the pictures stored as files on the webserver, and have the field smallimage of the database table points to the corresponding picture file as a relative url. Then when loading the page, the web browser would be able to request the picture file according to the url specified to the src attribute of img tag.
Hi,

In the src of your image, you're supposed to put the URL where the browser will find the picture. Here, you're giving it the image itself. The browser fails to see a valid url there, and displays a red cross.
What you want to do is build a HttpHandler that will get some photo id as a url parameter. in the img src, put the address of the handler (something like blah.ashx?photoid=25).
The handler will send the contents of the image field of your database directly to the response binary stream and set the content type.
Checkthis for a tutorial (even though they should use a HttpHandler instead of an aspx page).

Let me know if this helps.

(BTW, there's a strange header thing in OLE fields in Access which may force you to skip the first 16 or something bytes of the field)
Thanks.
But i already have my .jpg files stored on my webserver, besides i have this files connected to smallimage column in database (access 2000 allows to make .jpg files elemet of database file and that is exactly what i have done).Shouldn't .aspx file read picture data directly from database in this case.
Thanks.
Yes, putting direct source to all .jpg files would propably solve the thing.But i have made .jpg files elemet of database file.Shouldn't .aspx file read picture data directly from database in this case.The other solution (httphandler) lies so far beyond my knowledge(I'm rather a begginer in asp.net).
Sorry. First reply is to Frank Tse and second to bleroy.
In your example the databind expression
<%#Container.DataItem("smallimage")%
does read the picture data from database and included in the response. However, the usage of the src attribute in img tag is expecting a URL to be specified, not the picture data itself (accroding to html spec).

So if you already have the pictures files on the server, all you need to bind the value to the src attribute is the location of the picture files. Otherwise, if the picture data must be retrieved from the database directly, you would need bleroy's HttpHandler approach.
My Access 2000 was the problem it didn't connect database ole object with .jpg file.I don't know why.I have convert my database to sql database.I have set my picture field as varchar.I set source of my pictures like that:
<img src="http://pics.10026.com/?src=image source"/<%# DataBinder.Eval
(Container.DataItem, "Column name") %>'

And now it works perfectly.
Anyway thanks for help.

0 comments:

Post a Comment