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;
}
}