Accounting Project Part 9 Accounting Financial Year in C#

Hi, Dear's here we learn how to implement stock in Accounting Project  POS in Visual Studio using C#. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like.

In this tutorial we will cover all the below mention topics:
    i)      How to Create Financial Year Form inside AcconntForms Folder in out project visual studio?
    ii)     How to design Financial Year form?
 
    iii)    How to Insert, update, delete and retrieve records from FinancialYearTable and show inside datagridview(dgv)?

So if you want all question answers watch the video, and for source code scroll down.



Design Account Control Form in C# / C-Sharp :
So in this example, we have account control form, show below in figure:



Source Code Account Controls Form : 
using System;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace POSAccounts.Forms.AccountForms
{
    public partial class FrmFinancialYear : Form
    {
        public FrmFinancialYear()
        {
            InitializeComponent();
            PanelBar.MouseDown += PanelBar_MouseDown;
        }

        private const int HT_CAPTION = 0x2; // what does this means
        private const int WM_NCLBUTTONDOWN = 0x00A1; // what does this means
        [DllImport("user32", CharSet = CharSet.Auto)]// what does this means
        private static extern bool ReleaseCapture();
        [DllImport("user32", CharSet = CharSet.Auto)]// what does this means
        private static extern int SendMessage(
        IntPtr hwnd,
        int wMsg,
        int wParam,
        int lParam);
        private void PanelBar_MouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                Rectangle rct = DisplayRectangle;
                if (rct.Contains(e.Location))
                {
                    ReleaseCapture();
                    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
                }
            }
        }


        private void dtpStartDate_ValueChanged(object sender, EventArgs e)
        {
            txtFinancialYear.Text = dtpStartDate.Value.ToString("MMM-yyyy") + " to " + dtpEndDate.Value.ToString("MMM-yyyy");
        }

        private void dtpEndDate_ValueChanged(object sender, EventArgs e)
        {
            txtFinancialYear.Text = dtpStartDate.Value.ToString("MMM-yyyy") + " to " + dtpEndDate.Value.ToString("MMM-yyyy");
        }

        private void FrmFinancialYear_Load(object sender, EventArgs e)
        {
            FillGrid(string.Empty);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close(); // using to close current form
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }

        // Retrive Data From FinancialYearTable using Select Method of Database layer
private void FillGrid(string searchvalue) { try { string query = "Select FinanicalYearID [ID], FinanicalYearTitle [Financial Year],StartDate,EndDate,Status FROM FinancialYearTable"; if (!string.IsNullOrEmpty(searchvalue)) { query = "Select FinanicalYearID [ID], FinanicalYearTitle [Financial Year],StartDate,EndDate,Status FROM FinancialYearTable Where FinanicalYearTitle like '%" + searchvalue.Trim() + "%'"; } DataTable dt = DatabaseLayer.Select(query); dgv.DataSource = dt; if (dt != null) { if (dt.Rows.Count > 0) { dgv.Columns[0].Visible = false; // FinanicalYearID dgv.Columns[1].Width = 120; // FinanicalYearTitle dgv.Columns[2].Width = 100; // StartDate dgv.Columns[3].Width = 100; // EndDate dgv.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; // Status } } } catch { dgv.DataSource = null; } } // Validation Method for More Details Watch Vedio private void EnableComponents() { btnCancel.Visible = true; btnUpdate.Visible = true; btnSave.Visible = false; btnClear.Visible = false; dgv.Enabled = false; txtSearch.Enabled = false; } // Valition Method for More Details Watch Vedio private void ResetComponents() { btnCancel.Visible = false; btnUpdate.Visible = false; btnSave.Visible = true; btnClear.Visible = true; dgv.Enabled = true; txtSearch.Enabled = true; dtpStartDate.Value = DateTime.Now; dtpEndDate.Value = DateTime.Now; txtFinancialYear.Clear(); FillGrid(string.Empty); } private void FormClear() { dtpStartDate.Value = DateTime.Now; dtpEndDate.Value = DateTime.Now; txtFinancialYear.Clear(); txtFinancialYear.Focus(); } private void btnCancel_Click(object sender, EventArgs e) { ResetComponents(); } private void btnClear_Click(object sender, EventArgs e) { FormClear(); } private void txtSearch_TextChanged(object sender, EventArgs e) { FillGrid(txtSearch.Text.Trim()); } private void btnSave_Click(object sender, EventArgs e) { try { ep.Clear(); // First Check Financial year Textbox, if it's is empty then show message to user, plese enter Account Controls Title if (txtFinancialYear.Text.Trim().Length == 0) { ep.SetError(txtFinancialYear, "Please Enter Financial Title!"); txtFinancialYear.Focus(); return; } DateTime startdate = dtpStartDate.Value; DateTime enddate = dtpEndDate.Value; double daysdiff = (enddate- startdate).TotalDays; if (daysdiff < 364) { ep.SetError(dtpStartDate, "Please Set 365 Days defference between Start Date and End Date"); dtpStartDate.Focus(); return; } // Check Account Contorl is already registered or not DataTable existdt = DatabaseLayer.Select("Select * From FinancialYearTable Where FinanicalYearTitle = '" + txtFinancialYear.Text.Trim() + "'"); if (existdt != null) { if (existdt.Rows.Count > 0) { ep.SetError(txtFinancialYear, "Already Exist!"); // if already Registered show this message txtFinancialYear.Focus(); txtFinancialYear.SelectAll(); return; } } // Now use database layer insert method to insert data in database in FinancialYearTable.
string insertquery = string.Format("Insert into FinancialYearTable(FinanicalYearTitle,StartDate,EndDate,Status) values('{0}','{1}','{2}','1')", txtFinancialYear.Text.Trim(), dtpStartDate.Value.ToString("yyyy/MM/dd"), dtpEndDate.Value.ToString("yyyy/MM/dd")); bool result = DatabaseLayer.Insert(insertquery); // use insert method for insertfinancial in FinancialYearTable
if (result == true) { MessageBox.Show("Registered Successfully!"); // if Insert show success message FillGrid(string.Empty); FormClear(); } else { MessageBox.Show("Please Provide Correct Details!"); // if Not Insert show success message } } catch { // incase of any issue is occure show this message MessageBox.Show("Some Unexptected issue is occure plz try agian!"); } } private void editToolStripMenuItem_Click(object sender, EventArgs e) { if (dgv != null) { if (dgv.Rows.Count > 0) { if (dgv.SelectedRows.Count == 1) { if (MessageBox.Show("Are you sure you want to update selected Financial Year?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { dtpStartDate.Value = Convert.ToDateTime(dgv.Rows[dgv.SelectedRows[0].Index].Cells[2].Value.ToString()); dtpEndDate.Value = Convert.ToDateTime(dgv.Rows[dgv.SelectedRows[0].Index].Cells[3].Value.ToString()); txtFinancialYear.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[1].Value.ToString(); EnableComponents(); } } else { MessageBox.Show("Please Select One Record!"); } } } } private void btnUpdate_Click(object sender, EventArgs e) { try { ep.Clear(); // First Check Financial year Title Textbox, if it's is empty then show message to user, please enter Account Controls Title if (txtFinancialYear.Text.Trim().Length == 0) { ep.SetError(txtFinancialYear, "Please Enter Financial Title!"); txtFinancialYear.Focus(); return; } DateTime startdate = dtpStartDate.Value; DateTime enddate = dtpEndDate.Value; double daysdiff = (enddate - startdate).TotalDays; if (daysdiff < 364) { ep.SetError(dtpStartDate, "Please Set 365 Days defference between Start Date and End Date"); dtpStartDate.Focus(); return; } // Check Account Contorl is already registered or not DataTable existdt = DatabaseLayer.Select("Select * From FinancialYearTable Where FinanicalYearTitle = '" + txtFinancialYear.Text.Trim() + "' and FinanicalYearID != '"+ dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value.ToString() + "'"); if (existdt != null) { if (existdt.Rows.Count > 0) { ep.SetError(txtFinancialYear, "Already Exist!"); // if already Registered show this message txtFinancialYear.Focus(); txtFinancialYear.SelectAll(); return; } } // Now use database layer insert method to insert data in database in FinancialYearTable.
string updatequery = string.Format("Update FinancialYearTable set FinanicalYearTitle = '{0}',StartDate = '{1}',EndDate = '{2}' where FinanicalYearID = '{3}'", txtFinancialYear.Text.Trim(), dtpStartDate.Value.ToString("yyyy/MM/dd"), dtpEndDate.Value.ToString("yyyy/MM/dd"), dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value.ToString()); bool result = DatabaseLayer.Update(updatequery); // use update method for update financial year in FinancialYearTable if (result == true) { MessageBox.Show("Registered Successfully!"); // if update show success message FillGrid(string.Empty); ResetComponents(); } else { MessageBox.Show("Please Provide Correct Details!"); // if Not updateshow success message } } catch { // incase of any issue is occure show this message MessageBox.Show("Some Unexptected issue is occure plz try agian!"); } } private void changeStatusToolStripMenuItem_Click(object sender, EventArgs e) { if (dgv != null) { if (dgv.Rows.Count > 0) { if (dgv.SelectedRows.Count == 1) { if (MessageBox.Show("Are you sure you want to change status?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { bool existstatus = Convert.ToBoolean(dgv.Rows[dgv.SelectedRows[0].Index].Cells[4].Value); int financialid = Convert.ToInt32(dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value); bool status = false; if (existstatus == true) { status = false; } else { status = true; } string updatequery = string.Format("update FinancialYearTable set" + " Status = '{0}' where FinanicalYearID = '{1}'", status, financialid); bool result = DatabaseLayer.Update(updatequery); if (result == true) { MessageBox.Show("Status Change Successfully!"); FillGrid(string.Empty); } else { MessageBox.Show("Please Try Again!"); } } } else { MessageBox.Show("Please Select One Record!"); } } else { MessageBox.Show("List is Empty!"); } } else { MessageBox.Show("List is Empty!"); } } } }

Comments