hi, I'm doing a Real Estate Website where I have an upload image web form which saves the image paths of the images into my database. I am trying to view details of the property in another web form, but I can't retrieve the images, only the data is being retrieved.

The following is the code I used to save the details of the property:

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(connString);
        con.Open();
            SqlCommand storeimage = new SqlCommand("INSERT INTO Properties "
        + "(RefNo, Price, Location, Type, NoOfBedrooms, Description, Basis, Image_Path1, Image_Path2, Image_Path3, Image_Path4, Image_Path5, Image_Path6) "
        + " values ('" + Label1.Text + "','" + Price.Text + "' , '" + Location.SelectedItem.Value + "','" + Type.SelectedItem.Value + "','" + Bedrooms.SelectedItem.Value + "','" + Description.Text + "','" + Basis.SelectedItem.Value + "','" + Imageupload.FileName + "','" + Imageupload1.FileName + "','" + Imageupload2.FileName + "','" + Imageupload3.FileName + "','" + Imageupload4.FileName + "','" + Imageupload5.FileName + "')", con);


            string strPath = Server.MapPath("Properties");
            string strPath1 = Server.MapPath("Properties");
            string strPath2 = Server.MapPath("Properties");
            if (Imageupload.HasFile)
            {
                Imageupload.SaveAs (strPath + "\\" + Imageupload.FileName);
            }

            if (Imageupload1.HasFile)
            {
                Imageupload1.SaveAs(strPath1 + "\\" + Imageupload1.FileName);
            }

            if (Imageupload2.HasFile)
            {
                Imageupload2.SaveAs(strPath2 + "\\" + Imageupload2.FileName);
            }

            if (Imageupload3.HasFile)
            {
                Imageupload3.SaveAs(strPath2 + "\\" + Imageupload3.FileName);
            }
            if (Imageupload4.HasFile)
            {
                Imageupload4.SaveAs(strPath2 + "\\" + Imageupload4.FileName);
            }
            if (Imageupload5.HasFile)
            {
                Imageupload5.SaveAs(strPath2 + "\\" + Imageupload5.FileName);
            }
            storeimage.ExecuteNonQuery();
            con.Close();

The following is the aspx code which shows the form and the C# code I am using to bind text boxes with details.

ASPX:

<div id=image1>
   <asp:Image ID="img1" runat="server" Height="120px" Width="100px" /> 
    </div>
<div id=image2>
    <asp:Image ID="img2" runat="server" Height="120px" Width="100px" />
    </div>
<div id=image3>
    <asp:Image ID="img3" runat="server" Width="100px" Height="120px"/>
    </div>
<div id=image4>
    <asp:Image ID="img4" runat="server" Height="120px" Width="100px" />
    </div>
<div id=image5>
    <asp:Image ID="img5" runat="server" Height="120px" Width="100px" />
    </div>


<div id=text>

<asp:Label ID=type runat=server Text="Type:"></asp:Label>

<br />

<asp:Label ID=refno runat=server Text="Reference Number:"></asp:Label>
<br />


<asp:Label ID=noofbedrooms runat=server Text="Bedrooms:"></asp:Label>

<br />


<asp:Label ID=price runat=server Text="Price:"></asp:Label>

<br />

<asp:Label ID=locality runat=server Text="Locality:"></asp:Label>

<br />

<asp:Label ID=description runat=server Text="Description:"></asp:Label>

<br />

C#

protected void Page_Load(object sender, EventArgs e)
    {
        if (!(IsPostBack))
        {
             string s = Request.QueryString["RefNo"].ToString();
            string select = "SELECT RefNo, Price, Location, Type, NoOfBedrooms, Description, Image_Path1, Image_Path2, Image_Path3, Image_Path4, Image_Path5 FROM Properties WHERE RefNo = " + s + "";


            SqlConnection con = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(select, con);
            SqlDataReader dr;
            con.Open();
            dr = cmd.ExecuteReader();


            
            while (dr.Read())
            {

                reference.Text = dr[0].ToString();
                price1.Text = dr[1].ToString();
                loc.Text = dr[2].ToString();
                type1.Text = dr[3].ToString();
                bedrooms.Text = dr[4].ToString();
                desc.Text = dr[5].ToString();
      
             }

            con.Close();
        }

Can you please help me with retrieving the images also?

Thanks in advance

Dani AI

Generated

You are running into the classic physical-path vs. URL mix‑up. Server.MapPath gives you a physical file system path on the server (for saving files). The Image.ImageUrl property must be a web URL the browser can request (relative like ~/Properties/house.jpg or absolute like /YourApp/Properties/house.jpg). In short: save with a physical path, store a virtual path, and bind the virtual path back to ImageUrl. was right not to use Server.MapPath when assigning ImageUrl, and ’s tip to store a URL-style path is the key.

On upload, build both paths once and store the virtual one in the database:

var fileName = Path.GetFileName(Imageupload.FileName);
var virtualPath = "~/Properties/" + fileName;        // store this in DB
var physicalPath = Server.MapPath(virtualPath);      // save the file here
Imageupload.SaveAs(physicalPath);

// INSERT ... @Image_Path1 = virtualPath (use parameters, not string concat)

On the details page, read that stored virtual path and resolve it for the client:

if (!dr.IsDBNull(dr.GetOrdinal("Image_Path1")))
{
    var vp = dr["Image_Path1"].ToString();           // e.g., "~/Properties/house.jpg"
    img1.ImageUrl = ResolveUrl(vp);                  // becomes app-rooted URL
}

Quick checks if it still does not render:

  • Ensure the images ended up under your web app (e.g., /YourApp/Properties) and that folder allows static file reads by IIS.
  • Store forward slashes in DB (~/Properties/file.jpg), not backslashes. Do not prepend physical paths like C:... to ImageUrl.
  • Parameterize your SQL for both INSERT and SELECT (e.g., WHERE RefNo = @refNo) to avoid injection and quoting issues.

This workflow cleanly separates save location from the browser-facing URL and should make your images appear as expected.

Recommended Answers

All 9 Replies

Firstly, this is an asp.net question, it should be in the asp.net forum. But since the code here is in C# I can understand why you posted it here.

There should be some img1.ImageUrl = dr[6]; ect ect

images are still not shown that way. could there be something wrong with the asp coding or the way that i've stored them?

well, nowhere in your posted code do you attempt to show them.

i know.. and i tried the img1.imageurl = dr[6].ToString and still they dont show

i am now using the following code however even though the red cross is not there anymore, the image is still not showing

string filepath = dr.GetString(6);
img1.ImageUrl = Server.MapPath(filepath);

The image url shouldn't be and absolute path, it should be a virtual path, ex ""

so you shouldn't be using Server.MapPath()

I trid the following:

String imgpath = ("Properties");
img1.ImageUrl = (imgpath + "//" + dr[6]);

however im getting an error saying the path does not exist.

you need the full virtual path to your properties folder (assuming thats where they are uploaded too) and then append the file name.

Hmmmm,

Assuming you have saved your images in the Images directory and you are storing the path in your SQL database as...

~/Images/Properties/Image1.gif

Then you should have no problem in using:

Image.ImageUrl=dr[6];

Please check the path that's being returned in dr[6]!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.