private void Add() { try { string item = txtItem.Text.Trim(); if (!String.IsNullOrEmpty(item)) { using (MyDayData db = new MyDayData()) { var query = from ts in db.Projects where ts.ProjectCode == item select new { Project = ts.ProjectCode }; if (query.FirstOrDefault() == null) { Project newItem = new Project { ProjectCode = item }; db.AddToProjects(newItem); db.SaveChanges(); this.SetStatus("Item was added successfully"); this.PopulateList(); txtItem.Text = ""; } } } } catch (Exception ex) { this.SetStatus(ex); this.PopulateList(); } finally { txtItem.Focus(); } }
private void Save() { try { using (MyDayData db = new MyDayData()) { string projectCode = txtProject.Text.Trim(); string taskCode = txtTask.Text.Trim(); string taskTypeCode = txtTaskType.Text.Trim(); string taskStatusCode = txtTaskStatus.Text.Trim(); decimal? estEff = null; string estEffStr = txtEstEffort.Text.Trim(); if (!String.IsNullOrEmpty(estEffStr)) estEff = Convert.ToDecimal(estEffStr); string action = txtAction.Text.Trim(); DateTime actionStartTime = dtpActionDate.Value.Date.Add(dtpActionTimeFrom.Value.Subtract(dtpActionTimeFrom.Value.Date)); DateTime actionEndTime = dtpActionDate.Value.Date.Add(dtpActionTimeTo.Value.Subtract(dtpActionTimeTo.Value.Date)); string actionTypeCode = txtActionType.Text.Trim(); Dictionary<string, string> actionTags = tpTags.GetTags(); if (pbNewProject.Visible) { Project newProject = new Project(); newProject.ProjectCode = projectCode; db.AddToProjects(newProject); } if (pbNewTaskType.Visible) { TaskType newTaskType = new TaskType(); newTaskType.TaskTypeCode = taskTypeCode; db.AddToTaskTypes(newTaskType); } if (pbNewTaskStatus.Visible) { TaskStatus newTaskStatus = new TaskStatus(); newTaskStatus.TaskStatusCode = taskStatusCode; db.AddToTaskStatuses(newTaskStatus); } if (pbNewActionType.Visible) { ActionType newActionType = new ActionType(); newActionType.ActionTypeCode = actionTypeCode; db.AddToActionTypes(newActionType); } if (pbNewTask.Visible) { Task newTask = new Task(); newTask.ProjectCode = projectCode; newTask.TaskCode = taskCode; newTask.TaskTypeCode = taskTypeCode; newTask.TaskStatusCode = taskStatusCode; newTask.EstimatedEffortHours = estEff; db.AddToTasks(newTask); } else { var query = from t in db.Tasks where t.ProjectCode.Equals(projectCode, StringComparison.InvariantCultureIgnoreCase) && t.TaskCode.Equals(taskCode, StringComparison.InvariantCultureIgnoreCase) select t; Task currentTask = query.First(); currentTask.TaskTypeCode = taskTypeCode; currentTask.TaskStatusCode = taskStatusCode; currentTask.EstimatedEffortHours = estEff; } MyDay.Data.Action actionEnt = null; if (lblActionCode.Text == "(New)") { actionEnt = new MyDay.Data.Action(); actionEnt.ProjectCode = projectCode; actionEnt.TaskCode = taskCode; actionEnt.ActionTypeCode = actionTypeCode; actionEnt.FromTime = actionStartTime; actionEnt.ToTime = actionEndTime; actionEnt.ActionComments = action; db.AddToActions(actionEnt); } else { long actionCode = Convert.ToInt64(lblActionCode.Text); var query = from t in db.Actions where t.ActionCode == actionCode select t; actionEnt = query.First(); actionEnt.ActionTypeCode = actionTypeCode; actionEnt.FromTime = actionStartTime; actionEnt.ToTime = actionEndTime; actionEnt.ActionComments = action; } using (var txn = db.BeginTransaction()) { db.SaveChanges(); var query = from t in db.ActionTags where t.ActionCode == actionEnt.ActionCode select t; foreach (var row in query) db.DeleteObject(row); foreach (KeyValuePair<string, string> tag in actionTags) { if (tag.Value.EndsWith("(New)") && !tag.Key.Equals(tag.Value)) { MyDay.Data.Tag newTag = new Tag(); newTag.TagCode = tag.Key; db.AddToTags(newTag); } MyDay.Data.ActionTag actionTagEnt = new ActionTag(); actionTagEnt.ActionCode = actionEnt.ActionCode; actionTagEnt.TagCode = tag.Key; db.AddToActionTags(actionTagEnt); } db.SaveChanges(); txn.Commit(); } } this.ResetControls(true); this.SetStatus("The specified Action was saved successfully"); } catch (Exception ex) { this.SetStatus(ex); } }