Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 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 20th, 2005, 08:01 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with foreach loop in Checkboxes datagrid

Hi All,

Below is the full code of a method which calls a saving stored procedure.Before doing this a foreach method with a nested loop for each column searches the datagrid for checked and uncheked boxes ( which it does successfully). The problem is 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 then goes 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, 08:05 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi mbge9pjb,

You may need to pass true as an additional parameter to Response.Redirect() to tell it stop executing the current page.

Alternatively, you can add the keyword throw in your catch statement to pass up the error....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old December 20th, 2005, 10:03 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think ultimately the best approach will either be a) to find out why exactly the foreach statement is being called again, or b) to introduce a statement in the finally clause to exit the method ( safe return perhaps??)

Thanks
mbge9pjb






Similar Threads
Thread Thread Starter Forum Replies Last Post
Does foreach loop support datareader deb_kareng C# 1 February 23rd, 2007 05:30 AM
ForEach Loop Bushido121 BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 0 October 23rd, 2006 07:55 PM
C# Datagrid Help foreach statement checkboxes mbge9pjb C# 4 December 20th, 2005 09:43 AM
Need help with foreach loop Arsi C# 3 September 7th, 2004 09:41 PM
foreach loop not working rajanikrishna Beginning PHP 1 November 3rd, 2003 04:14 AM





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