Retrieving image filename from database and displaying image on page

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.
OVC-it-guyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CodeCruiserConnect With a Mentor Commented:
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
 
CyrexCore2kCommented:
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
 
CodeCruiserCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

0
 
CodeCruiserCommented:
Try this
<asp:Image ID="imgMed" BorderWidth="0" runat="server" ImageUrl=Server.MapPath("/images/medium/" + <%#Eval("med_img") %>) />
0
 
OVC-it-guyAuthor Commented:
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
 
OVC-it-guyAuthor Commented:
<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
 
OVC-it-guyAuthor Commented:
And I'm going to assume the Server.MapPath is based on the root directory for the web site.
0
 
CodeCruiserCommented:
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
 
OVC-it-guyAuthor Commented:
You're right. Let me try that.
0
 
OVC-it-guyAuthor Commented:
Not databinding.
0
 
CodeCruiserCommented:
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
 
OVC-it-guyAuthor Commented:
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
 
CodeCruiserCommented:
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
 
OVC-it-guyAuthor Commented:
Let me try each and get back to you.
0
 
OVC-it-guyAuthor Commented:
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
 
OVC-it-guyAuthor Commented:
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
 
OVC-it-guyAuthor Commented:
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
 
CyrexCore2kCommented:
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
 
OVC-it-guyAuthor Commented:
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
 
CodeCruiserCommented:
why grade B then?
0
 
CyrexCore2kCommented:
And why didn't I get any points? :P
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.