Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3847332
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:32:08+00:00 2026-05-19T16:32:08+00:00

need help in tweaking my logic little bit. as you see i have two

  • 0

need help in tweaking my logic little bit.

as you see i have two foreach loop and both for different columns in gridview
this code is in OnRowUpdating in the gridview for more info what exactly i am doing you can see the closed thread here

the problem is:

on every loop i will be updating a row to db but since i have two different loop it will not exit till the first loop finishes.

for an example: i have _rpt.Count = 2 so it will loop twice before it hit the second loop.

GridViewRow row = gv.SelectedRow;           
Repeater _rpt = gv.Rows[e.RowIndex].Cells[8].FindControl("rptReg") as Repeater;
Repeater _rpt1 = gv.Rows[e.RowIndex].Cells[9].FindControl("rptVisitor") as Repeater;

foreach (RepeaterItem item in _rpt.Items)
{
    TextBox _txt = item.FindControl("txtId") as TextBox;
    TextBox _txt1 = item.FindControl("txtName") as TextBox;
    //update db
}

foreach (RepeaterItem item1 in _rpt1.Items)
{ 
    TextBox _txt3 = item1.FindControl("txtVisitor") as TextBox;
    //update db
} 

is there a way that i can read both foreach loop values?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:32:09+00:00Added an answer on May 19, 2026 at 4:32 pm

    If you’re doing a loop through multiple collections, and want to access each collection more than once, than a foreach may not be the best fit.

    Try a regular for loop instead:

    for (int i = 0; i < _rpt.Items.Count; i++)  
    {  
        TextBox _txt = _rpt.Items[i].FindControl("txtId") as TextBox;  
        TextBox _txt1 = _rpt.Items[i].FindControl("txtName") as TextBox;
    
        if (_rpt1.Items.Count > i)
            TextBox _txt3 = _rpt1.Items[i].FindControl("txtVisitor") as TextBox;
    
        //update db
    } 
    

    The above will only work as expected if (_rpt.Items.Count >= _rpt1.Items.Count), so be sure to check for and use the collection with the most items if they are ever going to be different.

    Using a foreach loop enumerates the items within the single collection, allowing you a direct reference to each item within each iteration. This is handy if you are only accessing the one collection, as you don’t need to use array indexers, and can just use the name you give it at the loop initialization.

    It’s not so handy when you are looping through multiple collections, as you ONLY have a reference to the collection used in the loop header.

    Using a for loop allows you to keep track of which index you are at (using an int), allowing you to just use standard array notation to get the items in the collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help with the following. I am creating two different combinations of view/controllers that
Need help with a query that I wrote: I have three tables Company id
need help/guide for sql select query, I have 2 table stock and stock_history, in
need help regarding USSD Gateway. I have to develop an app, which will directly
need help in error in database pivot. i have table tamed table_score like below:
Need help. I have Graph API code that uses an auth token with offline
Need help... I have 3 classes, Manager which holds 2 pointers. One to class
Need help for that. I have a tabbar with 2 views, A and B
need help to read xml file. i have one xml file which i want
I need some help tweaking my code to look for another attribute in this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.