Solved

Retrieving image filename from database and displaying image on page

Posted on 2008-10-01
22
587 Views
Last Modified: 2010-08-05
I am developing an ASP.NET web page with VB.NET.  I have the filenames for images stored in a column of my SQL Server database.  I am querying the data for a specific product SKU.  The result of the query will bring a file name, such as 08326A.jpg.  I need the proper syntax for the VB.NET to retrieve that filename and then display that image from its location in a folder on the web server.  I've tried looking online and all I came up with was something like:
<asp:Image ID="imgMed" BorderWidth="0" runat="server" ImageUrl="/images/medium/" + <%# DataBinder.Eval(Container.DataItem, "med_img") %> />
but that only causes errors.
0
Comment
Question by:OVC-it-guy
  • 12
  • 7
  • 3
22 Comments
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 22619975
I'm not sure why but intellisense is very misleading whenever I try to do something like this. However, the following seems to work. Note that I intentionally did not surround the braces with quotes.
<asp:Image ID="imgMed" BorderWidth="0" runat="server" ImageUrl=<%# "/images/medium/" + DataBinder.Eval(Container.DataItem, "med_img") %> />

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22623993
What is the error you get?
Following may help
<asp:Image ID="imgMed" BorderWidth="0" runat="server" ImageUrl=Server.MapPath("/images/medium/" + <%# DataBinder.Eval(Container.DataItem, "med_img") %>) />
0
 

Author Comment

by:OVC-it-guy
ID: 22624152
Ok, on the first (from CyrexCore2k), I get the error: BC30456: 'DataItem' is not a member of 'System.Web.UI.Page'

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22624189
Try this
<asp:Image ID="imgMed" BorderWidth="0" runat="server" ImageUrl=Server.MapPath("/images/medium/" + <%#Eval("med_img") %>) />
0
 

Author Comment

by:OVC-it-guy
ID: 22624264
And on the second (from CodeCruiser), I get the error: The server tag is not well formed.
Let's try this another way and see what you two (or anyone else) thinks.
Below is the Sub for getting the data.  Let's try it with my variable "graphic1"
Dim strConn2 As String = (ConfigurationManager.ConnectionStrings("omahavaccineConnectionString").ToString)

            Dim GetProductSKU As String = "SELECT product.sku AS sku, product_main.product_name AS product, " & _

                "product.product_name as short_descr, product.description AS description, product.price AS price, " & _

                "product.sale_price AS sale_price, product.special_start_date AS start_date, product.special_end_date AS end_date, " & _

                "product.graphic1 AS graphic1, . . . "

            Dim MyConn As New SqlConnection(strConn2)

            Dim CmdSDR As New SqlCommand(GetProductSKU, MyConn)

            Dim myDR As SqlDataReader

            MyConn.Open()

            myDR = CmdSDR.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

            Do While myDR.Read

                'Me.lblSKU.Text = myDR.Item("SKU".ToString)

                If Not IsDBNull(myDR.Item("sku")) Then

                    lblSKU.Text = myDR.GetString(myDR.GetOrdinal("sku"))

                End If

                If Not IsDBNull(myDR.Item("product")) Then

                    lblProduct.Text = myDR.GetString(myDR.GetOrdinal("product"))

                End If

                If Not IsDBNull(myDR.Item("short_descr")) Then

                    lblShort_descr.Text = myDR.GetString(myDR.GetOrdinal("short_descr"))

                End If

                If Not IsDBNull(myDR.Item("description")) Then

                    lblDescription.Text = myDR.GetString(myDR.GetOrdinal("description"))

                End If

                If Not IsDBNull(myDR.Item("price")) Then

                    lblPrice.Text = myDR.GetString(myDR.GetOrdinal("price"))

                End If

                If Not IsDBNull(myDR.Item("graphic1")) Then

                    Dim graphic1 As String = myDR.GetString(myDR.GetOrdinal("graphic1"))

                End If

Then close the reader and End Sub
 

And here's where I'm trying to display the image:

<td width="200" align="center" valign="middle" bgcolor="#dddddd">

                <asp:Image ID="imgMed" BorderWidth="0" runat="server" ImageUrl=Server.MapPath("/images/" + <%# DataBinder.Eval(Container.DataItem, "graphic1") %>) /></td>

Open in new window

0
 

Author Comment

by:OVC-it-guy
ID: 22624303
<asp:Image ID="imgMed" BorderWidth="0" runat="server" ImageUrl=Server.MapPath("/images/" + <%#Eval("graphic1") %>) /> results in the error: The server tag is not well formed.
0
 

Author Comment

by:OVC-it-guy
ID: 22624318
And I'm going to assume the Server.MapPath is based on the root directory for the web site.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22624398
If you are using databinding in the code behind then why dont you set the image url from the code behind?
like
imgMed.ImageUrl=Server.MapPath("/images/" & myDR.GetString(myDR.GetOrdinal("graphic1"))
               
0
 

Author Comment

by:OVC-it-guy
ID: 22624444
You're right. Let me try that.
0
 

Author Comment

by:OVC-it-guy
ID: 22624528
Not databinding.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22624590
What you mean by not databinding. You are not databinding here but are retrieving records from database. Can you debug through the code to see what's coming back from myDR.GetString(myDR.GetOrdinal("graphic1") and full image url is being formed and whether its valid path or not?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:OVC-it-guy
ID: 22624676
Sorry.  I'm past that.  I put in your code and ran it.  No errors on the page itself.  So I ran through the site, passing variables and what I got was the error: System.InvalidOperationException: Failed to map the path '/images/05348A.4c.GIF'. (Honest, that's what I inherited for image filenames.) It refers to the line of code: imgMed.ImageURL = Server.MapPath("/images/" & myDR.GetString(myDR.GetOrdinal("graphic1")))
This should be valid, because if the URL for the site was www.PetSuppliesDelivered.com, then that image is at www.PetSuppliesDelivered.com/images/05348A.4c.GIF .
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22624811
Is the Images folder a subfolder in the root directory of the aspx trying to display it? Open the source of the page in notepad and see what is the path of the image url being generated by the server.mappath if at all. Otherwise try without using server.mappath. Also try with a test image of one extension.
0
 

Author Comment

by:OVC-it-guy
ID: 22624839
Let me try each and get back to you.
0
 

Author Comment

by:OVC-it-guy
ID: 22625078
Yes, the images folder is a subfolder of the root directory.  In the error message provided by running it in MS Visual Web Developer 2005 Express Edition gives "Failed to map the path '/images/08525.4c.gif' ." When I tried it again without using server.mappath, the image still isn't displayed.  No errors though.  Source code shows it as <img id="imgMed" src="/images/08525.4c.gif" style="border-width:0px;" /> which is correct.  When I replace the <asp:Image ...> with a standard hardcoded <img src="/images/08525.4c.gif"> the image is displayed.  Tried it with your last suggestion, I copied/pasted that image onto the root directory and, of course' removed the "/images/" & from the code and it worked.
0
 

Author Comment

by:OVC-it-guy
ID: 22625185
This could be problematic, for we have over 3800 products (and images for each).  And I have the images sized in three scales for different page functions, each size in their own subfolder.  So I sure hope there is a way to get the images to display from subfolders.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 22625272
There should not be a problem for displaying images from subfolders. Did you try removing one of the . dots from the filename? I suspect this as a problem because the file becomes multiextensioned. Also try server.mappath with a single . in file name. Also remove the first / from the name. like
"Images/08525.4c.gif"
0
 

Author Comment

by:OVC-it-guy
ID: 22625506
I can't remember if I was using Dreamweaver or Visual Web Developer at the time, but one/both of them threw in the leading slant bar when I broused to the location of the images.  I'll gladly try that, as well as your other suggestions.
0
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 22625612
OVC-it-guy I was assuming that image was inside a repeater. Since the container object happens to be System.UI.Page I'm guessing that assumption was incorrect. In light of that change your ASPX code to this:

<asp:Image ID="imgMed" BorderWidth="0" runat="server" ImageUrl="" />

On your code behind, once you have the file name in the SqlDataReader object (you said you were using a sql query to get it) add the line:

imgMed.ImageUrl = "/images/medium/" + dr["image_file"];

And your image should display correctly.
0
 

Author Comment

by:OVC-it-guy
ID: 22625936
Removed the extra . in some filenames - same result, error: Failed to map the path '/images/08525.gif'.
Removed the server.mappath without the extra . in filename - no error, no image displayed.
Removed leading / with server.mapppath - no error, no image displayed (source code shows correct path though - <img id="imgMed" src="C:\Inetpub\wwwroot\omahavaccine_redesigned\images\08525.gif" ...> ).
Removed leading / and removed server.mappath - and that worked!  To be sure, I deleted the images I put on the root directory.  So that's the solution.
Code that worked:
imgMed.ImageURL = "images/" & myDR.GetString(myDR.GetOrdinal("graphic1"))
And on the web page:
<asp:Image ID="imgMed" BorderWidth="0" runat="server" />
FYI Visual Web Developer doesn't like the imgMed.ImageURL = portion of the code.  It says 'imgMed' is not declared, but it clearly is on the page.  But it doesn't throw an exception/error, so . . .
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22627178
why grade B then?
0
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 22627200
And why didn't I get any points? :P
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...)) The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now