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

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);

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.