Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two image control on my page, when i take its image url from images folder then its showing image and when i take image url from other folder(Ads_Images) then it is not showing image, Don't know whats wrong with it...
ASP.NET
<asp:Image ID="Image1" runat="server" ImageUrl="Ads_Images/7f0e7ef3-4cb8-40e0-a835-00314768a70a70-515.jpg"/>

<asp:Image ID="Image2" runat="server" ImageUrl="Images/imagenotavailable.jpg" />

where Ads_Images and Images are two folders in my project, Images is my default folder and in Ads_Images user can store images and save path to database, I am using GUID to add with my images.
C#
protected void btnUpload_Click(object sender, EventArgs e)
    {
        string fileName = string.Empty;
        string filePath = string.Empty;
        string getPath = string.Empty;
        string pathToStore = string.Empty;
        if (FileUpload1.PostedFile != null)
        {
            fileName = FileUpload1.FileName;
            filePath = Server.MapPath("Ads_Images/" + System.Guid.NewGuid() + fileName);
            FileUpload1.SaveAs(filePath);
            SqlCommand cmd = new SqlCommand("insert into userimage (FileName, FilePath)" + " values(@FileName, @FilePath)", con);
            cmd.Parameters.AddWithValue("@FileName", fileName);
            int getPos = filePath.LastIndexOf("\\");
            int len = filePath.Length;
            getPath = filePath.Substring(getPos, len - getPos);
            pathToStore = getPath.Remove(0, 1);
            cmd.Parameters.AddWithValue("@FilePath", pathToStore);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            
        }
        }

protected void btnShow_Click(object sender, EventArgs e)
    {
        SqlCommand cmd=new SqlCommand("select * from userimage where id="+8 ,con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Label1.Text = dr["FileName"].ToString();
            if (!string.IsNullOrEmpty(Convert.ToString(dr["FilePath"])))
            {
              Image1.ImageUrl = "~/Ads_Images/" + Convert.ToString(dr["FilePath"]);
            }
        }
        con.Close();
    }

The image is also not showing when i try to retreive through data reader, but when i debug code its fetching proper image value at imageurl.

There is some problem in folder(Ads_Images), It stores image in it but not fetching image from it, if i using the folder(Images) and try to store and fetch image then it works fine.
Anyone knows, why it happens???
I Dont want to store all user images with my Images folder.
Posted
Updated 22-May-14 4:16am
v4
Comments
KaushalJB 22-May-14 5:49am    
Put Updatepanel for your image if the retrieved path is fine.
Raj Negi 22-May-14 6:11am    
not working

you are saving to
C#
filePath = Server.MapPath("Ads_Images/" + System.Guid.NewGuid() + fileName);

And Loading from
C#
Image1.ImageUrl = "~/Ads_Images/" + Convert.ToString(dr["FilePath"]);

try with same like changing saving path to
C#
filePath = Server.MapPath("~/Ads_Images/" + System.Guid.NewGuid() + fileName);
 
Share this answer
 
Comments
Raj Negi 22-May-14 6:14am    
not working :(
DamithSL 22-May-14 6:30am    
you have hard code image id as 8, is there any record for that id and manually check for image existence for that image id in your Ads_Images folder
Raj Negi 22-May-14 6:35am    
yes, when i debug my code it is showing the correct path to the image but not show the image.
At Image1.ImageUrl i got this value on debugging "~/Ads_Images/6672cc38-e09e-44d0-a3cc-343420bbedd66030.jpg"
And in Ads_Images folder i have image with this name "6672cc38-e09e-44d0-a3cc-343420bbedd66030.jpg"
DamithSL 22-May-14 8:23am    
have you check the value of filePath? copy that path and try to open it.
Raj Negi 22-May-14 10:06am    
not work...there is some problem in folder(Ads_Images), It stores image in it but not fetching image from it, if i using the folder(Images) and try to store and fetch image then it works fine.
Don't know why???
No a GUID is actually a good idea to avoid naming collisions. Make sure to avoid spaces.

For relative paths always use ./ in front of the path if you want to start from the current folder. I´ve had issues where I didn´t do this.
so in your case:

C#
<asp:image id="Image1" runat="server" imageurl="./Ads_Images/7f0e7ef3-4cb8-40e0-a835-00314768a70a70-515.jpg" xmlns:asp="#unknown" />
<asp:image id="Image2" runat="server" imageurl="./Images/imagenotavailable.jpg" xmlns:asp="#unknown" />


Secondly, is Ads_Images a folder in the same level as the Images folder?
Thirdly, if the user saves the image to this folder, the control needs to be updated in order for the image to show.
In your case you need to wait until the images exists on disk and or reload the page or call some ajax update script to update the image.

Hope this gives you some ideas.
 
Share this answer
 
Comments
Raj Negi 22-May-14 6:06am    
i changes to...
<asp:image id="Image1" runat="server" imageurl="./Ads_Images/7f0e7ef3-4cb8-40e0-a835-00314768a70a70-515.jpg" xmlns:asp="#unknown" />
but still image is not showing.
If <asp:Image ID="Image2" runat="server" ImageUrl="Images/imagenotavailable.jpg" /> is working fine...why second image is not working, it is just a folder same as Images folder but with the images uploaded by users.
V. 22-May-14 12:05pm    
did you reload the page or the control?
Raj Negi 22-May-14 14:33pm    
yes ofcourse, Actually there is some problem in folder(Ads_Images), It stores image in it but not fetching image from it, if i using the folder(Images) and try to store and fetch image then it works fine.
There is some problem in Ads_Images folder, I create a new folder(Image) and try to store and fetch images...now its working. There is no problem in the code but still i do not understand why Ads_Images folder images are not accessible. Anyway now i am able to store & retreive images with my new folder. Thanks to all for your answers. This problem already consumes too much time.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900