Find Records Between two Dates in C# and MS Access Database

In this tutorial, you are going to learn how to find records between two dates using C# and MS Access database. With the use of this method, you will be able to search the records you need in the database between two dates. On the other hand, you can filter the records and display it inside the datagridview. There are times that you might encounter this kind of problem while coding, so I hope this can help you solve it.

Creating Application

Step 1

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

Step 2

Make the Form just like shown below. ps2

Step 3

Open the code editor and create a connection between c# and ms access database. After that, initialize all the classes that are needed.
  1.  
  2. OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\person_db.accdb");
  3.         OleDbCommand cmd;
  4.         OleDbDataAdapter da;
  5.         DataTable dt;
  6.         string sql;

Step 4

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

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.  
  2.         private void Form1_Load(object sender, EventArgs e)
  3.         {
  4.             sql = "Select Fullname,u_name as Username, u_type as UserRole,DateJoined From tbluser";
  5.             find_data(sql, dataGridView1);
  6.         }

Step 6

Double click the button and do the following codes for searching data between two dates.
  1.  
  2.         private void button1_Click(object sender, EventArgs e)
  3.         {
  4.             sql = "Select Fullname,u_name as Username, u_type as UserRole,DateJoined From tbluser WHERE DateJoined >= #" + dtpFrom.Value.ToString("MM/dd/yyyy") + "# and  DateJoined >= #" + dtpFrom.Value.ToString("MM/dd/yyyy") + "#";
  5.             find_data(sql, dataGridView1);
  6.         }
Note: Add using System.Data.OleDb; above the namespace to access OleDB libraries. 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.v

Add new comment