Problem Iterating Through A Datagrid

G

Guest

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;
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;
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
 
G

Guest

Hi,

Try
for (int i = 0; i <= reportGrid.Items.Count - 1; i++)
instead of
for (int i = 0; i <= reportGrid.Items.Count; i++)

I believe Count has the exact count of items. But your int i starts with 0.

Ganesh
 
G

Guest

I did as you suggested. However, the problem persists.

Many Thanks

Ganesh said:
Hi,

Try
for (int i = 0; i <= reportGrid.Items.Count - 1; i++)
instead of
for (int i = 0; i <= reportGrid.Items.Count; i++)

I believe Count has the exact count of items. But your int i starts with 0.

Ganesh

kscdavefl said:
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;
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;
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
 
D

Daniel Billingsley

Have you done any debugging? I would expect it happens even on the very
first time (i=0). Is that so?

If fcaText is null when trying to check the Text property, as seems to be
the case, it means that FindControl couldn't find "fca". That's what you'd
need to fix.

I'm not familiar with the Webforms DataGrid, but perhaps if you explained
why you think there should be a control with the ID "fca" in the
DataGridItem someone else could chime in.

Object reference not set to an instance of an object.

Line 143: DataGridItem item = reportGrid.Items;
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top