/// <summary> /// The Submit button was clicked. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_addNewArea_Click(object sender, EventArgs e) { //If the linklabel which led us to this form was for adding a new value for the Area master table. if (clickedLinkName.Contains("Area")) { //Validation if (textBox_addNewMaster.Text.Length == 0 ) { MessageBox.Show("Please enter a value.", "Error"); return; } int textValue; if (Int32.TryParse(textBox_addNewMaster.Text.Trim(), out textValue)) { MessageBox.Show("Please enter a valid value.", "Error"); return; } if (db.Areas.Select(x => x.AreaName).Contains(textBox_addNewMaster.Text)) { MessageBox.Show("This value already exists.", "Error"); return; } int areaId = 0; if (db.Areas.Count() != 0) areaId = db.Areas.Select(x => x.AreaId).Max(); //Validation was successful. Area newArea = new Area(); newArea.AreaName = textBox_addNewMaster.Text; newArea.AreaId = areaId + 1; db.Areas.AddObject(newArea); } //If the linklabel which led us to this form was for adding a new value for the MealPlan master table. if (clickedLinkName.Contains("MealPlan")) { //Validation if (textBox_addNewMaster.Text.Length == 0 ) { MessageBox.Show("Please enter a value.", "Error"); return; } int textValue=0; if (!Int32.TryParse(textBox_addNewMaster.Text.Trim(), out textValue)) { MessageBox.Show("Please enter a valid number.", "Error"); return; } if (Int32.Parse(textBox_addNewMaster.Text) <= 0) { MessageBox.Show("Please enter a positive value for Meal Plan.", "Error"); return; } if (db.MealPlans.Select(x => x.MealAmount).Contains(Int32.Parse(textBox_addNewMaster.Text))) { MessageBox.Show("This Meal Plan already exists.", "Error"); return; } int lastMealPlanId = 0; if (db.MealPlans.Count() != 0) lastMealPlanId = db.MealPlans.Select(x => x.MealPlanId).Max(); //Validation was successful. MealPlan newMealPlan = new MealPlan(); newMealPlan.MealAmount = Int32.Parse(textBox_addNewMaster.Text); newMealPlan.MealPlanId = lastMealPlanId + 1; db.MealPlans.AddObject(newMealPlan); } db.SaveChanges(); MessageBox.Show("Added Successfully.", "Success"); this.Close(); //close the window. }
/// <summary> /// Deprecated Method for adding a new object to the MealPlans EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToMealPlans(MealPlan mealPlan) { base.AddObject("MealPlans", mealPlan); }
/// <summary> /// To add a new MealPlan /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_addMealPlan_Click(object sender, EventArgs e) { //Performing validation. if (textBox_mealPlan.Text.Length == 0 && textBox_editMealPlan.Text.Length == 0) { MessageBox.Show("Please enter a valid Value", "Error"); return; } if (textBox_editMealPlan.Text.Trim().Length != 0) { MealPlan mealPlan = comboBox_mealPlans.SelectedItem as MealPlan; if (db.MealPlans.Select(x => x.MealAmount).Contains(Int32.Parse(textBox_editMealPlan.Text))) { MessageBox.Show("Already Exists.", "Error"); return; } db.MealPlans.Where(x => x.MealAmount == mealPlan.MealAmount).Single().MealAmount = Int32.Parse(textBox_editMealPlan.Text); db.SaveChanges(); //Refresh the Area combo-box. CommonUtilities.populateMealPlans(comboBox_mealPlans); MessageBox.Show("Updated Successfully", "Success"); textBox_editMealPlan.Text = ""; return; } int textValue = 0; if (!Int32.TryParse(textBox_mealPlan.Text.Trim(), out textValue)) { MessageBox.Show("Not a Valid Number", "Error"); return; } if (Int32.Parse(textBox_mealPlan.Text) <= 0) { MessageBox.Show("Cant Be Zero or A Negative Number.", "Error"); return; } if (db.MealPlans.Select(x => x.MealAmount).Contains(Int32.Parse(textBox_mealPlan.Text))) { MessageBox.Show("Already Exists.", "Error"); return; } //Validation succeeded. if (textBox_mealPlan.Text.Trim().Length != 0) { int lastMealPlanId = 0; if (db.MealPlans.Count() != 0) lastMealPlanId = db.MealPlans.Select(x => x.MealPlanId).Max(); MealPlan newMealPlan = new MealPlan(); newMealPlan.MealPlanId = lastMealPlanId + 1; newMealPlan.MealAmount = textBox_mealPlan.Text == "" ? 0 : Int32.Parse(textBox_mealPlan.Text); if (newMealPlan.MealAmount == 0) { MessageBox.Show("Cannot be 0", "Error"); return; } db.MealPlans.AddObject(newMealPlan); db.SaveChanges(); MessageBox.Show("Added Successfully", "Success"); //Refresh the MealPlan combo-box. CommonUtilities.populateMealPlans(comboBox_mealPlans); textBox_mealPlan.Text = ""; } }
/// <summary> /// Create a new MealPlan object. /// </summary> /// <param name="mealPlanId">Initial value of the MealPlanId property.</param> /// <param name="mealAmount">Initial value of the MealAmount property.</param> public static MealPlan CreateMealPlan(global::System.Int32 mealPlanId, global::System.Int32 mealAmount) { MealPlan mealPlan = new MealPlan(); mealPlan.MealPlanId = mealPlanId; mealPlan.MealAmount = mealAmount; return mealPlan; }