tags:

views:

223

answers:

3

Ok, I think I am close to making a break through on this.

I have the following code that should, theoretically, populate an image using the file path through a sql database

public void Image1_Load(object sender, EventArgs e)
{
    ////Code to retrieve logo image from tblMemberLogo - Currently does not work!!!

    var connectionstring = ConfigurationManager.ConnectionStrings["PDCConnectionString"].ConnectionString;
    using (var cn = new SqlConnection("Data Source=STRSQL04;Initial Catalog=PDC;Integrated Security=True"))
    using (var cmd = cn.CreateCommand())
    {
        cn.Open();
        cmd.CommandText = "SELECT LogoFilePath FROM tblMemberlogo WHERE MemberID = '123'";
        //cmd.Parameters.Add("123", "5");
        using (var reader = cmd.ExecuteReader())
        {
            if (reader.Read())
            {
                var filepath = reader.GetString(0);
                Image1.ImageUrl = filepath;
                Label2.Text = filepath;
            }
        }
    }
}

Now, I know I am close as the Label2 brings back the appropriate file path for Member 123. However, Image1 still possesses the dreaded red cross. The source code for this Image control is simply the following.

Image ID="Image1" runat="server" Height="71px"  
            Width="400px" onload="Image1_Load"

I will buy whoever helps me get this over the finish one a nice big pint as I am on the verge of having (another) breakdown!!!

Please find below the output html.

img id="Image1" src="file:c:\online%20reporting\SQL%20Solutions\Member%20Logo\123.GIF" style="height:71px;width:400px;border-width:0px;"

A: 

I am not sure how your data is stored but in most cases when assigning images server side you need to make sure its relative to the website.

Do this using :

Image1.ImageUrl = Server.MapPath(filepath);

RC1140
+1  A: 

Depending on the Url you may need to use ResolveClientUrl or Server.MapPath

Image1.ImageUrl = ResolveClientUrl(filepath);

OR

Image1.ImageUrl = Server.MapPath(filepath);
SteadyEddi
I attempted the latter (Server.MapPath (filepath))and received an "HttpException was unhandled file:....' is not a valid virtual path.The ResolveClientUrl method just gave me the the dreaded red cross unfortunately.Thank you for the suggestions though.
MrDean
+3  A: 

Unfortunately, you can't use a "file:" URL as the src of an image. What you need to do is turn that path into a valid HTTP URL.

So if your images are in "c:\online reporting\SQL Solutions", you should add a virtual directory in IIS under your site root that points there, maybe call it "/logos" or something, then you can go:

String fileName = Path.GetFileName(reader.GetString(0)); Image1.ImageUrl = "/logos/" + fileName;

If your web root is already running in, say, "c:\online reporting", it's even easier -- you can skip the virtual directory and just use "/SQL%20Solutions/" as the beginning of the URL.

_rusty
Grr, not sure why that code example wouldn't go onto two lines.
_rusty
I can never get the code insert to work properly! I understand what you are saying though and will give it a go...thanks rusty.
MrDean
Hello rusty...sorry...I am thick as pigsh*t (well just brand new to this) can you give me a few pointers as to how I can "So if your images are in "c:\online reporting\SQL Solutions", you should add a virtual directory in IIS under your site root that points there"
MrDean
Sure, a virtual directory is just a directory where IIS makes it look like it's part of the web directory, but really it exists elsewhere. So if my web directory was c:\web, and the directory c:\web\images existed, then for the URL /images, IIS would look in c:\web\images. But, say your images were in c:\temp, you could set up IIS so that when someone went to /images, IIS would look in c:\temp rather than c:\web\temp -- that's a virtual directory. More info here: http://msdn.microsoft.com/en-us/library/zwk103ab.aspx
_rusty