Click here to Skip to main content
16,004,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void ReadTextBoxes()
{
    string strValue = string.Empty;
    int n = NumberOfControls;
    Panel pnl = (Panel)Page.FindControl("Panel1");

    for (int i = 0; i <= NumberOfControls; i++)
    {
        //:confused: This line Give me error please give me solution
        string boxName = "TextBoxID" + (i + 1).ToString();


        TextBox tb = pnl.FindControl(boxName) as TextBox;
        strValue += tb.Text + "\n";

    }
    Response.Write(strValue);

}
Posted
Updated 8-Mar-10 19:02pm
v2

1 solution

Either you are getting Panel Object value as null or textbox Object value as null.


REASON 1:
Error line must be:
strValue += tb.Text + "\n";

Issue1 to me looks at:
strValue += tb.Text + "\n";


Looks like you are getting "null" for tb value here:
TextBox tb = pnl.FindControl(boxName) as TextBox;


Reason:
string boxName = "TextBoxID" + (i + 1).ToString();



REASON 2:
Error line must be:
TextBox tb = pnl.FindControl(boxName) as TextBox;

If pnl value is null, then this line would throw an error.

Issue2 to me looks at:
TextBox tb = pnl.FindControl(boxName) as TextBox;


Looks like you are getting "null" for pnl value here:
Panel pnl = (Panel)Page.FindControl("Panel1");


Check that you have "Panel1" named Panel with you.

===========
I don't think you must be getting error at the location specified by you.
string boxName = "TextBoxID" + (i + 1).ToString();


There is nothing wrong with this statement.
 
Share this answer
 

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