473,387 Members | 1,532 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.

Problem Iterating Through A Datagrid

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
Nov 16 '05 #1
3 1110
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" wrote:
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

Nov 16 '05 #2
I did as you suggested. However, the problem persists.

Many Thanks

"Ganesh" wrote:
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" wrote:
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

Nov 16 '05 #3
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.

"kscdavefl" <ks*******@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
<snip>
Object reference not set to an instance of an object.
<snip>
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

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
9
by: Ed | last post by:
Is there any known issue with the use of "Scripting.FileSystemObject"? Here's a snippet of my code to obtain the filesize of a file Set fso = CreateObject("Scripting.FileSystemObject") Set...
3
by: Billy Jacobs | last post by:
I have created a DataGridColumnDatePicker Component so that I can put a datetimepicker control in my datagrid. It almost works. When I put my mouse in the cell it changes to a datetimepicker...
5
by: Jeff | last post by:
IDE: VS 2003 :NET OS: XP Pro My app have a form with a tab-control on it. The tab-control have 2 tabpages. One of the tabpages displays a datagrid, and the other tabpage displays details (order...
1
by: Dave Bailey via DotNetMonster.com | last post by:
I need to access the information in only one column of a datagrid. The column is column. When iterating through the grid I want to find all instances where the entry is "" and change it to read...
14
by: Jacko | last post by:
Hi guys, Say I made a SELECT statement to my sql DB that would return 50 rows that I will use a sqldatareader to access. Instead of iterating through each and every row of the datareader, I'd...
1
by: Tommy | last post by:
I have a nested repeater. It displays the data correctly, etc.. I have a problem however.. when i iterate through the textboxes in my code all of the values of the text property are empty. I need...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
15
RMWChaos
by: RMWChaos | last post by:
In my ongoing effort to produce shorter, more efficient code, I have created a "chicken and egg" / "catch-22" problem. I can think of several ways to fix this, none of them elegant. I want my code...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.