How to Search Data Between Two Dates Using C#

This time, I will teach you how to search data between two dates Using C#. This method has the ability to find all the records in the database between two inclusive dates. In this way, you can filter the data to be displayed in the datagridview. Hope this tutorial will help you to solve your problem in filtering data between two dates. See the procedure below.

Creating Database

Create a database named “db_payroll”. Write this query to create a table and add the data in the table.
  1. CREATE TABLE `tblpayroll` (
  2. `ID` int(11) NOT NULL,
  3. `EmployeeName` varchar(90) NOT NULL,
  4. `Department` varchar(90) NOT NULL,
  5. `Salary` double NOT NULL,
  6. `PayrollDate` date NOT NULL
  7.  
  8. --
  9. -- Dumping data for table `tblpayroll`
  10. --
  11.  
  12. INSERT INTO `tblpayroll` (`ID`, `EmployeeName`, `Department`, `Salary`, `PayrollDate`) VALUES
  13. (1, 'Janobe Palacios', 'IT Department', 12000, '2018-10-31'),
  14. (2, 'Jeanniebe Nillos', 'Accounting Department', 10000, '2018-10-31'),
  15. (3, 'Janobe Palacios', 'IT Department', 15000, '2018-11-30'),
  16. (4, 'Jeanniebe Nillos', 'Accounting Department', 12000, '2018-11-30'),
  17. (5, 'Janobe Palacios', 'IT Department', 9000, '2018-09-30'),
  18. (6, 'Jeanniebe Nillos', 'Accounting Department', 8000, '2018-09-30');

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#. psDates1

Step 2

Do the Form just like show below. psDates2

Step 3

Open the code editor and create a connection between c# and mysql database. After that, initialize all the classes that are needed.
  1. MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=db_payroll;sslMode=none;");
  2. MySqlCommand cmd = new MySqlCommand();
  3. MySqlDataAdapter da = new MySqlDataAdapter();
  4. DataTable dt;

Step 4

Create a method for retrieving data in the database.
  1. private void loadData(string sql,DataGridView dtg)
  2. {
  3. try
  4. {
  5. con.Open();
  6. cmd = new MySqlCommand();
  7. cmd.Connection = con;
  8. cmd.CommandText = sql;
  9. da = new MySqlDataAdapter();
  10. da.SelectCommand = cmd;
  11. dt = new DataTable();
  12. da.Fill(dt);
  13.  
  14. dtg.DataSource = dt;
  15.  
  16. }
  17. catch (Exception ex)
  18. {
  19. MessageBox.Show(ex.Message);
  20.  
  21. }
  22. finally
  23. {
  24. con.Close();
  25. da.Dispose();
  26. }
  27. }

Step 5

Double click the form and call the method that you have created to display the data into the datagridview in the first load of the form.
  1. string sql;
  2. sql = "SELECT * FROM tblpayroll";
  3. loadData(sql, dataGridView1);

Step 6

Double click the button and do the following codes for searching data between two dates.
  1. string sql;
  2. DateTime from = dateTimePicker1.Value;
  3. DateTime to = dateTimePicker2.Value;
  4.  
  5. sql = "SELECT * FROM tblpayroll WHERE DATE(PayrollDate) BETWEEN '" + from.ToString("yyyy-MM-dd") + "' AND '" + to.ToString("yyyy-MM-dd") + "'";
  6. loadData(sql, dataGridView1);
Note: First, add MySQL.Data.dll for the references. Second, add this using MySql.Data.MySqlClient; above the namespace to access MySQL library. The complete source code is included. You can download it and run it on your computer. For more question about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT FB Account – https://www.facebook.com/onnaj.soicalap Or feel free to comment below.

Add new comment