?
Solved

Loop Through a Datatable

Posted on 2011-09-13
8
Medium Priority
?
273 Views
Last Modified: 2012-05-12
Hi all,
I would like to loop through the result of my query and display the images in my aspx page.

The code is getting the desired result from the database at the moment, but, I am only able to get the first image displayed whereas I would like to display more if there are more than one in the result
 Please see the code below.
string imagefinder = "SELECT    dbo.Digital_Camera_Images.DCImgID, dbo.Digital_Camera_Images.RelatedFilmID FROM  dbo.Digital_Camera_Images RIGHT OUTER JOIN dbo.Cases ON dbo.Digital_Camera_Images.RelatedCaseID = dbo.Cases.CaseID WHERE     (dbo.Digital_Camera_Images.DCImgID IS NOT NULL) AND (dbo.Cases.CaseID IN(SELECT CaseID FROM dbo.Cases WHERE (FormattedCaseNo = '" + pcn.Text + "') AND (VRM = '" + vrm.Text + "'))) ";

                    SqlCommand myimageCommand = new SqlCommand(imagefinder, myConnection);

                    DataTable myimageDataTable = new DataTable();
                    SqlDataAdapter myimageSQLDataAdapter = new SqlDataAdapter(myimageCommand);

                    myimageSQLDataAdapter.Fill(myimageDataTable);


                    pcnGridView.DataSource = myDataTable;
                    pcnGridView.DataBind();
                   

                    string imageUrl = spurFolder + myimageDataTable.Rows[0].ItemArray[1] + "\\" + "000000000" + myimageDataTable.Rows[0].ItemArray[0] + ".jpg"; 

                    Image1.ImageUrl = imageUrl;
                    Image1.Width=200;
                    Image1.Height = 200;

Open in new window

0
Comment
Question by:olootu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 4

Accepted Solution

by:
nost2 earned 1000 total points
ID: 36529225
You can loop through the rows with for each:
SqlCommand myimageCommand = new SqlCommand(imagefinder, myConnection);

            DataTable myimageDataTable = new DataTable();
            SqlDataAdapter myimageSQLDataAdapter = new SqlDataAdapter(myimageCommand);

            myimageSQLDataAdapter.Fill(myimageDataTable);


            pcnGridView.DataSource = myDataTable;
            pcnGridView.DataBind();

            string spurFolder = "";
            foreach (DataRow row in myimageDataTable.Rows)
            {
                string imageUrl = spurFolder + row.ItemArray[1] + "\\" + "000000000" + row.ItemArray[0] + ".jpg";

                Image1.ImageUrl = imageUrl;
                Image1.Width = 200;
                Image1.Height = 200;
            }

Open in new window

0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 36529375
If you're trying to populate the GridView, which is what I think you're trying to do, then you need to implment the RowDataBound() event properly.

In your OnInit() do this:

pncGridView.RowDataBound += new GridViewRowEventHandler( pncGridView_RowDataBound );

Then, in the class itself have this event defined:

        void pncGridView_RowDataBound( object sender, GridViewRowEventArgs e )
        {
            if ( e.Row.RowType == DataControlRowType.DataRow )
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
                DataRow row = drv.Row;
               
                // The rest you should be able to do.  It will call this event for each row returned in
                // myDataTable automatically.
             }
        }

0
 

Author Comment

by:olootu
ID: 36529518
@nost2:
Your code works but it is producing the same result as mine: only the first row image is showing.
Do I need to add more Image control to the aspx page?

I want to display more than one image.

@Dan7el:
No I am not populating the gridview in this scenario. The gridview is being populated from another query which I have ommitted from the code above.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:mr_nadger
ID: 36535466
am I being daft here, or is there only the one image control in the grid, which is being reset for every value?
If you're trying to display multiple images, won't you need to instantiate an image control for each one?
0
 

Author Comment

by:olootu
ID: 36537354
@mr_nadger,

As I  explained above, I am not using any gridview in this scenario.(I have  now taken the gridview off the code below for clarity). What I am trying to do is to add the image dynamically like this:

1. query the database
2. loop through the result
3. add an image control to the .aspx (<div id="infobox" runat="server"><asp:Image ID="Image1"  runat="server" /> </div>)
4. put the result of the loop above to the ID of the image control to display the images.
This is working but only displays one image. I have tried the same code with a hyperlink control and this lists ALL the returned result (please see below).

My questions is: why is this working for Hyperlink control (displays all the 9 results) but not working for image control (that displays ONE result)?

string imagefinder = "SELECT    dbo.Digital_Camera_Images.DCImgID, dbo.Digital_Camera_Images.RelatedFilmID FROM  dbo.Digital_Camera_Images RIGHT OUTER JOIN dbo.Cases ON dbo.Digital_Camera_Images.RelatedCaseID = dbo.Cases.CaseID WHERE     (dbo.Digital_Camera_Images.DCImgID IS NOT NULL) AND (dbo.Cases.CaseID IN(SELECT CaseID FROM dbo.Cases WHERE (FormattedCaseNo = '" + pcn.Text + "') AND (VRM = '" + vrm.Text + "'))) ";

                    SqlCommand myimageCommand = new SqlCommand(imagefinder, myConnection);

                    DataTable myimageDataTable = new DataTable();
                    SqlDataAdapter myimageSQLDataAdapter = new SqlDataAdapter(myimageCommand);

                    myimageSQLDataAdapter.Fill(myimageDataTable);


                     foreach (DataRow dt in myimageDataTable.Rows)
                    {
                        HyperLink ola = (HyperLink)new HyperLink();

                        ola.Text =   dt["DcImgID"].ToString();
                        ola.NavigateUrl = "~/pcnHandler.ashx?film=" + dt[1] + "&photo=" + "000000000" + dt["DcImgID"];
                       infobox.Controls.Add(ola);

                      }

Open in new window

0
 

Author Comment

by:olootu
ID: 36541821
Thanks all. It is now resolved. What I did; I created an Imagebutton on the fly.
string imagefinder = "SELECT    dbo.Digital_Camera_Images.DCImgID, dbo.Digital_Camera_Images.RelatedFilmID FROM  dbo.Digital_Camera_Images RIGHT OUTER JOIN dbo.Cases ON dbo.Digital_Camera_Images.RelatedCaseID = dbo.Cases.CaseID WHERE     (dbo.Digital_Camera_Images.DCImgID IS NOT NULL) AND (dbo.Cases.CaseID IN(SELECT CaseID FROM dbo.Cases WHERE (FormattedCaseNo = '" + pcn.Text + "') AND (VRM = '" + vrm.Text + "'))) ";

                    SqlCommand myimageCommand = new SqlCommand(imagefinder, myConnection);

                    DataTable myimageDataTable = new DataTable();
                    SqlDataAdapter myimageSQLDataAdapter = new SqlDataAdapter(myimageCommand);

                    myimageSQLDataAdapter.Fill(myimageDataTable);


                    pcnGridView.DataSource = myDataTable;
                    pcnGridView.DataBind();

                    foreach (DataRow dt in myimageDataTable.Rows)
                    {
                        ImageButton img_btn = new ImageButton();
                        img_btn.Height = 90;
                        img_btn.Width = 90;
                        img_btn.AlternateText = "PCN Image ";
                        img_btn.Style.Value = "margin-right:5px;margin-bottom:5px;";
                        img_btn.PostBackUrl = "~/pcnHandler.ashx?film=" + dt["RelatedFilmID"].ToString() + "&photo=" + "000000000" + dt["DCImgID"].ToString();
                        img_btn.ImageUrl = spurFolder + dt["RelatedFilmID"].ToString() + "\\" + "000000000" + dt["DCImgID"].ToString() + ".jpg";
                        imagedisplay.Controls.AddAt(1,img_btn);
                       
                       
                    }

Open in new window

0
 

Author Closing Comment

by:olootu
ID: 36541831
I added an image button on the fly to display each image.
0
 
LVL 7

Expert Comment

by:mr_nadger
ID: 36541972
which is the point I was making, you needed to add a button per image
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question