private void LoadPublishersFromDatabase()
        {
            using (var db = new FreddansBokhandelContext())
            {
                if (db.Database.CanConnect())
                {
                    publishers = db.Förlag.Include(b=>b.Books).ToList();
                }

                else
                {
                    MessageBox.Show("Kunde inte koppla upp mot databasen.");
                }

                db.Dispose();
            }
        }
        private void LoadAuthorsFromDatabase()
        {
            using (var db = new FreddansBokhandelContext())
            {
                if (db.Database.CanConnect())
                {
                    authors = db.Författare.Include(b => b.BooksAuthors)
                              .ThenInclude(i => i.IsbnNavigation)
                              .ToList();
                }
                else
                {
                    MessageBox.Show("Kunde inte koppla upp mot databasen.");
                }

                db.Dispose();
            }
        }
예제 #3
0
        private void AddOrEditEmployee()
        {
            var store = comboBoxStores.SelectedItem as Store;

            if (CheckIfEmployeeCanBeAdded() == false)
            {
                MessageBox.Show("Ett eller flera fält är tomma eller i fel format. Försök igen."); return;
            }


            using (var db = new FreddansBokhandelContext())
            {
                if (db.Database.CanConnect())
                {
                    var newEmployee = new Employee
                    {
                        Id           = SetEmployeeID(),
                        FirstName    = textBoxFirstName.Text.Trim(),
                        LastName     = textBoxLastName.Text.Trim(),
                        DateOfBirth  = dateTimePicker1.Value,
                        Address      = textBoxAddress.Text.Trim(),
                        ZipCode      = textBoxZipCode.Text.Trim(),
                        PostalAdress = textBoxPostAddress.Text.Trim(),
                        HireDate     = dateTimePicker2.Value,
                        Email        = textBoxEmail.Text.Trim(),
                        Telephone    = textBoxTelephone.Text.Trim(),
                        Store        = store.Id,
                        Role         = comboBoxRole.SelectedItem.ToString()
                    };

                    if (selectedEmployee == null)
                    {
                        db.Add(newEmployee);
                    }
                    else
                    {
                        db.Update(newEmployee);
                    }

                    db.SaveChanges();
                    Close();
                }
            }
        }
        private void AddorEditPublishers()
        {
            if (DoesPublisherExists() == false && selectedPublisher == null)
            {
                MessageBox.Show("Författaren finns redan i systemet."); return;
            }
            if (CheckIfNameIsEmpty() == false)
            {
                MessageBox.Show("Namnfältet är tomt. Försök igen."); return;
            }

            using (var db = new FreddansBokhandelContext())
            {
                if (db.Database.CanConnect())
                {
                    var newPublisher = new Publisher
                    {
                        Id            = SetPublisherID(),
                        Name          = textBoxName.Text.Trim(),
                        Address       = textBoxAddress.Text.Trim(),
                        ZipCode       = textBoxZipCode.Text.Trim(),
                        PostalAddress = textBoxPostalAdress.Text.Trim(),
                        Country       = textBoxCountry.Text.Trim()
                    };

                    if (selectedPublisher == null)
                    {
                        db.Add(newPublisher);
                    }
                    else
                    {
                        db.Update(newPublisher);
                    }

                    db.SaveChanges();
                    Close();
                }
            }
        }
        private async Task LoadOrdersFromDatabaseAsync()
        {
            db = new FreddansBokhandelContext();

            try
            {
                if (db.Database.CanConnect())
                {
                    orders = await db.Ordrar
                             .Include(o => o.OrderDetails)
                             .Include(a => a.EmployeeIDs)
                             .Include(b => b.Stores)
                             .ThenInclude(ls => ls.StockBalance)
                             .ThenInclude(i => i.IsbnNavigation)
                             .OrderBy(b => b.OrderDate)
                             .ToListAsync();

                    orderDates.Clear();

                    foreach (var date in orders)
                    {
                        orderDates.Add(date.OrderDate);
                    }

                    orderDates = orderDates.Distinct().ToList();
                }
                else
                {
                    MessageBox.Show("Kunde inte ladda in från databasen.");
                }
            }
            catch
            {
                return;
            }
        }