binding image control to dataset

I have a image control in my page. I am retrieving image paths and stored on dataset and want to bind to image control. (how will i do it without gridview)

example : if 10 image paths coming from dataset then it will automatically  display 10 images in webpage. How will i do it.
LVL 22
Tapan PattanaikSenior EngineerAsked:
Who is Participating?
Kishan ZunjareConnect With a Mentor Sr. Software EngineerCommented:
//Convert DataSet to DataTable by getting DataTable at first zero-based index
DataTable myDataTable = myDataSet.Tables[0];  
//Loop over the rows.
foreach (DataRow row in myDataTable.Rows)
 Image image = new Image();
 Image1.ImageUrl =dRow[ "path"].ToString();

Hope this will help for you..
Kishan ZunjareSr. Software EngineerCommented:

First you have to do for loop, get one by one path from dataset; take some placeholder in aspx page like any table or panel and then in For loop create new object of image and add this image control to that placeholder.

This is psuedo code;
for (int i = 0; i < datasetCount; i++){
Image image = new Image ();
Image1.ImageUrl = path;

Hope this helps;
Tapan PattanaikSenior EngineerAuthor Commented:
Hi Kishan,

can you share more in depths.
Robert SchuttConnect With a Mentor Software EngineerCommented:
The previous solution should work fine, however it's also possible to use a Repeater and data binding, which is closer to how I read the question.

Put this in your aspx (change field names in Eval(...) to your own):
    <asp:Repeater ID="ImageRepeater1" runat="server">
        <ItemTemplate><div class="PrettyPicture"><img alt="<%# Eval("ImageDescription") %>" title="<%# Eval("ImageDescription") %>" src="<%# Eval("ImagePath") %>"></div></ItemTemplate>

Open in new window

and put this style for example in your <head>:
    <style type="text/css">
            border: solid 2px gray;
            background-color: silver;
            margin: 4px;
            padding: 6px;
            float: left;

Open in new window

and in code-behind, for testing I just used Page load, the test data you can leave out of course, just bind to your own dataset/table:
        protected void Page_Load(object sender, EventArgs e) {
            // test data
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("ImageDescription", typeof(string)), new DataColumn("ImagePath", typeof(string)) });
            dt.Rows.Add(new object[] { "image 1", "images/image1.png" });
            dt.Rows.Add(new object[] { "image 2", "images/image2.png" });
            dt.Rows.Add(new object[] { "image 3", "images/image3.png" });

            // bind repeater control and data
            ImageRepeater1.DataSource = dt;

Open in new window

Tapan PattanaikSenior EngineerAuthor Commented:
Thanks to all.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.