Retrive data from dynamically created table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    #1

    Retrive data from dynamically created table

    I have created two tables dynamically and have added textboxes inside the table. now i am trying to retrieve the data from the the textboxes and i am getting a null reference error. I am not sure why i am getting this error. Please advice.

    Code to set the tables

    Code:
     protected void LoadCityStateControl(int origin, int destination)
        {
            //initializing the origin city/state controls
            Table city = new Table();
            city.ID = "tblcity";
            city.BorderStyle = BorderStyle.Solid;
            city.BorderWidth = 1;
    
    
            for (int i = 0; i < origin; i++)
            {
                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                tc.ID = "tc" + (i + 1);
                Label mx = new Label();
                mx.Text = "Origin City/State #" + (i + 1) + " : ";
                mx.ID = "lblorgcitystate" + (i + 1);
    
                TextBox tx = new TextBox();
                tx.ID = "txtOrgCityState" + (i + 1);
    
                Label mn = new Label();
                mn.ID = "lbltest" + (i + 1);
                mn.Text = "  (eg: Cape Girardeau,MO )";
    
                tc.Controls.Add(mx);
                tc.Controls.Add(tx);
                tc.Controls.Add(mn);
                tr.Cells.Add(tc);
                city.Rows.Add(tr);
            }
            cpanel.Controls.Add(city);
    
            //initializing the destination city/state controls
            Table descity = new Table();
            descity.ID = "tbldescity";
            descity.BorderStyle = BorderStyle.Solid;
            descity.BorderWidth = 1;
    
            for (int i = 0; i < destination; i++)
            {
                TableRow trd = new TableRow();
                TableCell tcd = new TableCell();
                tcd.ID = "tcd" + (i + 1);
                Label mxd = new Label();
                mxd.Text = "Destination City/State #" + (i + 1) + " : ";
                mxd.ID = "lbldestcitystate" + (i + 1);
    
                TextBox txd = new TextBox();
                txd.ID = "txtDestCityState" + (i + 1);
    
                Label mnd = new Label();
                mnd.ID = "lbldtest" + (i + 1);
                mnd.Text = "  (eg: Dallas,TX )";
    
                tcd.Controls.Add(mxd);
                tcd.Controls.Add(txd);
                tcd.Controls.Add(mnd);
                trd.Cells.Add(tcd);
                descity.Rows.Add(trd);
            }
            cpanel.Controls.Add(descity);
    
    
        }
    Here is the code to retrieve data

    Code:
            int orgcounter = Int32.Parse(txtNumPicks.Text);
            int descounter = Int32.Parse(txtNumDrops.Text);
    
            for (int i = 0; i <= orgcounter; i++)
            {
                foreach (Control c in cpanel.Controls)
                {
                     Table m = (Table)c.FindControl("tbldescity");
                    foreach (TableRow r in m.Rows)
                     {
                        foreach (TableCell tc in r.Cells)
                       {
                           orgcity = ((TextBox)tc.FindControl("txtOrgCityState" +  (i).ToString())).Text;
                       }
    
                    }
                }
           }
    I just noticed that if i try to get the controls count inside the panel i get a value zero. Why is this happening.

    Code:
     lblerr.Text = c.Controls.Count.ToString();
    Please help . Thank you in advance
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    When you insert/name your controls you use i+1, when you search, you are using just i, so sometimes the FindControl will return a null value since it cannot find the control

    Comment

    • semomaniz
      Recognized Expert New Member
      • Oct 2007
      • 210

      #3
      I did put I+1 while searching and still i am getting the same error. I noticed that the reason why i was getting this error was i didnt set the form at page load event. The form is based on input that i get from another form. In other words i determine the count of pick and drop from a form which gets hidden after value is submitted. then the dynamically created form is displayed. If i recreate the form inside the click event of previous form submit button i still am not able to get the value from textboxes.

      To avoid this i am trying to redirect the page to the same page and have the form created dynamically in the page load event. But i am still not able to retrieve data.

      Comment

      Working...