Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
1.6 kB
1
Indexable
Never
 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}");
     }
 }
Leave a Comment