Looping within a Loop

  • Thread starter Thread starter Ryan Ternier
  • Start date Start date
R

Ryan Ternier

I'm having an issue with an inner loop.

Here's the basic Code:

For each......{

//Do code here

//start other loop
For each....{
...}


}

Everything works 100% when I step through the project, however when I break
during the inner loop, and either:
1)Encounter an error
-or-
2)Stop the debugger

It will continue with all iterations of the 1st loop... and not touch the
2nd loop anymore. meaning running through 1000+ rows in a datatable and add
them to an online DB.

Has anyone dealt with an issue like this? I'm working on sensitive
information and this issue is causing big problems.

Thanks!

/RT
 
....so the inner stop condition has been met...but what does that look like?
show more code please
 
Hi ,

With out further code it's impossible to know what is going on.

My guess is that the inner condition is not dependand of the outer loop.

Cheers,
 
Here's the First Loop:


foreach(DataRow row in tblItem.Rows)
{
//Get the FormID
int intFormID = 0;

//Get the FormID, then increment it by 1
strSQL = "SELECT MAX(FormID) from tblItems";
comItem.CommandText = strSQL;
intFormID = ToSQLValidInt(comItem.ExecuteScalar().ToString());
intFormID++;

//Add the item.
strSQL = strStart + "Values(" + row["SourceID"] + "," +
row["OwnerID"] + ",'" + row["ItemDate"] + "','" + row["DueDate"];
strSQL += "'," + row["StatusID"] + ",'" + row["StatusDate"] + "'," +
row["FollowUpID"] + "," + row["ModifiedBy"];
strSQL += ",'" + row["DateModified"] + "'," + row["GoalID"] + "," +
row["SubjectID"] + "," + row["GeographicAreaID"];
strSQL += "," + ToSQLValidBit(row["IsActive"]) + ",'" +
row["Description"] + "'," + row["CreatedBy"] + ",'" +
row["DateCreated"];
strSQL += "'," + intFormID + ")";
comItem.CommandText = strSQL;
comItem.ExecuteNonQuery();

//get the ID of the new item added to the DB.
strSQL = "SELECT IDENT_CURRENT('tblITems')";
comItem.CommandText = strSQL;
intItemID = Int32.Parse(comItem.ExecuteScalar().ToString());
intOldItemID = Int32.Parse(row["ItemID"].ToString());


//Update the Current Item, to change the Type of it, depending on
what is filled in with the V1 table.
strSQL = "UPDATE tblItems SET TypeID = ";
//Find out what Type the item is. Resolution or Complaint
if(row["Resolution"].ToString() != "")
{
strSQL += "1 WHERE ItemID = " + intItemID;
comItem.CommandText = strSQL;
comItem.ExecuteNonQuery();
ResolutionItemRecords(comItem,row,intItemID);
}
else if (row["ConcernDescription"].ToString() != "")
{
strSQL += "3 WHERE ItemID = " + intItemID;
comItem.CommandText = strSQL;
comItem.ExecuteNonQuery();
ComplaintItemRecords(comItem,row,intItemID);
}

/*What tables need to be done for Each Item:
* tblTasks, tblAmendments, tblAgainst, tblItemCategories,
tblItemAttachments */
ItemAmendmentConversion(tblItem, intItemID, intOldItemID);
ItemAttachmentConversion(tblItem, intItemID, intOldItemID);

TaskConversion(tblItem,intItemID, intOldItemID);



}//foreach
}

------------
And here's the 2nd loop (TaskConversion) don't worry about the Amendment
/ Attachment Conversions
------------

private void TaskConversion(DataTable tblItem, int intItemID, int
intOldItemID)
{
/*Purpose: When an item is created, all tasks that are associated
with that item must
* be coppied over, keeping the itemID the same as that
previously added.
*
* Tables That are related to tblTasks: tblItems, tblStaff,
tblFollowUps(v2)
*/
//Create the Tasks Table.
DataTable tblTasks = new DataTable("tblTasks");
tblTasks = CreateTable("tblTasks", strDBV1, tblTasks);


SqlConnection conItem = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Up
dateConnection"]);
conItem.ConnectionString += strDBV2;
int intTaskID = 0;
SqlCommand comItem = new SqlCommand();

comItem.Connection = conItem;
comItem.Connection.Open();

//After the items have been transfered over, it's time for the tasks.
string strSQL = "";
string strStart = "";

//ItemID, and IsActive must be created using functions.
strStart = "INSERT INTO tblTasks(ItemID, OrderID, OwnerID, TaskDate,
Description, PriorityID, DueDate, StatusID, StatusDate, " +
"ModifiedBy, DateMOdified, CreatedBy, DateCreated, isActive)";

/*Loop through each row within the Tasks Table.
* If the ItemID in the row = the intItemID passed into this function
* then go ahead and add this data to the DB
*/
try
{
foreach(DataRow row in tblTasks.Rows)
{
//Loop through each row, but we can only add tasks that are part of
the current item.
//intOldItemID - the ItemID will be replaced with the new ItemID.
if(Int32.Parse(row["ItemID"].ToString()) == intOldItemID)
{
int intOrderID = 0;

//Insert the NewData into the tasks table.
strSQL = strStart + "Values(" + intItemID + "," + row["OrderID"]
+ "," + row["StaffResponsible"] + ",'" + row["TaskDate"] + "','" +
row["Description"];
strSQL += "'," + row["PriorityID"] + ",'" + row["DueDate"] + "',"
+ row["StatusID"] + ",'" + row["StatusDate"];
strSQL += "'," + row["ModifiedBy"] + ",'" + row["DateMOdified"] +
"'," + row["CreatedBy"] + ",'" + row["DateCreated"] + "',1)";
comItem.CommandText = strSQL;
comItem.ExecuteNonQuery();

//Get the current task ID.
strSQL = "SELECT IDENT_CURRENT('tblTasks')";
comItem.CommandText = strSQL;
intTaskID = Int32.Parse(comItem.ExecuteScalar().ToString());

//For each Task these are the tables that need to be edited:
// tblDependecies, tblDependencies, tblTaskAttachments
}
}
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
conItem.Close();
conItem = null;
comItem = null;
}

}
 
Back
Top