Beispiel #1
0
        private void saveBtn_Click(object sender, EventArgs e)
        {
            using (var db = new AccountingSystem())
            {
                string ErrorMessage = "";
                if (ItemCode.Text.Equals(""))
                {
                    ErrorMessage = "الرجاء ادخال الكود";
                }
                else if (ItemName.Text.Equals(""))
                {
                    ErrorMessage = "الرجاء ادخال الأسم";
                }
                else if (ItemExpireDate.Text.Equals(""))
                {
                    ErrorMessage = "الرجاء ادخال تاريخ الانتهاء";
                }
                else if (TypeBox.SelectedItem.Equals("-اختر-"))
                {
                    ErrorMessage = " الرجاء اختيار النوع";
                }
                else if (comboBoxStore.EditValue == null)
                {
                    ErrorMessage = " الرجاء اختيار المخزن";
                }
                else if (ItemGroup.EditValue == null)
                {
                    ErrorMessage = " الرجاء اختيار المجموعة";
                }

                if (ErrorMessage.Equals(""))
                {
                    try
                    {
                        if (!isEditForm)
                        {
                            addItem.Name            = ItemName.Text;
                            addItem.Code            = ItemCode.Text;
                            addItem.Type            = TypeBox.SelectedItem.ToString();
                            addItem.ExpireDate      = DateTime.Now;
                            addItem.Status          = ItemStatus.Checked;
                            addItem.WharehouseId    = int.Parse(comboBoxStore.EditValue.ToString());
                            addItem.ItemGroupsId    = int.Parse(ItemGroup.EditValue.ToString());
                            addItem.Discription     = ItemDiscritption.Text;
                            addItem.SupplierId      = (ItemSupplireId.EditValue == null) ? 0 : int.Parse(ItemSupplireId.EditValue.ToString());
                            addItem.VendorId        = (ItemVendorId.EditValue == null) ? 0 : int.Parse(ItemVendorId.EditValue.ToString());
                            addItem.ExpireAlarmDays = (ItemExpireAlarm.Text.Equals("")) ? 0 : int.Parse(ItemExpireAlarm.Text);
                            addItem.FreezDate       = (ItemFreezDays.Text.Equals("")) ? 0 : int.Parse(ItemFreezDays.Text);
                            addItem.MonthlySale     = (ItemMonthlySale.Text.Equals("")) ? 0 : int.Parse(ItemMonthlySale.Text);
                            addItem.MinQouantity    = (ItemMinQuant.Text.Equals("")) ? 0 : int.Parse(ItemMinQuant.Text);

                            dbContextItems.Items.Add(addItem);
                        }
                        dbContextItems.SaveChanges();
                        dbContextDetails.SaveChanges();
                        if (!isEditForm)
                        {
                            ItemName.Text         = "";
                            TypeBox.SelectedIndex = 0;
                            comboBoxStore.DeselectAll();
                            ItemGroup.DeselectAll();
                            ItemExpireDate.Text = "";
                            DetailGrid.RefreshDataSource();
                            ItemDiscritption.Text = "";
                            ItemMinQuant.Text     = "";
                            ItemMonthlySale.Text  = "";
                            ItemFreezDays.Text    = "";
                            ItemCode.Text         = (int.Parse(ItemCode.Text) + 1) + "";
                            Id = (int.Parse(dbContextItems.Items.Select(d => d.Id).Max().ToString()) + 1) + "";
                            DetGridView.SelectAll();
                            DetGridView.DeleteSelectedRows();
                            return;
                        }
                    }
                    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                    {
                        Exception raise = dbEx;
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                string message = string.Format("{0}:{1}",
                                                               validationErrors.Entry.Entity.ToString(),
                                                               validationError.ErrorMessage);
                                raise = new InvalidOperationException(message, raise);
                            }
                        }
                        throw raise;
                    }
                    catch (Exception dbEx)
                    {
                        Exception raise = dbEx;
                        var       val   = dbEx.InnerException;
                        MessageBox.Show(val.ToString());
                        //throw raise;
                    }
                }
                else
                {
                    MessageBox.Show(ErrorMessage);
                }
            }
        }