I am using the following code to iterate through a DataGrid to change the
value in one of the columns:
private void UpdateFCA()
{
for (int i = 0; i <= reportGrid.Items.Count; i++)
{
DataGridItem item = reportGrid.Items[i];
TextBox fcaText = (TextBox)item.FindControl("fca");
if (fcaText.Text == "")
{
fcaText.Text = "N";
}
else
{
fcaText.Text = "Y";
}
}
}
When I run the code I get the following error:
Server Error in '/5YrPlan' Application
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Source Error:
Line 143: DataGridItem item = reportGrid.Items[i];
Line 144: TextBox fcaText = (TextBox)item.FindControl("fca");
Line 145: if (fcaText.Text == "")
Line 146: {
Line 147: fcaText.Text = "N";
Source File: c:\inetpub\wwwroot\5yrplan\reportform.aspx.cs Line: 145
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
_5YrPlan.reportForm.UpdateFCA() in
c:\inetpub\wwwroot\5yrplan\reportform.aspx.cs:145
_5YrPlan.reportForm.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\5yrplan\reportform.aspx.cs:33
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750
Can anyone help with the code to make this error go away?
Thanks,
Dave