private void frmStart_Load(object sender, EventArgs e) { intFoodstuffCount = DataConnection.NumFoodstuffs(); intRecipeMaterialCount = DataConnection.NumRecipeMaterials(); }
private void btnSaveRecipe_Click(object sender, EventArgs e) { /* * Here's what this is needing to do: * Validate the various fields on the form: * Name must exist * Rest are optional but if they do exist: * PriceSold is a double * ServingSize, PrepTime, CookTime are integers * Check if entries in the ingredients list (which are matched to foodstuffs) exist * in the foodstuff master list. * If they do exist, get their IDs and generate Recipe items to link this new item to them. * If they do NOT exist, prompt user whether to make generic entries for them * (i.e. ID and Name, rest being null) * Then make Recipe entries from these new foodstuffs we just made. * Once that's done, we can push to database...I think... * -Dustin */ if (blnValidate()) //really this is the level you'd do validation at { DialogResult button = MessageBox.Show("Are you sure you want to save this data?", "Save Recipe", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (button == DialogResult.Yes) { //they clicked yes, go about adding it //generate the id string newID = txtName.Text.Substring(0, Math.Min(txtName.Text.Split(' ')[0].Length, 4));//grab the first up to 4 characters of the name //get the item count and add one. newID += (DataConnection.NumFoodstuffs() + 1).ToString("0000#"); List <string> newTags = new List <String>(); if (lbxTags.Items.Count > 0) { for (int i = 0; i < lbxTags.Items.Count; i++) { newTags.Add(lbxTags.Items[i].ToString()); } } //temp fix for blank textboxes so the foodstuff constructor doesn't error out with a blank string if (txtPrepTime.Text == "") { //convert to a double txtPrepTime.Text = "-1"; } if (txtCookTime.Text == "") { txtCookTime.Text = "-1"; } if (txtServingSize.Text == "") { txtServingSize.Text = "-1"; } if (txtPriceSold.Text == "") { txtPriceSold.Text = "-1.0"; } List <Recipe> newItemIngredients = new List <Recipe>(); //if lsvIngredients is empty...atomic ingredient? Don't hand the list to the constructor. if (lsvIngredients.Items.Count > 0) { foreach (ListViewItem lvi in lsvIngredients.Items) { //check to see whether the item exists or not by matching name, which is the third column of the listview. if (DataConnection.FindFoodstuffsNamed(lvi.SubItems[2].Text).Count > 0) { //this is supposed to return true if a name match is found //and add it to the new food's ingredient list newItemIngredients.Add(new Recipe(newID, (DataConnection.FindFoodstuffsNamed(lvi.SubItems[2].Text)[0].ID), lvi.SubItems[0].Text, lvi.SubItems[1].Text)); } else { //Did not find a match... //making dummy entries //grab the first up to 4 characters of the name string newPHID = lvi.SubItems[2].Text.Substring(0, Math.Min(lvi.SubItems[2].Text.Split(' ')[0].Length, 4)); newPHID += (DataConnection.NumFoodstuffs() + 1).ToString("0000#"); //new placeholder ID, name from listview's name column FoodStuff fsPlaceholder = new FoodStuff(newPHID, lvi.SubItems[2].Text); //should automatically generate the self-referencing RecipeMaterial stub... DataConnection.AddFoodStuff(fsPlaceholder); //then put this in the list for the actual food we're going to add newItemIngredients.Add(new Recipe(newID, newPHID, lvi.SubItems[0].Text, lvi.SubItems[1].Text)); } } } //build the new foodstuff we're adding FoodStuff newFS = new FoodStuff(newID, txtName.Text, txtDirections.Text, Convert.ToInt32(txtPrepTime.Text), Convert.ToInt32(txtCookTime.Text), Convert.ToDouble(txtPriceSold.Text), Convert.ToInt32(txtServingSize.Text), newTags, newItemIngredients); //send to database DataConnection.AddFoodStuff(newFS); } //Then Clear the form and show a message stating the info was saved lblSaved.Text = "Recipe was saved. Please enter new recipe"; var cntrlCollections = GetAll(this, typeof(TextBox)); foreach (Control ctrl in cntrlCollections) { if (ctrl is TextBox) { ctrl.Text = string.Empty; } } lsvIngredients.Items.Clear(); lbxTags.Items.Clear(); timer1.Start(); } else { MessageBox.Show(ErrMessage); } // FoodStuff fs = new FoodStuff(txtName.Text, // if (blnValid = true) //{ // Add stuff into food stuff class and into database //} { } }