Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2005, 07:02 PM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# Datagrid Help foreach statement checkboxes

Hi All,

Basically i have the following code. When the foreach statement is finished and the method continues, at the end of the method it re-enters the foreach statement ( which i don't want to happen as i have all the data i need ) The error i am getting is 'Object reference not set to an instance of an object'. However, more importantly how do i stop the code stepping into the foreach statement for a second time? Thanks, mbge9pjb

public void btnSave_Click(object sender, System.EventArgs e)


{

if ( Page.IsValid)


{

bool newState = false;


foreach(DataGridItem dgi in EditCalendar.Items)

{
for ( int i =0; i <checkedMonthOfYear.Length; i++)
{
CheckBox chkEfaPeak = (CheckBox) dgi.FindControl(checkedMonthOfYear[i]);
newState = chkEfaPeak.Checked;
DataSet years = getDataSetFromSessionYears();
years.Tables[0].Rows[ dgi.DataSetIndex][months[i]]= (newState ? "Y": "N");
EditCalendar.EditItemIndex = -1;
}

}

//some nmore code here but ultimately i think the problem is with the above sample. Once the nested loop has finished and continues with the method, it returns back to the 'foreach' statement which i don't want to happen

 
Old December 15th, 2005, 12:32 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

At what point is it going back to the foreach?

-Peter
 
Old December 20th, 2005, 07:29 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter,

Here is the full code of the method. Basically it always hit's the catch statement i have thrown. It exits the foreach statement then goes upto "calendar.saveCalendar ....." At this point it should go to the finally statement but it returns to the foreach statement. Any ideas why this might be happening?

Cheers
mbge9pjb

public void btnSave_Click(object sender, System.EventArgs e)

{

try
{
   foreach(DataGridItem dgi in EditCalendar.Items)
    {

    for ( int i =0; i< checkedMonthOfYear.Length; i++)
    {
    CheckBox chkEfaPeak = (CheckBox) dgi.FindControl(checkedMonthOfYear[i]);
    bool newState = chkEfaPeak.Checked;
    DataSet years = getDataSetFromSessionYears();
    years.Tables[0].Rows[dgi.DataSetIndex][months[i]]= (newState ? "Y": "N");
    }

   }
Int32 idCalendar = Int32.Parse.lstCalendars.Items[ lstCalendars.SelectedIndex].Value);
Int32 idCalendarVersion = Int32.Parse( hiddenCalendarVersionId.Value);
Int16 retcode;
DataSet changes = getDataSetFromSessionYears().GetChanges( DataRowState.Modified);
Calendar calendar = new Calendar( Configuration.getConnStr());
calendar.saveCalendar idCalendar, idCalendarVersion, SecurityManager.getUserID(),changes, out retcode);
}

catch (NullReferenceException )
{
 // makes sure the method catches datagrid referencing null objects
 Response.Redirect("../configuration/Peak_Scaling.aspx");
}

  finally
  {
  setDataSetToSessionYears(null);
  }
}

 
Old December 20th, 2005, 07:55 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry, mistake in the last description, it gets to the finally statement and then goes back to the foreach loop

 
Old December 20th, 2005, 09:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

The execution returning to the loop once it had entered finally?
Just check your program flow once again. I see a high possibility of
btnSave_Click getting called twice. What you r seeing as a
'return to foreach' may just be the second execution of the function.

Just put a break point on the name of the function or put a trace at
the beginning of the function before try block and debug it to see
if the function is getting called twice.

Regards
Ankur Verma





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select all checkboxes in a datagrid kawal.singh C# 2 May 7th, 2007 02:09 PM
problem with foreach loop in Checkboxes datagrid mbge9pjb .NET Framework 2.0 2 December 20th, 2005 10:03 AM
Using Checkboxes in datagrid ractim General .NET 2 September 21st, 2004 05:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.