How to Search Data Using DateTimePicker in C#

One of the biggest problems in programming is how to deal with the dates. So, In this tutorial, I will teach you how to search for data using DateTimePicker in C#. This project has the ability to search the data in the database by the dates you choose in the DateTimePicker. The data will automatically display into the datagridview when the date you choose is equal to the transaction date in the database.

Creating a Database

Go to http://localhost/phpmyadmin/ and create a new database named it "salesdb". Execute the following codes for creating a table.
  1. CREATE TABLE `tblsales` (
  2.   `SalesId` int(11) NOT NULL,
  3.   `TRANSDATE` date NOT NULL,
  4.   `Product` varchar(90) NOT NULL,
  5.   `TotalSales` double NOT NULL
Execute the following codes for inserting data in the table.
  1. INSERT INTO `tblsales` (`SalesId`, `TRANSDATE`, `Product`, `TotalSales`) VALUES
  2. (1, '2018-01-30', 'Cellphone', 1400),
  3. (2, '2018-02-28', 'Laptop', 800),
  4. (3, '2018-03-31', 'Desktop', 5052),
  5. (4, '2019-04-30', 'Ipod', 8030),
  6. (5, '2019-05-31', 'Tablet', 10000);

Creating Application

Step 1

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

Step 2

Do the form just like shown below. figure 2

Step 3

Add MySQL.Data.dll

Step 4

Press F7 to open the code editor. In the code editor, add a namespace to access MySQLlibraries
  1.  
  2. using MySql.Data.MySqlClient;

Step 5

Establish a connection between C# and MySQL database. After that, declare all the classes that are needed.
  1.  
  2.         MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=salesdb;sslMode=none");
  3.         MySqlDataAdapter da;
  4.         MySqlCommand cmd;
  5.         DataTable dt;
  6.         string sql;

Step 6

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

Step 7

Write the following code 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.  
  5.             sql = "SELECT * FROM `tblsales`";
  6.             retrieveData(sql, dataGridView1);
  7.         }

Step 8

Write the following code for searching data in the DataGridView using the DateTimePicker when the button is clicked
  1.  
  2.         private void button1_Click(object sender, EventArgs e)
  3.         {
  4.  
  5.             DateTime dateToday = dateTimePicker1.Value;
  6.  
  7.             string strDate = dateToday.ToString("yyyy-MM-dd");
  8.              
  9.             sql = "SELECT * FROM `tblsales` WHERE Date(`TRANSDATE`) = '" + strDate + "'";
  10.             retrieveData(sql, dataGridView1);
  11.         }
The complete source code is included you can download it and run it on your computer. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Comments

Add new comment