Accounting Project Part 5 Customer and Supplier Forms 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 CustomerForms and SupplierForms Folder's inside Forms Folder in visual studio?
    ii)     How to Create Customer Forms inside customerforms and and Supplier Forms in supplierforms folder in our project?
    iii)    How to design supplier form and customer form?
    iv)    How to Insert, update, delete and retrieve records from customer table and show inside datagridview?
    v)     How to Insert, update, delete and retrieve records from supplier table and show inside datagridview?

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

Design Supplier Form in C# / C-Sharp :
So in this example, we have supplier form, show below in figure:


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

namespace POSAccounts.Forms.SupplierForms
{
    public partial class FrmSupplier : Form
    {
        public FrmSupplier()
        {
            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 btnClose_Click(object sender, EventArgs e)
        {
            this.Close(); // Close current Form or context
        }

        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 Supplier(it's table) using Select Method of Database layer
        private void FillGrid(string searchvalue)
        {
            try
            {
                // Retirve All Supplier List
                string query = "Select SupplierID [ID],  SupplierName [Supplier], ContactNo [Contact No], Email [Email], Address,Description FROM SupplierTable";
                if (!string.IsNullOrEmpty(searchvalue))
                { 
                    //Retrive on Condition Base means fultering
                    query = "Select SupplierID [ID],  SupplierName [Supplier], ContactNo [Contact No], Email [Email], Address,Description FROM SupplierTable WHERE (SupplierName + ' ' + ContactNo + ' ' + Email + ' ' +Address ) like '%" + searchvalue.Trim() + "%'";
                }

                DataTable dt = DatabaseLayer.Select(query);
                dgv.DataSource = dt;
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        dgv.Columns[0].Width = 80; // SupplierID
                        dgv.Columns[1].Width = 120; // SupplierName
                        dgv.Columns[2].Width = 100; // ContactNo
                        dgv.Columns[3].Width = 140; // Email
                        dgv.Columns[4].Width = 300; // Address
                        dgv.Columns[5].Width = 400; // Description
                    }
                }
            }
            catch
            {
                dgv.DataSource = null;
            }
        }
        // Valition Method for 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 to reset or cancel updation for details watch vedio
        private void ResetComponents()
        {
            btnCancel.Visible = false;
            btnUpdate.Visible = false;
            btnSave.Visible = true;
            btnClear.Visible = true;
            dgv.Enabled = true;
            txtSearch.Enabled = true;
            FillGrid(string.Empty);
            ClearForm();
        }
        // Clear Form Method for details watch vedio
        private void ClearForm()
        {
            txtName.Clear();
            txtDescription.Clear();
            txtEmailAddress.Clear();
            txtAddress.Clear();
            txtContactNo.Clear();
            txtSearch.Clear();
        }



        private void txtSearch_TextChanged(object sender, EventArgs e)
        { 
            // Filter Search Key
            FillGrid(txtSearch.Text.Trim());
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //Cancel Updation
            ResetComponents();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                ep.Clear();
                if (txtName.Text.Trim().Length == 0) // Check if Supplier Name is Empty or null then show message please enter Supplier Name
                {
                    ep.SetError(txtName, "Please Enter Supplier Title!");
                    txtName.Focus();
                    return;
                }

                if (txtContactNo.Text.Trim().Length < 10) // Check if Supplier Contact No is Empty, null or incorrect then show message please enter Supplier Correct Contact No
                {
                    ep.SetError(txtContactNo, "Please Enter Supplier Contact No!");
                    txtContactNo.Focus();
                    return;
                }

                if (txtAddress.Text.Trim().Length  == 0) // Check if Address is Empty or null then show message please enter address
                {
                    ep.SetError(txtAddress, "Please Enter Supplier Address!");
                    txtAddress.Focus();
                    return;
                }

                // Check Supplier is Already exist in supplier table or not
                DataTable dt = DatabaseLayer.Select("Select * From SupplierTable Where SupplierName = '" + txtName.Text.Trim() + "' and ContactNo = '" + txtContactNo.Text.Trim() + "' and SupplierID != '"+dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value+"'");
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        ep.SetError(txtName, "Already Exist!");
                        txtName.Focus();
                        txtName.SelectAll();
                        return;
                    }
                }

                // Update Query for Supplier details
                string updatequery = string.Format("update SupplierTable set SupplierName = '{0}',ContactNo = '{1}',Email = '{2}',Address = '{3}',Description = '{4}' Where SupplierID = '{5}'"
                    , txtName.Text.Trim(), txtContactNo.Text.Trim(), txtEmailAddress.Text.Trim(), txtAddress.Text.Trim(), txtDescription.Text.Trim(), dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value);
                bool result = DatabaseLayer.Update(updatequery);  // Use update method of database layer to add new supplier in supplier table
                if (result == true)
                {
                    MessageBox.Show("Updated Successfully!");  // Show Successfully Message
                    ResetComponents();
                }
                else
                {
                    MessageBox.Show("Please Provide Correct Details!"); // if any issue occure related to query, show this message
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); // if occure any exception like unexpected issue, show actual message
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearForm(); // Clear Form
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ep.Clear();
                if (txtName.Text.Trim().Length == 0) // Check if Supplier Name is Empty or null then show message please enter Supplier Name
                {
                    ep.SetError(txtName, "Please Enter Supplier Title!");
                    txtName.Focus();
                    return;
                }

                if (txtContactNo.Text.Trim().Length < 10) // Check if Supplier Contact No is Empty, null or incorrect then show message please enter Supplier Correct Contact No
                {
                    ep.SetError(txtContactNo, "Please Enter Supplier Contact No!");
                    txtContactNo.Focus();
                    return;
                }

                if (txtAddress.Text.Trim().Length == 0) // Check if Address is Empty or null then show message please enter address
                {
                    ep.SetError(txtAddress, "Please Enter Supplier Address!");
                    txtAddress.Focus();
                    return;
                }

                // Check Supplier is Already exist in supplier table or not
                DataTable dt = DatabaseLayer.Select("Select * From SupplierTable Where SupplierName = '" + txtName.Text.Trim() + "' and ContactNo = '" + txtContactNo.Text.Trim() + "'");
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        ep.SetError(txtName, "Already Exist!"); // If Exit then show exist message to user near txtName 
                        txtName.Focus();
                        txtName.SelectAll();
                        return;
                    }
                }

                // Insert Query for supplier registeration
                string insertquery = string.Format("Insert into SupplierTable(SupplierName,ContactNo,Email,Address,Description) " +
                    " values ('{0}', '{1}', '{2}','{3}', '{4}')", txtName.Text.Trim(), txtContactNo.Text.Trim(), txtEmailAddress.Text.Trim(), txtAddress.Text.Trim(), txtDescription.Text.Trim());
                bool result = DatabaseLayer.Insert(insertquery); // Use Insert method of Database Layer to insert customer details
                if (result == true)
                {
                    MessageBox.Show("Registered Successfully!"); // if insert then show successfully message
                    ResetComponents();
                }
                else
                {
                    MessageBox.Show("Please Provide Correct Details!"); // in case of query issue is occure, show this message
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); // Here show exception message, like service down etc
            }
        }

        private void FrmSupplier_Load(object sender, EventArgs e)
        {
            // Load Registered Supplier on form Load
            FillGrid(string.Empty);
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Select Supplier for updation
            if (dgv != null)
            {
                if (dgv.Rows.Count > 0)
                {
                    if (dgv.SelectedRows.Count == 1)
                    {
                        if (MessageBox.Show("Are you sure you want to update selected record!", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            txtName.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[1].Value.ToString(); // SupplierName
                            txtEmailAddress.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[3].Value.ToString(); // EmailAddress
                            txtContactNo.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[2].Value.ToString(); // ContactNo
                            txtAddress.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[4].Value.ToString(); // Address
                            txtDescription.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[5].Value.ToString(); // Description
                            EnableComponents();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please Select One Record!");
                    }
                }
            }
        }
    }
}



Design Customer Form in C# / C-Sharp :
So in this example, we have customer form, show below in figure:

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

namespace POSAccounts.Forms.CustomerForms
{
    public partial class FrmCustomer : Form
    {
        public FrmCustomer()
        {
            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 btnClose_Click(object sender, EventArgs e)
        {
            this.Close(); // Close Customer 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 CustomerTable(it's table) using Select Method of Database layer
        private void FillGrid(string searchvalue)
        {
            try
            {
                // Retirve All Customer List
                string query = "Select CustomerID [ID],  CustomerName [Customer], ContactNo [Contact No], Email [Email], Address FROM CustomerTable";
                if (!string.IsNullOrEmpty(searchvalue))
                {
                    //Retrive on Condition Base means fultering
                    query = "Select CustomerID [ID],  CustomerName [Customer], ContactNo [Contact No], Email [Email], Address FROM CustomerTable WHERE (CustomerName + ' ' + ContactNo + ' ' + Email + ' ' +Address ) like '%" + searchvalue.Trim() + "%'";
                }

                DataTable dt = DatabaseLayer.Select(query);
                dgv.DataSource = dt;
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        dgv.Columns[0].Width = 80; // CustomerID
                        dgv.Columns[1].Width = 120; // CustomerName
                        dgv.Columns[2].Width = 100; // ContactNo
                        dgv.Columns[3].Width = 160; // Email
                        dgv.Columns[4].Width = 400; // Address
                    }
                }
            }
            catch
            {
                dgv.DataSource = null;
            }
        }
        // Valition Method for 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 to reset or cancel updation for details watch vedio
        private void ResetComponents()
        {
            btnCancel.Visible = false;
            btnUpdate.Visible = false;
            btnSave.Visible = true;
            btnClear.Visible = true;
            dgv.Enabled = true;
            txtSearch.Enabled = true;
            FillGrid(string.Empty);
            ClearForm();
        }
        // Clear Form Method for details watch vedio
        private void ClearForm()
        {
            txtName.Clear();
            txtEmailAddress.Clear();
            txtAddress.Clear();
            txtContactNo.Clear();
            txtSearch.Clear();
        }


        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            // Filter Search Key
            FillGrid(txtSearch.Text);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ep.Clear();
                if (txtName.Text.Trim().Length == 0)  // Check if Customer Name is Empty or null then show message please enter Customer
                {
                    ep.SetError(txtName, "Please Enter Customer Title!");
                    txtName.Focus();
                    return;
                }

                if (txtContactNo.Text.Trim().Length < 10 && txtContactNo.Text.Trim().Length > 0)// Check if Customer Contact No is Empty, null or incorrect then show message please enter Customer Correct Contact No
                {
                    ep.SetError(txtContactNo, "Please Enter Customer Contact No!");
                    txtContactNo.Focus();
                    return;
                }

                // Check Customer is Already exist in customer table or not
                DataTable dt = DatabaseLayer.Select("Select * From CustomerTable Where CustomerName = '" + txtName.Text.Trim() + "' and ContactNo = '" + txtContactNo.Text.Trim() + "'");
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        ep.SetError(txtName, "Already Exist!");
                        txtName.Focus();
                        txtName.SelectAll();
                    }
                }

                // Insert Query for Customer Registeration
                string insertquery = string.Format("Insert into CustomerTable(CustomerName,ContactNo,Email,Address) " +
                    " values ('{0}', '{1}', '{2}','{3}')", txtName.Text.Trim(), txtContactNo.Text.Trim(), txtEmailAddress.Text.Trim(), txtAddress.Text.Trim());
                bool result = DatabaseLayer.Insert(insertquery); // Use insert method of database layer to add new customer in customer table
                if (result == true)
                {
                    MessageBox.Show("Registered Successfully!");  // Show Successfully Message
                    ResetComponents();
                }
                else
                {
                    MessageBox.Show("Please Provide Correct Details!"); // if any issue occure related to query, show this message
                }
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message); // if occure any exception like unexpected issue, show actual message
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearForm();  // Close current Form or context
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                ep.Clear();
                if (txtName.Text.Trim().Length == 0)  // Check if Customer Name is Empty or null then show message please enter Customer
                {
                    ep.SetError(txtName, "Please Enter Customer Title!");
                    txtName.Focus();
                    return;
                }

                if (txtContactNo.Text.Trim().Length < 10 && txtContactNo.Text.Trim().Length > 0)// Check if Customer Contact No is Empty, null or incorrect then show message please enter Customer Correct Contact No
                {
                    ep.SetError(txtContactNo, "Please Enter Customer Contact No!");
                    txtContactNo.Focus();
                    return;
                }

                // Check Customer is Already exist in customer table or not
                DataTable dt = DatabaseLayer.Select("Select * From CustomerTable Where CustomerName = '" + txtName.Text.Trim() + "' and ContactNo = '" + txtContactNo.Text.Trim() + "' and CustomerID != '" + dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value + "'");
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        ep.SetError(txtName, "Already Exist!"); // If Exit then show exist message to user near txtName 
                        txtName.Focus();
                        txtName.SelectAll();
                        return;
                    }
                }
                // Update Query for Customer Updation
                string updatequery = string.Format("update CustomerTable set CustomerName = '{0}',ContactNo = '{1}',Email = '{2}',Address = '{3}' Where CustomerID = '{4}'"
                    , txtName.Text.Trim(), txtContactNo.Text.Trim(), txtEmailAddress.Text.Trim(), txtAddress.Text.Trim(), dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value);
                bool result = DatabaseLayer.Update(updatequery); // Use Update method of Database Layer to update customer details
                if (result == true)
                {
                    MessageBox.Show("Updated Successfully!"); // if update then show successfully message
                    ResetComponents();
                }
                else
                {
                    MessageBox.Show("Please Provide Correct Details!"); // in case of query issue is occure, show this message
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); // Here show exception message, like service down etc
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            // Cancel Updation
            ResetComponents();
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Select customer for updation
            if (dgv != null)
            {
                if (dgv.Rows.Count > 0)
                {
                    if (dgv.SelectedRows.Count == 1)
                    {
                        if (MessageBox.Show("Are you sure you want to update selected record!", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            txtName.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[1].Value.ToString(); // SupplierName
                            txtEmailAddress.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[3].Value.ToString(); // EmailAddress
                            txtContactNo.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[2].Value.ToString(); // ContactNo
                            txtAddress.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[4].Value.ToString(); // Address
                            EnableComponents();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please Select One Record!");
                    }
                }
            }
        }

        private void FrmCustomer_Load(object sender, EventArgs e)
        {
            // Load Registered Customer on form load
            FillGrid(string.Empty);
        }
    }
}


Comments