Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi, i'm a beginner here

I have images in datalist which i get from database. I want to be able to click on an image which would than take me to a detail page where the same image would be displayed and the information about the image would be displayed too.

how do i display the image in my detail page ?

i save the image name from my folder in my database , e.g chocolate.jpg

this is my current code not sure if its the correct way of doing it

in product.aspx
CSS
<ItemTemplate>

                    
                    <asp:HyperLink ID="hlnk" NavigateUrl='<%# string.Format("ViewProductDetail.aspx?ProductID={0}", Eval("ProductID")) %>' runat="server">
                    
             <asp:Image ID="Image1" runat="server" Height="127px" 
                    ImageUrl='<%# Bind("Picture", "~/Image/{0}") %>' Width="129px" />
                    
                    </asp:HyperLink>

                    <br />
            <asp:Label ID="ProductNameLabel" runat="server" 
                Text='<%# Eval("ProductName") %>' />
            <br />
            <asp:Label ID="PriceLabel" runat="server" 
                Text='<%# Eval("Price","{0:C}") %>' />
            <br />
            <br />
        </ItemTemplate>


in ProductDetail.aspx.cs
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.Count != 0)
        {

            int ProductID = Convert.ToInt32(Request.QueryString["ProductID"]);
            if (!IsPostBack)
            {
                string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection myConnect = new SqlConnection(strConnectionString);
                SqlCommand cmd = new SqlCommand("SELECT ProductID, ProductName, Price, Description, Picture FROM Product WHERE ProductID=" + ProductID, myConnect);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "Product");

                myConnect.Open();
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {

                    txtProductName.Text = reader["ProductName"].ToString();
                    txtPrice.Text = reader["Price"].ToString();
                    txtDescription.Text = reader["Description"].ToString();
                    image????

                }
            }
        }
    }


does my ProductDetail.aspx require another datalist?
Posted
Comments
JoCodes 12-Dec-13 4:49am    
Whats the issue faced in this code?

The code seems to be fine for your requirement.

The second question is whether a datalist is needed in the Details page- which is not required since there is only one detailed image to display at a time.

You can easily use a image control with the path within a div accompanied with some literal controls or paragragh to display the other details.

Hope this helps you...
 
Share this answer
 
HI
try like this,


in parent page:

XML
<asp:HyperLink ID="hlnk" NavigateUrl='<%# string.Format("ViewProductDetail.aspx?ProductID={0}&Picture={1}", Eval("ProductID"), Eval("Picture")) %>' runat="server">


in details page:


C#
int ProductID = Convert.ToInt32(Request.QueryString["ProductID"]);
                string picture = "~/Image/" + Request.QueryString["Picture"];
                Image1.ImageUrl = picture;
 
Share this answer
 
Comments
shiiny 12-Dec-13 5:41am    
thx , it work :)
Karthik_Mahalingam 12-Dec-13 5:43am    
:) welcome
shiiny 5-Jan-14 21:25pm    
hi , is there any other way to display the image? because i only wan the url to be ProductID=1 instead of ProductID=1&Picture=makeup.jpg
Karthik_Mahalingam 5-Jan-14 23:56pm    
yes u can get the image url from database... by passing the product id to the db..
Member 10273213 7-May-14 2:50am    
what is the ~/Image/ thing in that?

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