Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am using asp Table in my page.

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TableRow tr = new TableRow();
            }
            else
            {
                if (Session["tblSystem"] != null)
                {
                    Table tb = (Table)Session["tblSystem"];
                    int count = tb.Rows.Count;
                    foreach (TableRow tr in tb.Rows)
                    {
                        tblSystem.Rows.Add(tr);
                    }
                }
            }
        }
        protected void CreateTextBox(object sender, ImageClickEventArgs e)
        {
            TextBox MyTextBox = new TextBox();
            //Assigning the textbox ID name 
            ImageButton img = sender as ImageButton;
            TableRow row = new TableRow();

            string button = img.AlternateText;
            switch (button)
            {
                case "System":
                    TableCell cell = new TableCell();
                    TableCell cell1 = new TableCell();
                    ImageButton minus = new ImageButton();

                    cell.ID = "tblCell" + ++system;
                    cell1.ID = "tblCell1" + system;
                    row.ID = "tblRow" + system;

                    minus.ID = "imgSystem" + system;
                    minus.ImageUrl = "~/Images/minus.png";

                    MyTextBox.ID = "txtSystem" + system;
                    MyTextBox.Width = 145;
                    MyTextBox.Height = 17;
                    MyTextBox.TextMode = TextBoxMode.SingleLine;

                    cell.Controls.Add(MyTextBox);
                    cell1.Controls.Add(minus);
                    //cell.Text = "Iteration : " + system.ToString();
                    //cell1.Text = "Iteration : " + system.ToString();
                    row.Cells.Add(cell);
                    row.Cells.Add(cell1);
                    tblSystem.Rows.Add(row);
                    Session["tblSystem"] = tblSystem;
                    minus.Click += new System.Web.UI.ImageClickEventHandler(deleteRow);
                    //pnlSystem.Controls.Add(MyTextBox);
                    break;
            }
        }
        protected void deleteRow(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            TableRow row = new TableRow();
            ImageButton img = sender as ImageButton;
            string val = img.ID;
            string id = val.Substring(val.Length - 1, 1);
            if (val.Contains("System"))
            {
                TableRow rw = (TableRow)row.FindControl("tblRow" + id);
                rw.Visible = false;
            }
            Session["tblSystem"] = tblSystem;
        }


I am getting error in foreach in load event. Please help.
Thanks in advance
Posted

1 solution

You get this error when using the collection in foreach and with in the loop the collection is manipulated by adding or removing the items. One possible alternative is to use for loop instead. Good luck.
 
Share this answer
 
Comments
srmohanr 20-Mar-14 0:00am    
Thanks for your help.
I have tried for loop.
Table tb = (Table)Session["tblSystem"];
if (Session["tblSystem"] != null)
{
int count = tb.Rows.Count;
for (int cnt = 1; cnt < count; cnt++)
{
TableRow tr = tb.Rows[cnt];
tblSystem.Rows.Add(tr);
}
}

It's showing me error 'Specified argument was out of the range of valid values.' Please help
ArunRajendra 20-Mar-14 2:06am    
Which line are you getting the error? you are working with index and index starts at 0.
srmohanr 20-Mar-14 2:08am    
I am getting error at index 2.
ArunRajendra 20-Mar-14 2:14am    
what is the count
srmohanr 20-Mar-14 2:23am    
count is 3

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