How to Create Daily, Weekly and Monthly Report in C#

In this tutorial, I will teach you how to create Daily, Weekly and Monthly Report in C#. This method has the ability to generate report that would be display in the DataGridView. This is one of the most important function in a system because it is where the user or admin will find out the transaction every month.So, let's begin.

Creating Database

Create a database named “dbtransaction”. Execute the following query for creating table and adding data in the table.
  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#. psReport1

Step 2

Do the form just like shown below. psReprt2

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 “Daily Report” radio button and do the following codes for the daily report.
  1.         private void rdo_daily_CheckedChanged(object sender, EventArgs e)
  2.         {
  3.             sql = "SELECT `ORNO`, `TRANSDATE` as 'Date', `AMOUNTSALE` as 'TOTALAMOUNT', `CASHIER` FROM `tbltransaction` WHERE DATE(`TRANSDATE`) =CURDATE()";
  4.             loadData(sql);
  5.         }

Step 7

Double click the “Weekly Report” radio button and do the following codes for the weekly report.
  1.         private void rdo_weekly_CheckedChanged(object sender, EventArgs e)
  2.         {
  3.             sql = "SELECT `ORNO`, `TRANSDATE` as 'Date', `AMOUNTSALE` as 'TOTALAMOUNT', `CASHIER` FROM `tbltransaction` WHERE WEEK(`TRANSDATE`) =WEEK(NOW())";
  4.             loadData(sql);
  5.         }

Step 8

Double click the “Monthly Report” radio button and do the following codes for the Monthly report.
  1.         private void rdo_monthly_CheckedChanged(object sender, EventArgs e)
  2.         {
  3.             sql = "SELECT `ORNO`, `TRANSDATE` as 'Date', `AMOUNTSALE` as 'TOTALAMOUNT', `CASHIER` FROM `tbltransaction` WHERE MONTH(`TRANSDATE`) =MONTH(NOW())";
  4.             loadData(sql);
  5.         }
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