Example #1
0
        private async void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                await using var dbContext = new BurgerContext();
                await dbContext.Database.EnsureCreatedAsync();

                if (!string.IsNullOrEmpty(txtName.Text) && !string.IsNullOrEmpty(txtPrice.Text) &&
                    !string.IsNullOrEmpty(txtOrder.Text))
                {
                    Order order;
                    await dbContext.Orders.AddAsync(
                        order = new Order()
                    {
                        Description = txtOrder.Text
                    });

                    await dbContext.Burgers.AddAsync(
                        new Burger()
                    {
                        Name = txtName.Text, Price = Convert.ToDouble(txtPrice.Text), Order = order
                    });

                    await dbContext.SaveChangesAsync();

                    MessageBox.Show(@"Data is saved", @"INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(@"Something went wrong", @"INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                txtName.Clear();
                txtPrice.Clear();
                txtOrder.Clear();
            }
        }
Example #2
0
        private async void btnRead_Click(object sender, EventArgs e)
        {
            await using var dbContext = new BurgerContext();

            var dtb = new DataTable();
            var dto = new DataTable();

            dtb.Columns.Add(new DataColumn("name", typeof(string)));
            dtb.Columns.Add(new DataColumn("price", typeof(double)));
            dtb.Columns.Add(new DataColumn("order", typeof(string)));

            dto.Columns.Add(new DataColumn("order_description", typeof(string)));

            try
            {
                //TODO: Make Column "order" filed up with data from Orders context
                dbContext.Orders.ToList().ForEach(o =>
                {
                    dto.Rows.Add(o.Description);
                });

                dbContext.Burgers.ToList().ForEach(b =>
                {
                    dtb.Rows.Add(b.Name, b.Price, b.Order);
                });

                dgvDataBurger.DataSource = dtb;
                dgvDataOrder.DataSource  = dto;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                txtName.Clear();
                txtPrice.Clear();
                txtOrder.Clear();
            }
        }