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 Folder to organize our forms in visual studio?
ii) How to Create View for select stock details from two tables at same time in
iii) How to implement ?
iv) How to Create Static Method for Insert Data in Sql Server Database table?
v) How to Create Static Method for update data in Sql server database table?
vi) How to Create Static Method for Delete data for Sql server base database table?
vii) How to Create Static Method to Select Data for Sql server database table?
viii) How to Implement database layer in our project with example?
So if you want all question answers watch the video, and for source code scroll down.
Design Stock Form in C# / C-Sharp :
So in this example, we have stock form, show below in figure:
Source Code Stock Form :
using System; using System.Data; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; using POSAccounts.Helpers; namespace POSAccounts.Forms.StockForms { public partial class FrmStock : Form { public FrmStock() { 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); } } } //Retrive Data From v_StockList(it's view) using Select Method of Database layer private void FillGrid(string searchvalue) { try { // Retirve All Project List string query = "Select ProductID [ID], CategoryID [ID], CategoryName [Category], Name [Item], MfcDate [MFC Date],ExpDate [Exp Date],Quantity [Current Quantity], PurchaseUnitPrice [Purchase Unit Price], SaleUnitPrice [Sale Unit Price], Description FROM v_StockList"; if (!string.IsNullOrEmpty(searchvalue)) { //Retrive on Condition Base means fultering query = "Select ProductID [ID], CategoryID [ID], CategoryName [Category], Name [Item], MfcDate [MFC Date],ExpDate [Exp Date],Quantity [Current Quantity], PurchaseUnitPrice [Purchase Unit Price], SaleUnitPrice [Sale Unit Price], Description FROM v_StockList WHERE (CategoryName + ' ' + Name + ' ' + Description) like '%"+searchvalue.Trim()+"%'"; } DataTable dt = DatabaseLayer.Select(query); dgv.DataSource = dt; if (dt != null) { if (dt.Rows.Count > 0) { dgv.Columns[0].Width = 80; // ProductID dgv.Columns[1].Visible = false; // CategoryID dgv.Columns[2].Width = 120; // CategoryName dgv.Columns[3].Width = 150; // Item dgv.Columns[4].Width = 100; // MfcDate dgv.Columns[5].Width = 100; // ExpDate dgv.Columns[6].Width = 80; // Quantity dgv.Columns[7].Width = 120; // PurchaseUnitPrice dgv.Columns[8].Width = 120; // SaleUnitPrice dgv.Columns[9].Width = 200; // 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(); txtSaleUnitPrice.Text = "0"; txtSearch.Clear(); } private void txtSearch_TextChanged(object sender, EventArgs e) { // Filter Search Key FillGrid(txtSearch.Text.Trim()); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); // Close current Form or context } 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 Item Name is Empty or null then show message please enter item title { ep.SetError(txtName, "Please Enter Item Title!"); txtName.Focus(); return; } if (cmbCategory.SelectedIndex == 0) // Check if category is not selected, show message please select product category { ep.SetError(cmbCategory, "Please select Category!"); cmbCategory.Focus(); return; } float sunitprice = 0; float.TryParse(txtSaleUnitPrice.Text, out sunitprice); // Check if found sale unit price then set it, other wise set 0 // Check Item is Already exist in stock or not DataTable existdt = DatabaseLayer.Select("Select * From ProductTable where CategoryID = '" + cmbCategory.SelectedValue + "' and Name = '" + txtName.Text.Trim() + "' and ProductID != '"+dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value+"' "); if (existdt != null) { if (existdt.Rows.Count > 0)// If Exit then show exist message to user near txtName { ep.SetError(txtName, "Already Exist!"); txtName.Focus(); txtName.SelectAll(); return; } } // Update Query for Product Updation string Updatequery = string.Format("Update ProductTable set CategoryID = '{0}',Name = '{1}',MfcDate ='{2}',ExpDate = '{3}',SaleUnitPrice = '{4}', Description = '{5}' where ProductID = '{6}' ", cmbCategory.SelectedValue, txtName.Text.Trim(), dtpMFC.Value.ToString("yyyy/MM/dd"), dtpExp.Value.ToString("yyyy/MM/dd"), sunitprice, txtDescription.Text, dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value); bool result = DatabaseLayer.Update(Updatequery); // User Update method of Database Layer to update product details if (result == true) { MessageBox.Show("Updated Successfully!"); // if update then show successfully message ResetComponents(); } else { MessageBox.Show("Some unexpected issue is occure plz try agian!"); // 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 btnSave_Click(object sender, EventArgs e) { try { ep.Clear(); if (txtName.Text.Trim().Length == 0) // Check if Item Name is Empty or null then show message please enter item title { ep.SetError(txtName, "Please Enter Item Title!"); txtName.Focus(); return; } if (cmbCategory.SelectedIndex == 0) // Check if category is not selected, show message please select product category { ep.SetError(cmbCategory, "Please select Category!"); cmbCategory.Focus(); return; } float sunitprice = 0; float.TryParse(txtSaleUnitPrice.Text, out sunitprice);// Check if found sale unit price then set it, other wise set 0 // Check Item is Already exist in stock or not DataTable existdt = DatabaseLayer.Select("Select * From ProductTable where CategoryID = '" + cmbCategory.SelectedValue + "' and Name = '" + txtName.Text.Trim() + "'"); if (existdt != null) { if (existdt.Rows.Count > 0) //If Exit then show exist message to user near txtName { ep.SetError(txtName, "Already Exist!"); txtName.Focus(); txtName.SelectAll(); return; } } // Insert Query to insert product in product table string insertquery = string.Format("insert into ProductTable(CategoryID,Name,MfcDate,ExpDate,SaleUnitPrice,Description) values('{0}','{1}','{2}','{3}','{4}','{5}')", cmbCategory.SelectedValue, txtName.Text.Trim(), dtpMFC.Value.ToString("yyyy/MM/dd"), dtpExp.Value.ToString("yyyy/MM/dd"), sunitprice, txtDescription.Text.Trim()); bool result = DatabaseLayer.Insert(insertquery); // Use insert method of database layer to add product in product table if (result == true) { MessageBox.Show("Registered Successfully!"); // Show Successfully Message ResetComponents(); } else { MessageBox.Show("Some unexpected issue is occure plz try agian!"); // 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) { // Clear Form for more details watch vedio ClearForm(); } private void btnMinimize_Click(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.WindowState = FormWindowState.Maximized; } else { this.WindowState = FormWindowState.Minimized; } } private void FrmStock_Load(object sender, EventArgs e) { // Fill all Category in cmbCategory combobox ComboHelper.AllCategories(cmbCategory); //Retrive all product/stock to fill datagridview dgv FillGrid(string.Empty); } private void label3_Click(object sender, EventArgs e) { } private void btnAddCategory_Click(object sender, EventArgs e) { // Add New Category string selectvalue = cmbCategory.SelectedValue.ToString(); // Get Selected Value FrmCategory frm = new FrmCategory(); frm.ShowDialog(); ComboHelper.AllCategories(cmbCategory); // Fill Categories cmbCategory.SelectedValue = selectvalue; // set selected value } private void txtSaleUnitPrice_KeyPress(object sender, KeyPressEventArgs e) { // Setting for only accept digits numbers if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.')) { e.Handled = true; } // only allow one decimal point if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1)) { e.Handled = true; } } private void editToolStripMenuItem_Click(object sender, EventArgs e) { // Select Product 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[3].Value.ToString(); // Item cmbCategory.SelectedValue = dgv.Rows[dgv.SelectedRows[0].Index].Cells[1].Value.ToString(); // CategoryID dtpMFC.Value = Convert.ToDateTime(dgv.Rows[dgv.SelectedRows[0].Index].Cells[4].Value.ToString()); // MFC Date dtpExp.Value = Convert.ToDateTime(dgv.Rows[dgv.SelectedRows[0].Index].Cells[5].Value.ToString()); // ExpDate txtSaleUnitPrice.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[8].Value.ToString(); // Sale Unit Price txtDescription.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[9].Value.ToString(); // Description EnableComponents(); } } else { MessageBox.Show("Please Select One Record!"); } } } } } }
Comments
Post a Comment