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
Here is the code to retrieve data
I just noticed that if i try to get the controls count inside the panel i get a value zero. Why is this happening.
Please help . Thank you in advance
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);
}
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;
}
}
}
}
Code:
lblerr.Text = c.Controls.Count.ToString();
Comment