Ejemplo n.º 1
0
        private void btnContDelete_Click(object sender, EventArgs e)
        {
            try
            {
                using (var context = new Classes.CustomerContext())
                {
                    Classes.CustomerContact deleted = contactList[currentContactIndex];
                    var result = (from a in context.CustomerContacts where a.ID ==deleted.ID select a).FirstOrDefault();

                    context.CustomerContacts.Remove(result);
                    context.SaveChanges();
                }
                UpdateContacts(currentCustomerID);
            }
            catch (Exception ex)
            {
                ShowMessage("Error deleting customer contact", ex);
            }
        }
Ejemplo n.º 2
0
        private void btnCustDelete_Click(object sender, EventArgs e)
        {
            try
            {

                //delete customer
                using (var context = new Classes.CustomerContext())
                {
                    //delete customer contacts
                    var contactResult = (from a in context.CustomerContacts where a.CustomerID == currentCustomerID select a).ToList();
                    foreach (var item in contactResult)
                    {
                        context.CustomerContacts.Remove(item);
                        context.SaveChanges();
                    }

                    var result = (from a in context.Customers where a.ID == currentCustomerID select a).FirstOrDefault();
                    context.Customers.Remove(result);
                    context.SaveChanges();
                }
                //refresh the combobox
                RefreshCustomerDatasource();
            }
            catch (Exception ex)
            {
                ShowMessage("Error deleting customer", ex);
            }
        }
Ejemplo n.º 3
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            //if new button is clicked, set current customer id to -1
            //if currentCustomerID != -1 do Update
            //else
            //Handle as new

            if (currentCustomerID == -1)
            {
                #region NewCustomer
                Classes.Customer customer = new Classes.Customer();
                if (txbCustName.Text.Trim() == "")
                {
                    //do nothing

                }
                else
                {
                    customer.Name = txbCustName.Text;

                    decimal convert = 0;
                    if (decimal.TryParse(txbLat.Text, out convert))
                    {
                        customer.Latitude = convert;
                    }
                    else
                    {
                        customer.Latitude = 0;
                    }

                    if (decimal.TryParse(txbLong.Text, out convert))
                    {
                        customer.Longitude = convert;
                    }
                    else
                    {
                        customer.Longitude = 0;
                    }
                    try
                    {
                        using (var context = new Classes.CustomerContext())
                        {
                            context.Customers.Add(customer);
                            context.SaveChanges();
                            lblCustID.Text = customer.ID.ToString();
                            currentCustomerID = customer.ID;
                        }

                    }
                    catch (Exception ex)
                    {
                        ShowMessage("Error saving customer to datasource", ex);
                    }
                }
                #endregion
            }
            else
            {
                #region UpdateCustomer
                if (txbCustName.Text.Trim() == "")
                {
                }
                else
                {
                    try
                    {
                        using (var context = new Classes.CustomerContext())
                        {
                            var result = (from a in context.Customers where a.ID == currentCustomerID select a).FirstOrDefault();
                            if (result != null)
                            {
                                result.Name = txbCustName.Text;
                                decimal tempValue;
                                result.Latitude = decimal.TryParse(txbLat.Text, out tempValue) ? tempValue : 0;
                                result.Longitude = decimal.TryParse(txbLong.Text, out tempValue) ? tempValue : 0;

                                context.SaveChanges();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ShowMessage("Error updating customer data", ex);
                    }
                }
                #endregion
            }

            RefreshCustomerDatasource();
        }
Ejemplo n.º 4
0
        private void btnSaveContact_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentContactIndex == -1) //new button has been clicked
                {
                    if ((txbCustContNum.Text != "") || (txbCustContEmail.Text != ""))
                    {
                        Classes.CustomerContact newContact = new Classes.CustomerContact();
                        newContact.CustomerID = currentCustomerID;
                        newContact.Name = txbCustContName.Text;
                        newContact.Email = txbCustContEmail.Text;

                        newContact.ContactNumber = txbCustContNum.Text;

                        using (var context = new Classes.CustomerContext())
                        {
                            context.CustomerContacts.Add(newContact);
                            context.SaveChanges();
                            currentContactIndex = newContact.ID;
                        }
                        UpdateContacts(currentCustomerID);
                    }
                    else
                    {
                        //empty fields. HAndle error and ignore save
                    }

                }
                else
                {
                    using (var context = new Classes.CustomerContext())
                    {
                        Classes.CustomerContact update = contactList[currentContactIndex];
                        update.Name = txbCustContName.Text;
                        update.Email = txbCustContEmail.Text;
                        update.ContactNumber = txbCustContNum.Text;
                    }
                }
            }
            catch (Exception ex)
            {
                ShowMessage("Error saving customer contact", ex);
            }
        }