//Method that adds new details row to the QuoteDetail ****ADD DETAILS BUTTON*** public bool CreateNewQuoteDetail(QuoteDetailToCreateDTO QuoteDetailNew) { bool check = false; using (TransactionScope transaction = new TransactionScope()) { try { QuoteDetail QuoteDetailIn = new QuoteDetail(); //Data transfer to the object that will be added to the DB QuoteDetailIn.Text = QuoteDetailNew.Text; QuoteDetailIn.Value = QuoteDetailNew.Value; using (var context = new RFQEntities()) { //Finding references for the Foreign Keys Quote existingQuote = context.Quotes.Single(qd => qd.QuoteID == QuoteDetailNew.QuoteID); //Adding refernces QuoteDetailIn.Quote = existingQuote; if (QuoteDetailIn.EntityState == EntityState.Detached) { context.QuoteDetails.AddObject(QuoteDetailIn); } context.SaveChanges(); } } catch (Exception e) { transaction.Dispose(); check = false; return check; } transaction.Complete(); check = true; return check; } }
private void buttonCreate_Click(object sender, EventArgs e) { try { ApplicationServices appService = new ApplicationServices(); bool emptyText = appService.EmptyTextValidation(this); appService.EmptyTextMark(this); if (!emptyText) throw new EmptyText(); DateTime StartDate = new DateTime(); DateTime EndDate = new DateTime(); string sStartDate = textBoxStartDate.Text; string sEndDate = textBoxEndDate.Text; bool startDateValid = DateTime.TryParse(sStartDate, out StartDate); bool endDateValid = DateTime.TryParse(sEndDate, out EndDate); if (!startDateValid) { textBoxStartDate.BackColor = Color.Red; throw new InvalidDateFormat(); } if (!endDateValid) { textBoxEndDate.BackColor = Color.Red; throw new InvalidDateFormat(); } if (StartDate > EndDate) { throw new DateChronolgy(); } QuoteDTO QuoteIn = new QuoteDTO(); QuoteIn.CompanyID = companyIDIn; QuoteIn.StartDate = StartDate; QuoteIn.EndDate = EndDate; IQuote QuoteFunctions = new QuoteService(); QuoteAdded QuoteValidation = QuoteFunctions.CreateNewQuoation(QuoteIn); if (QuoteValidation.Added == true) { QuoteDetailToCreateDTO DetailToCreate = new QuoteDetailToCreateDTO(); DetailToCreate.QuoteID = QuoteValidation.LastId; DetailToCreate.Text = textBoxDescription.Text; string sValue = textBoxQuantity.Text; decimal dValue; bool isParsed = decimal.TryParse(sValue, out dValue); if (!isParsed) { throw new QuantityWrong(); } DetailToCreate.Value = dValue; bool check = QuoteFunctions.CreateNewQuoteDetail(DetailToCreate); if (check) { MessageBox.Show("Quote was created. ", "app", MessageBoxButtons.OK); this.Close(); instanceMainForm.LoadData(); } else { throw new QuoteFailed(); } } else { throw new QuoteFailed(); } } catch (EmptyText et) { MessageBox.Show("Empty Text, all fields must be filled out.", "app", MessageBoxButtons.OK); } catch (InvalidDateFormat idf) { MessageBox.Show("Date format is invalid. Date format is: 'yyyy/mm/dd' ", "app", MessageBoxButtons.OK); } catch (QuoteFailed qf) { MessageBox.Show("Quote creation failed. Please check your data. ", "app", MessageBoxButtons.OK); } catch (DateChronolgy dc) { MessageBox.Show("Dates are not choronological. ", "app", MessageBoxButtons.OK); } catch(QuantityWrong qw) { MessageBox.Show("Quanity should be decimal. ", "app", MessageBoxButtons.OK); } }