Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 20th, 2005, 08:01 AM
Registered User
Join Date: Dec 2005
Location: , , .
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?


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


   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


Reply With Quote
  #2 (permalink)  
Old December 20th, 2005, 08:05 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts

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....


Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #3 (permalink)  
Old December 20th, 2005, 10:03 AM
Registered User
Join Date: Dec 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

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??)


Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 06:41 AM.

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