Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
13 kB
1
Indexable
Never
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FinalExam_Aguila
{
    public partial class Form1 : Form
    {
        private string ConnString = @"SERVER=localhost;DATABASE=booking;UID=root;PWD=;";

        public Form1()
        {
            InitializeComponent();
            //COLORS MOUSE

            //NewBooking button
            button3.MouseEnter += btnNewBooking_MouseEnter;
            button3.MouseLeave += btnNewBooking_MouseLeave;
            //Button Delete
            btnDel.MouseEnter += btnDelete_MouseEnter;
            btnDel.MouseLeave += btnDelete_MouseLeave;
            //Button Refresh
            btnRefresh.MouseEnter += btnRefresh_MouseEnter;
            btnRefresh.MouseLeave += btnRefresh_MouseLeave;



        }

        public int code = 0;

        private MySqlConnection ConnectToDatabase()
        {
            MySqlConnection Conn = new MySqlConnection(ConnString);
            try
            {
                Conn.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection Error: " + ex.Message);
            }
            return Conn;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadBooks();
           
        }

        private void LoadBooks()
        {
            MySqlConnection Conn = ConnectToDatabase();
            string sSQL = "SELECT Code_num, EventType, EventName, Customer, PhoneNum, Date_event, TIME_FORMAT(Time_event, '%h:%i %p') AS Time, Status_event FROM eventlog";
            DataTable dt = new DataTable();

            try
            {
                MySqlCommand cmd = new MySqlCommand(sSQL, Conn);
                MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                adapter.Fill(dt);
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading books: " + ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e) //SEARCH BUTTON
        {
            FilterBookingData();
        }

        private void FilterBookingData()
        {
            string eventType = cbEventType.SelectedItem?.ToString();
            DateTime dateFrom = dateTimePicker1.Value.Date;
            DateTime dateTo = dateTimePicker2.Value.Date.AddDays(1);

            try
            {
                MySqlConnection Conn = ConnectToDatabase();
                string sSQL = "SELECT * FROM eventlog WHERE Date_event >= @dateFrom AND Date_event < @dateTo";
                DataTable dt = new DataTable();

                if (!string.IsNullOrEmpty(eventType))
                {
                    sSQL += " AND EventType = @eventType";
                }
                else
                {
                    MessageBox.Show("Event Type is Empty", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Question);

                    return; // Stop further execution when EventType is empty
                }

                using (MySqlCommand cmd = new MySqlCommand(sSQL, Conn))
                {
                    cmd.Parameters.AddWithValue("@dateFrom", dateFrom);
                    cmd.Parameters.AddWithValue("@dateTo", dateTo);
                    if (!string.IsNullOrEmpty(eventType))
                    {
                        cmd.Parameters.AddWithValue("@eventType", eventType);
                    }

                    MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                    adapter.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
            catch (MySqlException ex)
            {
                MessageBox.Show($"Database Error: {ex.Message}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred: {ex.Message}");
            }
        }


        private void btnRefresh_MouseEnter(object sender, EventArgs e)
        {
            btnRefresh.BackColor = Color.LightBlue; // Change background color to red
            btnRefresh.ForeColor = Color.White; // Change text color to white
        }


        private void btnRefresh_MouseLeave(object sender, EventArgs e)
        {
            btnRefresh.BackColor = Color.FromArgb(192, 192, 255); // Revert background color to default
            btnRefresh.ForeColor = SystemColors.ControlText; // Revert text color to default
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshBookingData();
        }

        private void RefreshBookingData()
        {
            try
            {
                MySqlConnection Conn = ConnectToDatabase();
                string sSQL = "SELECT Code_num, EventType, EventName, Customer, PhoneNum, Date_event, TIME_FORMAT(Time_event, '%h:%i %p') AS Time, Status_event FROM eventlog";
                DataTable dt = new DataTable();

                try
                {
                    MySqlCommand cmd = new MySqlCommand(sSQL, Conn);
                    MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                    adapter.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error loading books: " + ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                }
            }
            catch (MySqlException ex)
            {
                MessageBox.Show($"Database Error: {ex.Message}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred: {ex.Message}");
            }
        }
        private void btnNewBooking_MouseEnter(object sender, EventArgs e)
        {
            button3.BackColor = Color.LightGreen; // Change background color
            button3.ForeColor = Color.White; // Change text color
        }

        private void btnNewBooking_MouseLeave(object sender, EventArgs e)
        {
            button3.BackColor = Color.FromArgb(192, 255, 192); // Revert background color
            button3.ForeColor = SystemColors.ControlText; // Revert text color
        }

        private void button3_Click(object sender, EventArgs e) //NEW BOOKING BUTTON
        {
            Form2 form = new Form2();
            form.Show();
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //Zzz
        }
        private void btnDelete_MouseEnter(object sender, EventArgs e)
        {
            btnDel.BackColor = Color.FromArgb(210, 10, 5); // Change background color to red
            btnDel.ForeColor = Color.White; // Change text color to white
        }


        private void btnDelete_MouseLeave(object sender, EventArgs e)
        {
            btnDel.BackColor = Color.FromArgb(255, 192, 192); // Revert background color to default
            btnDel.ForeColor = SystemColors.ControlText; // Revert text color to default
        }

        private void btnDel_Click(object sender, EventArgs e)
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    DialogResult result = MessageBox.Show(
                        "Are you sure you want to delete this booking?",
                        "Confirm Deletion",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Warning);

                    if (result == DialogResult.Yes)
                    {
                        try
                        {
                            // Find the index of the column with the name "Code_num"
                            int columnIndex = -1;
                            foreach (DataGridViewColumn column in dataGridView1.Columns)
                            {
                                if (column.Name == "Code_num")
                                {
                                    columnIndex = column.Index;
                                    break;
                                }
                            }

                            if (columnIndex == -1)
                            {
                                MessageBox.Show("Column 'Code_num' not found in DataGridView.");
                                return;
                            }

                            // Retrieve the value from the selected row using the column index
                            string codeToDelete = dataGridView1.SelectedRows[0].Cells[columnIndex].Value.ToString();

                            // Construct the DELETE SQL query using the correct column name
                            string deleteSql = $"DELETE FROM eventlog WHERE Code_num = '{codeToDelete}'";

                            using (MySqlConnection conn = new MySqlConnection(ConnString))
                            {
                                conn.Open();
                                MySqlCommand cmd = new MySqlCommand(deleteSql, conn);
                                int rowsAffected = cmd.ExecuteNonQuery();
                                if (rowsAffected > 0)
                                {
                                    MessageBox.Show("Booking deleted successfully!");
                                    RefreshBookingData();
                                }
                                else
                                {
                                    MessageBox.Show("Failed to delete booking. Please try again.");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show($"An error occurred during deletion: {ex.Message}");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please select a booking to delete.");
                }
            }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int index = dataGridView1.SelectedRows[0].Index;
                string event_ID = dataGridView1.Rows[index].Cells[0].Value.ToString();  

                // Retrieve data from the selected row
                string Code_num = dataGridView1.SelectedRows[0].Cells["Code_num"].Value.ToString();
                string EventType = dataGridView1.SelectedRows[0].Cells["EventType"].Value.ToString();
                string EventName = dataGridView1.SelectedRows[0].Cells["EventName"].Value.ToString();
                string Customer = dataGridView1.SelectedRows[0].Cells["Customer"].Value.ToString();
                string PhoneNum = dataGridView1.SelectedRows[0].Cells["PhoneNum"].Value.ToString(); 
                string Time_event = dataGridView1.SelectedRows[0].Cells["Time"].Value.ToString();
                string EventDate = dataGridView1.SelectedRows[0].Cells["DateTime"].Value.ToString();
                string Status_event = dataGridView1.SelectedRows[0].Cells["Status_event"].Value.ToString();
   
               


                // Pass the data to the form for editing
                Form3 form = new Form3(Code_num, EventType, EventName, Customer, PhoneNum, Time_event, event_ID, Status_event);

                form.txtCustomer.Text = Customer;
                form.cbEventType.Text = EventType;
                form.txtEventName.Text = EventName;
                form.cbStatus.Text = Status_event;
                form.EventTime.Text = Time_event;
                form.EventDate.Text = EventDate;
                form.txtGuest.Text = PhoneNum;
                
                
               
                
                
                form.ShowDialog(); // Show the form as a dialog

                // After editing in the form, refresh the DataGridView to reflect changes
                RefreshBookingData();
            }
            else
            {
                MessageBox.Show("Please select a row to update.");
            }
        }

        private void btnClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
Leave a Comment