3

I have a basic foreach loop that calls a static method which makes a connection to a database and inserts some data. For some reason it will only iterate through the first item in the collection when I run the application without debugging. If I debug the application and set a break point on the foreach loop, it will iterate through all of the items in the collection.

If I set a break point and step over the foreach loop, it will demonstarte the same behavior as if I was running the application without debugging.

Does anyone know what would cause this type of behavior?

Here is a simplified version of the source code:

List<MyObject> objectlist = new List<MyObject>();

//some code to populate list

foreach(MyObject myobject in objectlist)
{
    string a = "a";
    string b = "b";

    MyLibrary.UpdateDatabase(a, b);
}

(I am using Visual Studio 2008 SP1)

Update

The process does not throw any exceptions with or without debugging the application.

5
  • 14
    Please post a small, complete example that demonstrates the problem. Commented Sep 22, 2009 at 19:57
  • 4
    This may be off completely but could it be you need a clean and there is a different build of the assemblies in the debug bin than in the application bin Commented Sep 22, 2009 at 20:03
  • 1
    Are you using multiple threads? Commented Sep 22, 2009 at 20:06
  • make sure you don't have a try..catch surrounding the foreach that might be eating the exceptions? Commented Sep 22, 2009 at 20:20
  • @Aaron Daniels - I am not using multiple threads Commented Sep 22, 2009 at 21:35

5 Answers 5

4

My guess would be that your code might be behaving differently when you give it more time by stepping through each line. (Presumably because of the database)

Make sure that the method isn't throwing any exceptions (put a catch block that calls Console.WriteLine or MessageBox.Show and see if anything happens).

Look in the database logs and see if there's anything interesting there.

Also, please post the full source of the method.

Sign up to request clarification or add additional context in comments.

3 Comments

Turning debug break on Exceptions is great for spotting these kind of things- msdn.microsoft.com/en-us/library/d14azbfh.aspx
Yes, but he says it mainly happens when not running under the debugger, so that's not enough.
I was referring to the "Make sure that the method isn't throwing any exceptions" as a comment- I've not posted an answer...
2

Normally when there is a difference between code running normally and code running in debug, it is related to the security context.

Code running in a process will run in the security context of that process. Code running in debug mode will run in the security context of the user doing the debugging.

The call to the database probably fails when the code is run normally, due to lack of rights. It would then appear that the loop only runs once.

Comments

1

It was not iterating through the foreach loop when I was not debugging the application because the myobject object was not used in the UpdateDatabase method call.

My source code should look like this:

List<MyObject> objectlist = new List<MyObject>();

//some code to populate list

foreach(MyObject myobject in objectlist)
{
    MyLibrary.UpdateDatabase(myobject.a, myobject.b);
}

2 Comments

Please update your original question when you have clarification (that's how SO works ;))
Wouldn't this answer been enough clarification?
0

For me it sounds like an exception. Just to be sure, did you checked everything in Debug - Exceptions to On?

Comments

0

I had the same problem. (The foreach loop was returning the first item...) I simply solved it by changing the 'foreach' loop by a 'for' loop and used the [i] to check every index in the list (that forced the loop to go through the list by every item)..

In my case, I had a list of objects of type Boat. (with boatType, cost, remark. etc) And I wanted to add only new boatTypes that don't exist.

this is the original code that didn't work:

//data access layer
DALInventory dalGear = new DALInventory();

public bool AddBoat(Boat boat)
{
     foreach (var item in AllBoats())
                {
                    if (item.BoatType == gear.BoatType)
                    {
                        //if boatType already exists
                        return false;
                    }
                }
                //if boatType doesn't exist
                dalGear.AddBoat(gear);
                return true;
}

This is the code (for loop) that solved my issue:

for (int i = 0; i < AllBoats().Count; i++)
                {
                    Boat b = new Boat();

                    b.BoatType = AllBoats()[i].BoatType;

                    if (b.BoatType == gear.BoatType)
                    {
                        return false;
                    }
                }

                dalGear.AddBoat(gear);
                return true;

I still don't understand why the first code (foreach loop) doesn't work, even though it seems to be right !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.