Ejemplo n.º 1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(validFields())
            {
                if (txtID.Text.Length > 0) //Alter
                {
                    try
                    {
                        using (context = new Teste_OnibusContext())
                        {
                            var id = int.Parse(txtID.Text);

                            STATION station = context.STATIONS.FirstOrDefault(x => x.Station_ID == id);

                            station.Station_Description = txtDescription.Text;

                            context.SaveChanges();

                            if (loadGridViewData(context))
                            {
                                lblMessage.Text = "Updated successfully!";
                                lblMessage.ForeColor = Color.Green;
                                enableFields(false);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Methods.DisplayMessage(lblMessage, "It was not possible to update the data", Color.Red);
                    }
                }
                //else // Add
                //{
                //    STATION station = new STATION();

                //    station = txtDescription.Text;

                //    using (context = new Teste_OnibusContext())
                //    {
                //        context.STATION.Add(station);

                //        context.SaveChanges();

                //        if (loadGridViewData(context))
                //        {
                //            lblMessage.Text = "Add successfully!";
                //            lblMessage.ForeColor = Color.Green;
                //            enableFields(false);
                //        }
                //    }
                //}
            }
            else
            {
                Methods.DisplayMessage(lblMessage, "Check the input data", Color.Red);
            }
            resetFields();
        }
Ejemplo n.º 2
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (validFields())
            {
                if (txtID.Text.Length > 0) //Alter
                {
                    try
                    {
                        using (context = new Teste_OnibusContext())
                        {
                            var id = int.Parse(txtID.Text);
                            LANDMARK_KNOWN_AS landmark = context.LANDMARK_KNOWN_AS.FirstOrDefault(x => x.Known_As_ID == id);

                            landmark.Known_As_Description = txtKnown.Text;

                            context.SaveChanges();

                            if (loadGridViewData(context))
                            {
                                lblMessage.Text = "Updated successfully!";
                                lblMessage.ForeColor = Color.Green;
                                enableFields(false);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Methods.DisplayMessage(lblMessage, "It was not possible to update the data", Color.Red);
                    }
                }
            }
            else
            {
                Methods.DisplayMessage(lblMessage, "Check the input data", Color.Red);
            }
            resetFields();
        }
Ejemplo n.º 3
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (validFields())
            {
                if (txtID.Text.Length > 0) //Alter
                {
                    try
                    {
                        using (context = new Teste_OnibusContext())
                        {
                            var id = int.Parse(txtID.Text);
                            BUS bus = context.BUS.FirstOrDefault(x => x.Bus_ID == id);

                            bus.Bus_Description = txtDescription.Text;
                            bus.Bus_Provider = txtProvider.Text;
                            bus.Bus_Color = txtColor.Text;

                            context.SaveChanges();

                            if (loadGridViewData(context))
                            {
                                lblMessage.Text = "Updated successfully!";
                                lblMessage.ForeColor = Color.Green;
                                enableFields(false);
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        Methods.DisplayMessage(lblMessage, "It was not possible to update the data", Color.Red);
                    }
                }
                else // Add
                {
                    BUS bus = new BUS();

                    bus.Bus_Description = txtDescription.Text;
                    bus.Bus_Provider = txtProvider.Text;
                    bus.Bus_Color = txtColor.Text;

                    using (context = new Teste_OnibusContext())
                    {
                        context.BUS.Add(bus);

                        context.SaveChanges();

                        if (loadGridViewData(context))
                        {
                            lblMessage.Text = "Add successfully!";
                            lblMessage.ForeColor = Color.Green;
                            enableFields(false);
                        }
                    }
                }
            }
            else
            {
                Methods.DisplayMessage(lblMessage, "Check the input data", Color.Red);
            }
            resetFields();
        }
Ejemplo n.º 4
0
        private void dgvBus_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            lblMessage.Text = string.Empty;
            if (MessageBox.Show("When performing this operation, the route of this bus will also be deleted. Are you sure you want to continue?", "Confirmation", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    using (context = new Teste_OnibusContext())
                    {
                        var id = int.Parse(e.Row.Cells["ID"].Value.ToString());

                        BUS bus = context.BUS.FirstOrDefault(x => x.Bus_ID == id);

                        ROUTE route = context.ROUTEs.FirstOrDefault(x => x.Bus_ID == id);

                        List<STATION_BUSES> station_bus = context.STATION_BUSES.Where(x => x.Buses_FK == id).ToList();

                        foreach (var item in station_bus)
                        {
                            context.STATION_BUSES.Remove(item);
                        }

                        context.ROUTEs.Remove(route);
                        context.BUS.Remove(bus);

                        context.SaveChanges();
                    }

                    Methods.DisplayMessage(lblMessage, "Deleted successfully!", Color.Green);
                }
                catch (Exception ex)
                {
                    Methods.DisplayMessage(lblMessage, "Error while deleting", Color.Red);
                }
                enableFields(false);
            }
            else
                e.Cancel = true;
        }
Ejemplo n.º 5
0
        private void dgvReferences_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            lblMessage.Text = string.Empty;
            if (MessageBox.Show("When performing this operation, all related landmarks will also be deleted. Are you sure you want to continue?", "Confirmation", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    using (context = new Teste_OnibusContext())
                    {
                        var id = int.Parse(e.Row.Cells["Known_As_ID"].Value.ToString());

                        LANDMARK_KNOWN_AS landmark = context.LANDMARK_KNOWN_AS.FirstOrDefault(x => x.Known_As_ID == id);

                        var parentLandmark = landmark.LANDMARK;
                        var relatedLandmarks = parentLandmark.LANDMARK_KNOWN_AS.ToList();

                        foreach (var item in relatedLandmarks)
                        {
                            context.LANDMARK_KNOWN_AS.Remove(item);
                        }

                        context.LANDMARK_KNOWN_AS.Remove(landmark);
                        context.LANDMARKs.Remove(parentLandmark);

                        context.SaveChanges();

                        if (loadGridViewData(context))
                        {
                            Methods.DisplayMessage(lblMessage, "Deleted successfully!", Color.Green);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Methods.DisplayMessage(lblMessage, "Error while deleting", Color.Red);
                }
                enableFields(false);
            }
            else
                e.Cancel = true;
        }