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

Submitted by: 
Language: 
Visitors have accessed this post 628 times.

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.