1

I am trying to set an image dynamically. Image control is not displaying the image but it is displaying the alternate text. I'm using VS 2008 with vb.net. I used information from this post to construct the code. During debug the file path is correct.

vb Code:

            Dim strImgURL As String = "C:\fldr1\fldr2\Projfldr3\images\emps\" 'local dev path where visual studio solution is located
            Dim filename As String = System.IO.Path.Combine(strImgURL, Session("EmpID").ToString() & ".jpg")            

            If (File.Exists(filename)) Then
                imgEmp.ImageUrl = filename
            Else
                filename = System.IO.Path.Combine(strImgURL, "99999.jpg")
                imgEmp.ImageUrl = filename 
            End If         

aspx:

<asp:image id="imgEmp" Runat="server" Height="100px" Width="77px" 
                        AlternateText="Employee's picture" />

Is it wrong to use "IO.Path.combine" when passing to ImageUrl?

UPDATE: Still working on this with lessons learned from this thread and this thread using MapPath property to set the path to images. The mappath should theoretically detect file path to the images folder in whatever environment I'm testing.

The only way I can get an image to show is if I hard-code filename value like below (removing any mappath code and system.io.path.combine() code entirely). btw I am taking a Int session variable, Session("EmpID") converting to string to create the filename property (e.g. 12345) - none of the conversion methods I'm using are effective:

1. Session("EmpID").ToString() (or  Convert.ToString(Session("EmpID")) 
2. strEmpID =Session("EmpID").ToString() + ".jpg"


strImgURL = "http://server/images/" + strEmpID + ".jpg"
Community
  • 1
  • 1
Doreen
  • 714
  • 2
  • 14
  • 36
  • Please check your resulting `filename` before it is checked if it exists... that will help a lot. – Ian Mar 02 '16 at 02:41

1 Answers1

0

Based on the link you provided, System.IO.Path.Combine automatically adds forward slashes to the strings. Then try to remove the excess forward slash at the end of strImgURL:

  Dim strImgURL As String = "C:\fldr1\fldr2\Projfldr3\images\emps"

Hope this will help. :)

Mark
  • 38
  • 6
  • No, it is not the case... the result with or without forward space will be the same. That's what `Path.Combine` is for after all, to help us resolve ambiguity. – Ian Mar 02 '16 at 02:37
  • @Mark, I removed the slash following emps as you suggested and now I get a square X block where the image should appear. Before there was nothing except the alt text. It's acting like it knows it's there but it's still not displaying. – Doreen Mar 02 '16 at 15:49
  • @Doreen Try to debug it if the imgEmp.ImageUrl path is correct? Or the image in the path exist? – Mark Mar 02 '16 at 23:41
  • @Mark during debug I did find an error in the path and got it to work from local only (but not on dev or prd machines). I've since been experimenting with MapPath (see my UPDATE) with no love. – Doreen Apr 14 '16 at 18:13