473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Retrive data from dynamically created table

210 Expert 100+
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

Expand|Select|Wrap|Line Numbers
  1.  
  2.  protected void LoadCityStateControl(int origin, int destination)
  3.     {
  4.         //initializing the origin city/state controls
  5.         Table city = new Table();
  6.         city.ID = "tblcity";
  7.         city.BorderStyle = BorderStyle.Solid;
  8.         city.BorderWidth = 1;
  9.  
  10.  
  11.         for (int i = 0; i < origin; i++)
  12.         {
  13.             TableRow tr = new TableRow();
  14.             TableCell tc = new TableCell();
  15.             tc.ID = "tc" + (i + 1);
  16.             Label mx = new Label();
  17.             mx.Text = "Origin City/State #" + (i + 1) + " : ";
  18.             mx.ID = "lblorgcitystate" + (i + 1);
  19.  
  20.             TextBox tx = new TextBox();
  21.             tx.ID = "txtOrgCityState" + (i + 1);
  22.  
  23.             Label mn = new Label();
  24.             mn.ID = "lbltest" + (i + 1);
  25.             mn.Text = "  (eg: Cape Girardeau,MO )";
  26.  
  27.             tc.Controls.Add(mx);
  28.             tc.Controls.Add(tx);
  29.             tc.Controls.Add(mn);
  30.             tr.Cells.Add(tc);
  31.             city.Rows.Add(tr);
  32.         }
  33.         cpanel.Controls.Add(city);
  34.  
  35.         //initializing the destination city/state controls
  36.         Table descity = new Table();
  37.         descity.ID = "tbldescity";
  38.         descity.BorderStyle = BorderStyle.Solid;
  39.         descity.BorderWidth = 1;
  40.  
  41.         for (int i = 0; i < destination; i++)
  42.         {
  43.             TableRow trd = new TableRow();
  44.             TableCell tcd = new TableCell();
  45.             tcd.ID = "tcd" + (i + 1);
  46.             Label mxd = new Label();
  47.             mxd.Text = "Destination City/State #" + (i + 1) + " : ";
  48.             mxd.ID = "lbldestcitystate" + (i + 1);
  49.  
  50.             TextBox txd = new TextBox();
  51.             txd.ID = "txtDestCityState" + (i + 1);
  52.  
  53.             Label mnd = new Label();
  54.             mnd.ID = "lbldtest" + (i + 1);
  55.             mnd.Text = "  (eg: Dallas,TX )";
  56.  
  57.             tcd.Controls.Add(mxd);
  58.             tcd.Controls.Add(txd);
  59.             tcd.Controls.Add(mnd);
  60.             trd.Cells.Add(tcd);
  61.             descity.Rows.Add(trd);
  62.         }
  63.         cpanel.Controls.Add(descity);
  64.  
  65.  
  66.     }
  67.  
Here is the code to retrieve data

Expand|Select|Wrap|Line Numbers
  1.         int orgcounter = Int32.Parse(txtNumPicks.Text);
  2.         int descounter = Int32.Parse(txtNumDrops.Text);
  3.  
  4.         for (int i = 0; i <= orgcounter; i++)
  5.         {
  6.             foreach (Control c in cpanel.Controls)
  7.             {
  8.                  Table m = (Table)c.FindControl("tbldescity");
  9.                 foreach (TableRow r in m.Rows)
  10.                  {
  11.                     foreach (TableCell tc in r.Cells)
  12.                    {
  13.                        orgcity = ((TextBox)tc.FindControl("txtOrgCityState" +  (i).ToString())).Text;
  14.                    }
  15.  
  16.                 }
  17.             }
  18.        }
  19.  
  20.  
I just noticed that if i try to get the controls count inside the panel i get a value zero. Why is this happening.

Expand|Select|Wrap|Line Numbers
  1.  lblerr.Text = c.Controls.Count.ToString();
  2.  
Please help . Thank you in advance
Nov 5 '08 #1
2 2446
Plater
7,872 Expert 4TB
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
Nov 5 '08 #2
semomaniz
210 Expert 100+
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.
Nov 5 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Michael Johnson Jr. | last post by:
I am trying to handle a button click event, which updates a web control table with data. The button is dynamically created in the table itself. When I call updateTable() in the Page_Load the new...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
3
by: Forconi Boris via .NET 247 | last post by:
Hi, I'm working on a project in witch I have to list data(products that can be selected with a certain quantity) from anXML document, with key words entered by the user. A Table isdynamically created...
3
by: Tim | last post by:
Hello All, Hope someone can point me in the right direction here. I have a ASPX page that is being created dynamically. My function adds Labels and Text Boxes to a Table ready for the user to...
2
by: Terry | last post by:
Any .dll or COM+ for .Net can help me retrive data from a Access file? The table contain several columns and the last one is a container which I store Article; because each article is a little bit...
2
by: Gian Paolo | last post by:
Hi all on .net 2.0 i have a dataset and a table with some rows. Is there a way to retrive the current row index ? So i can retrive other value of that column... something like...
4
by: assgar | last post by:
Hi I am stuck on a problem. I use 3 scripts(form, function and process). Development on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. The form displays...
23
nehashri
by: nehashri | last post by:
hi i am designing a database using Ms Access and ASP. i have 3 tables in access namely 'PERSONAL', other as 'POLICY' and 3rd one is named as 'STAFF'. in the contact table i have ID, Name, Children...
1
by: amritranjan | last post by:
How to retrive image file from MS access database and display this in another JSPpage -------------------------------------------------------------------------------- This is my Jsp code for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.