How to Create Inclusive Dates Report in C#

This time, I will teach you how to create inclusive dates report in c#. I based this on my last tutorial which is how to create a Daily, Weekly and Monthly Report in C#. When you create a report this program will help you a lot because this will retrieve the previous months transactions. And this can also retrieve the previous weeks and specific dates of transactions. So let's begin:

Creating Database

Create a database named “dbtransaction”. Execute the following query for creating table and adding data in the table that you have created.
  1. CEATE TABLE `tbltransaction` (
  2. `TRANSID` int(11) NOT NULL,
  3. `ORNO` int(30) NOT NULL,
  4. `TRANSDATE` date NOT NULL,
  5. `AMOUNTSALE` double NOT NULL,
  6. `CASHIER` varchar(30) NOT NULL
  7.  
  8. --
  9. -- Dumping data for table `tbltransaction`
  10. --
  11.  
  12. INSERT INTO `tbltransaction` (`TRANSID`, `ORNO`, `TRANSDATE`, `AMOUNTSALE`, `CASHIER`) VALUES
  13. (1, 70004, '2018-12-03', 385, 'Janno Palacios'),
  14. (2, 70005, '2018-12-03', 385, 'Janno Palacios'),
  15. (3, 70002, '2018-12-01', 385, 'Janno Palacios'),
  16. (4, 70001, '2018-12-01', 385, 'Janno Palacios'),
  17. (5, 70006, '2018-12-04', 69, 'Janno Palacios'),
  18. (6, 70007, '2018-12-04', 69, 'Janno Palacios'),
  19. (7, 70003, '2018-12-02', 138, 'Janno Palacios');

Creating Application

Step 1

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

Step 2

Do the form just like shown below. psFROMDates1

Step 3

Add a namespace to access MySQL libraries.
  1. using MySql.Data.MySqlClient;

Step 4

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

Step 5

Create a method for retrieving data in the database.
  1. private void loadData(string sql)
  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_list.DataSource = dt;
  15.  
  16.  
  17. } catch (Exception ex)
  18. {
  19. MessageBox.Show(ex.Message);
  20. }
  21. finally
  22. {
  23. con.Close();
  24. da.Dispose();
  25. }
  26. }

Step 6

Double click the “Search” button and do the following codes for the inclusive dates report.
  1. private void btn_search_Click(object sender, EventArgs e)
  2. {
  3. sql = "SELECT `ORNO`, `TRANSDATE` as 'Date', `AMOUNTSALE` as 'TOTALAMOUNT', `CASHIER` FROM `tbltransaction` WHERE DATE(`TRANSDATE`) BETWEEN '" + dtp_from .Text + "' AND '" + dtp_to .Text + "'";
  4. loadData(sql);
  5. lblTitle.Text = "Report Inclusive Dates from " + dtp_from .Text + " to " + dtp_to .Text ;
  6. }
Note: The database file is included inside the folder. 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.

Add new comment