0% found this document useful (0 votes)
156 views

Prog 3

The document contains C# class files for different business logic layers (BLL) and data access layers (DAL) used in an e-commerce application called AnyStore. The BLL classes define properties for different entities like categories, customers, products, transactions etc. The DAL classes contain data access methods like insert, update, delete to perform CRUD operations on these entities in the database. CategoriesDAL class shows example methods to select, insert, update and delete category data from database tables.

Uploaded by

Amila Čehić
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Prog 3

The document contains C# class files for different business logic layers (BLL) and data access layers (DAL) used in an e-commerce application called AnyStore. The BLL classes define properties for different entities like categories, customers, products, transactions etc. The DAL classes contain data access methods like insert, update, delete to perform CRUD operations on these entities in the database. CategoriesDAL class shows example methods to select, insert, update and delete category data from database tables.

Uploaded by

Amila Čehić
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 60

FOLDER BLL

categoriesBLL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyStore.BLL
{
class categoriesBLL
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public DateTime added_date { get; set; }
public int added_by { get; set; }

}
}

DeaCustBLL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyStore.BLL
{
class DeaCustBLL
{
public int id { get; set; }
public string type { get; set; }
public string name { get; set; }
public string email { get; set; }
public string contact { get; set; }
public string address { get; set; }
public DateTime added_date { get; set; }
public int added_by { get; set; }
}
}

loginBLL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyStore.BLL
{
class loginBLL
{
public string username { get; set; }
public string password { get; set; }
public string user_type { get; set; }

}
}

productsBLL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyStore.BLL
{
class productsBLL
{
//Getters and Setters for product module
public int id { get; set; }
public string name { get; set; }
public string category { get; set; }
public string description { get; set; }
public decimal rate { get; set; }
public decimal qty { get; set; }
public DateTime added_date { get; set; }
public int added_by { get; set; }

}
}
transactionDetailBLL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyStore.BLL
{
class transactionDetailBLL
{
public int id { get; set; }
public int product_id { get; set; }
public decimal rate { get; set; }
public decimal qty { get; set; }
public decimal total { get; set; }
public int dea_cust_id { get; set; }
public DateTime added_date { get; set; }
public int added_by { get; set; }

}
}

transactionsBLL.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyStore.BLL
{
class transactionsBLL
{
public int id { get; set; }
public string type { get; set; }
public int dea_cust_id { get; set; }
public decimal grandTotal { get; set; }
public DateTime transaction_date { get; set; }
public decimal tax { get; set; }
public decimal discount { get; set; }
public int added_by { get; set; }
public DataTable transactionDetails { get; set; }
}
}

userBLL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnyStore.BLL
{
class userBLL
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public string username { get; set; }
public string password { get; set; }
public string contact { get; set; }
public string address { get; set; }
public string gender { get; set; }
public string user_type { get; set; }
public DateTime added_date { get; set; }
public int added_by { get; set; }

}
}

FOLDER DAL

categoriesDAL.cs

using AnyStore.BLL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.DAL
{
class categoriesDAL
{
/*Static String Method for Database Connection String*/
static string myconnstrng =
ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

#region Select Method


public DataTable Select()
{
/*Creating Database Connection*/
SqlConnection conn = new SqlConnection(myconnstrng);

DataTable dt = new DataTable();

try
{
/*Writing SQL Query to get all the data from Database*/
string sql = "SELECT * FROM tbl_categories";

SqlCommand cmd = new SqlCommand(sql, conn);


SqlDataAdapter adapter = new SqlDataAdapter(cmd);
/*Open*/
conn.Open();
/*Adding the value from adapter to Data Table dt*/
adapter.Fill(dt);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);

}
finally
{
conn.Close();
}

return dt;
}
#endregion
#region Insert New Category
public bool Insert(categoriesBLL c)
{
/*Creating A BOolean Variable and set its default value to false */
bool isSucces = false;

/*Connectin to Database*/
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
/*Writing Wuery to add new category*/
string sql = "INSERT INTO tbl_categories (title, description, added_date,
added_by) VALUES (@title, @description, @added_date, @added_by) ";
/*Creating SQL Command to pass values in our query*/
SqlCommand cmd = new SqlCommand(sql, conn);
//Passing Values through parameter
cmd.Parameters.AddWithValue("@title", c.title);
cmd.Parameters.AddWithValue("@description", c.description);
cmd.Parameters.AddWithValue("@added_date", c.added_date);
cmd.Parameters.AddWithValue("added_by", c.added_by);

//Open Database Connection


conn.Open();

//creating the int variable to execute query


int rows = cmd.ExecuteNonQuery();

//If the Query is executed successfully then its value will be greater than 0 else it
will be less than 0

if(rows > 0)
{
isSucces = true;
}
else
{
isSucces = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return isSucces;
}
#endregion
#region Update Method
public bool Update(categoriesBLL c)
{
//Creating Boolean variable and set its default value to false
bool isSuccess = false;

//Creating Sql Connection


SqlConnection conn = new SqlConnection(myconnstrng);

try
{
//Query to Update Category
string sql = "UPDATE tbl_categories SET title=@title, description=@description,
added_date=@added_date, added_by=@added_by WHERE id=@id";

//SQL Command to Pass the Value on Sql Query


SqlCommand cmd = new SqlCommand(sql, conn);

//Passing Value using cmd


cmd.Parameters.AddWithValue("@title", c.title);
cmd.Parameters.AddWithValue("@description", c.description);
cmd.Parameters.AddWithValue("@added_date", c.added_date);
cmd.Parameters.AddWithValue("@added_by", c.added_by);
cmd.Parameters.AddWithValue("@id", c.id);

//Database Connecrtion
conn.Open();
//Create Int cariable to execute
int rows = cmd.ExecuteNonQuery();
//if the query is successsfully executed then value will be greater then zero
if(rows >0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return isSuccess;
}
#endregion
#region Delete Category Method
public bool Delete(categoriesBLL c)
{
//Create a Boolean variable and set its value to false
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
//SQL Query to delete databes
string sql = "DELETE tbl_categories WHERE id=@id";

SqlCommand cmd = new SqlCommand(sql, conn);


//Passing the value using cmd
cmd.Parameters.AddWithValue("@id", c.id);

//Open SqlConnestion
conn.Open();

int rows = cmd.ExecuteNonQuery();

if(rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;
}
#endregion
#region Method for Search Functionality
public DataTable Search(string keywords)
{
SqlConnection conn = new SqlConnection(myconnstrng);
//Creating Data Table to hold the data from database temporarily
DataTable dt = new DataTable();

try
{
//SQl query to search vcategories from database
string sql = "SELECT * FROM tbl_categories WHERE id LIKE '%" + keywords +
"%' OR title LIKE '%" + keywords + "%' OR description LIKE '%" + keywords + "%' ";
//Creatig SQL command to execute the Query
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);

//Passing values from adapter do data tavle dt


conn.Open();
adapter.Fill(dt);

}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();

return dt;

}
#endregion

}
}

DeaCustDAL.cs

using AnyStore.BLL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.DAL
{
class DeaCustDAL
{
//Static String Method for db conn
static string myconnstrng =
ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

#region Select Method for Dealer and Customer


public DataTable Select()
{
SqlConnection conn = new SqlConnection(myconnstrng);

DataTable dt = new DataTable();

try
{
string sql = "SELECT * FROM tbl_dea_cust";

//Creating sql command to execute query


SqlCommand cmd = new SqlCommand(sql, conn);

//Creating sql data adapter to store data from db


SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return dt;
}
#endregion
#region Insert Method to Add details of Dealer or Customer
public bool Insert(DeaCustBLL dc)
{
SqlConnection conn = new SqlConnection(myconnstrng);

bool isSuccess = false;

try
{
string sql = "INSERT INTO tbl_dea_cust (type, name, email, contact, address,
added_date, added_by) VALUES (@type, @name, @email, @contact, @address,
@added_date, @added_by)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@type", dc.type);
cmd.Parameters.AddWithValue("@name", dc.name);
cmd.Parameters.AddWithValue("@email", dc.email);
cmd.Parameters.AddWithValue("contact", dc.contact);
cmd.Parameters.AddWithValue("@address", dc.address);
cmd.Parameters.AddWithValue("@added_date", dc.added_date);
cmd.Parameters.AddWithValue("@added_by", dc.added_by);
conn.Open();

int rows = cmd.ExecuteNonQuery();

if(rows>0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}

catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return isSuccess;
}
#endregion
#region Update Method for Dealer and Customer Module
public bool Update(DeaCustBLL dc)
{
SqlConnection conn = new SqlConnection(myconnstrng);
bool isSuccess = false;

try
{
string sql = "UPDATE tbl_dea_cust SET type=@type, name=@name,
email=@email, address=@address, added_date=@added_date, added_by=@added_by
WHERE id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@type", dc.type);
cmd.Parameters.AddWithValue("@name", dc.name);
cmd.Parameters.AddWithValue("@email", dc.email);
cmd.Parameters.AddWithValue("@contact", dc.contact);
cmd.Parameters.AddWithValue("@address", dc.address);
cmd.Parameters.AddWithValue("@added_date", dc.added_date);
cmd.Parameters.AddWithValue("@added_by", dc.added_by);
cmd.Parameters.AddWithValue("@id", dc.id);

conn.Open();

int rows = cmd.ExecuteNonQuery();


if(rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return isSuccess;

}
#endregion
#region Delete Method for Dealer and Customer Module
public bool Delete(DeaCustBLL dc)
{
SqlConnection conn = new SqlConnection(myconnstrng);

bool isSuccess = false;

try
{
string sql = "DELETE FROM tbl_dea_cust WHERE id=@id";

SqlCommand cmd = new SqlCommand(sql, conn);


cmd.Parameters.AddWithValue("@id", dc.id);

conn.Open();
int rows = cmd.ExecuteNonQuery();
if(rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;
}
#endregion
#region Search Method fro Dealer and Customer Module
public DataTable Search(string keyword)
{
SqlConnection conn = new SqlConnection(myconnstrng);

DataTable dt = new DataTable();

try
{
string sql = "SELECT * FROM tbl_dea_cust WHERE id LIKE '%" + keyword +
"%' OR type LIKE '%" + keyword + "%' OR name LIKE '%" + keyword + "%' ";

SqlCommand cmd = new SqlCommand(sql, conn);


SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dt;

}
#endregion
#region Method to Search Dealer or Customer for Transaction Module
public DeaCustBLL SearchDealerCustomerForTransaction(string keyword)
{
//Create an object for DeaCustBLL classs
DeaCustBLL dc = new DeaCustBLL();

//Create db conn
SqlConnection conn = new SqlConnection(myconnstrng);

//Create a data table to hold the value temporarily


DataTable dt = new DataTable();

try
{
string sql = "SELECT name, email, contact, address from tbl_dea_cust WHERE id
LIKE '%"+keyword+ "%' OR name LIKE '%" + keyword + "%' ";

//Create a sql data adapter to execute the query


SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
conn.Open();

//Transfer the data from sqlData adaoter to data tavle


adapter.Fill(dt);

if (dt.Rows.Count > 0)
{
dc.name = dt.Rows[0]["name"].ToString();
dc.email = dt.Rows[0]["email"].ToString();
dc.contact = dt.Rows[0]["contact"].ToString();
dc.address = dt.Rows[0]["address"].ToString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dc;
}
#endregion
#region Method to get ID Of The Dealer or Customer based on Name
public DeaCustBLL GetDeaCustIDFromName(string Name)
{
//first create an object of deacust BLL and return it
DeaCustBLL dc = new DeaCustBLL();

//sql conn
SqlConnection conn = new SqlConnection(myconnstrng);
DataTable dt = new DataTable();

try
{
string sql = "SELECT id FROM tbl_dea_cust WHERE name '" + Name + "' ";
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
conn.Open();
if(dt.Rows.Count>0)
{
dc.id = int.Parse(dt.Rows[0]["id"].ToString());
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return dc;
}
#endregion

}
}

loginDAL.cs

using AnyStore.BLL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.DAL
{
class loginDAL
{
/*Static string to Connect Database*/
static string myconstrng =
ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

public bool loginCheck(loginBLL loginBLL)


{
/*Create a bool variable and set it value to false and return it*/
bool isSuccess = false;

/*Connecting to Database*/
SqlConnection conn = new SqlConnection(myconstrng);

try
{
/*SQL Query to check login*/
string sql = "SELECT * FROM tbl_users WHERE username=@username AND
password=@password AND user_type=@user_type";

/*Creating SQL command to pass valuer*/


SqlCommand cmd = new SqlCommand(sql, conn);

cmd.Parameters.AddWithValue("@username", loginBLL.username);
cmd.Parameters.AddWithValue("@password", loginBLL.password);
cmd.Parameters.AddWithValue("@user_type",loginBLL.user_type);

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

adapter.Fill(dt);

/*Checking the rows in Datatable*/


if(dt.Rows.Count>0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}

}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;

}
}
}

productsDAL.cs

using AnyStore.BLL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.DAL
{
class productsDAL
{
//Creating STATI String Method for DB Connection
static string myconnstrng =
ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

#region Select method for Product Module


public DataTable Select()
{
//Create Sql Connection too con nect db
SqlConnection conn = new SqlConnection(myconnstrng);

//Datatable to hold the data from database


DataTable dt = new DataTable();

try
{
//Writing rhw Query to Select all the products from Database
String sql = "SELECT * FROM tbl_products";

//Creating sql comanf to execute query


SqlCommand cmd = new SqlCommand(sql, conn);
//Sql data adapter to hold the value from db
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();

adapter.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return dt;
}
#endregion
#region Method to Insert Product in database
public bool Insert(productsBLL p)
{
//Creating bool variable and set its default value to false
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);

try
{
//SQL query to insert product into db
String sql = "INSERT INTO tbl_products(name, category, description, rate, qty,
added_date, added_by) VALUES (@name, @category, @description, @rate, @qty,
@added_date, @added_by)";

//Create SQL Command to pass the values


SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@name", p.name);
cmd.Parameters.AddWithValue("@category", p.category);
cmd.Parameters.AddWithValue("@description", p.description);
cmd.Parameters.AddWithValue("@rate", p.rate);
cmd.Parameters.AddWithValue("@qty", p.qty);
cmd.Parameters.AddWithValue("@added_date", p.added_date);
cmd.Parameters.AddWithValue("@added_by", p.added_by);

conn.Open();

int rows = cmd.ExecuteNonQuery();

if(rows >0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;
}
#endregion
#region Method to Update Product in Database

public bool Update(productsBLL p)


{
bool isSuccess = false;

SqlConnection conn = new SqlConnection(myconnstrng);


try
{
//Sql query to update
String sql = "UPDATE tbl_products SET name=@name, category=@category,
description=@description, rate=@rate, added_date=@added_date, added_by=@added_by
WHERE id=@id ";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@name", p.name);
cmd.Parameters.AddWithValue("@category", p.category);
cmd.Parameters.AddWithValue("@description", p.description);
cmd.Parameters.AddWithValue("@rate", p.rate);
cmd.Parameters.AddWithValue("@qty", p.qty);
cmd.Parameters.AddWithValue("@added_date", p.added_date);
cmd.Parameters.AddWithValue("@added_by", p.added_by);
cmd.Parameters.AddWithValue("@id", p.id);

conn.Open();

int rows = cmd.ExecuteNonQuery();

if(rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}

}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return isSuccess;
}

#endregion
#region Method to Delete Product from Database

public bool Delete(productsBLL p)


{
bool isSuccess = false;

SqlConnection conn = new SqlConnection(myconnstrng);

try
{
String sql = "DELETE FROM tbl_products WHERE id=@id";

SqlCommand cmd = new SqlCommand(sql, conn);

cmd.Parameters.AddWithValue("@id", p.id);

conn.Open();
int rows = cmd.ExecuteNonQuery();
if(rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return isSuccess;
}

#endregion
#region SEARCH Method for Product Module
public DataTable Search(string keywords)
{
SqlConnection conn = new SqlConnection(myconnstrng);
//Creating DataTable to hold value from db
DataTable dt = new DataTable();

try
{
string sql = "SELECT * FROM tbl_products WHERE id LIKE '%" + keywords +
"%' OR name LIKE '%" + keywords + "%' OR category LIKE '%" + keywords + "%'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();

adapter.Fill(dt);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return dt;
}
#endregion
#region Method to Search Product in Transaction Module
public productsBLL GetProductsForTransaction(string keyword)
{
productsBLL p = new productsBLL();
SqlConnection conn = new SqlConnection(myconnstrng);
DataTable dt = new DataTable();

try
{
string sql = "SELECT name, rate, qty FROM tbl_products WHERE id LIKE '%" +
keyword + "%' OR name LIKE '%" + keyword + "%' ";
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);

conn.Open();

adapter.Fill(dt);
if(dt.Rows.Count>0)
{
p.name = dt.Rows[0]["name"].ToString();
p.rate = decimal.Parse(dt.Rows[0]["rate"].ToString());
p.qty = decimal.Parse(dt.Rows[0]["qty"].ToString());

}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return p;
}
#endregion
#region Method to Get ProductID based on Product Name
public productsBLL GetProductIDFromName(string ProductName)
{
//first create an object of deacust BLL and return it
productsBLL p = new productsBLL();

//sql conn
SqlConnection conn = new SqlConnection(myconnstrng);
DataTable dt = new DataTable();

try
{
string sql = "SELECT id FROM tbl_products WHERE name '" + ProductName + "'
";
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
conn.Open();
if (dt.Rows.Count > 0)
{
p.id = int.Parse(dt.Rows[0]["id"].ToString());
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return p;
}
#endregion
#region Method To Get Current Quantity from The Database Based on Product ID
public decimal GetProductQty(int ProductID)
{
SqlConnection conn = new SqlConnection(myconnstrng);
decimal qty = 0;
DataTable dt = new DataTable();

try
{
string sql = "SELECT qty FROM tbl_products WHERE id = " + ProductID;

SqlCommand cmd = new SqlCommand(sql, conn);


SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);

if(dt.Rows.Count>0)
{
qty = decimal.Parse(dt.Rows[0]["qty"].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return qty;
}
#endregion
#region Method To Update Quantity
public bool UpdateQuantity(int ProductID, decimal Qty)
{
bool success = false;
SqlConnection conn = new SqlConnection(myconnstrng);

try
{
string sql = "UPDATE tbl_products SET qty=@qty WHERE id=@id";

SqlCommand cmd = new SqlCommand(sql, conn);


cmd.Parameters.AddWithValue("@qty", Qty);
cmd.Parameters.AddWithValue("@id", ProductID);
conn.Open();

int rows = cmd.ExecuteNonQuery();


if(rows > 0)
{
success = true;
}
else
{
success = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return success;
}
#endregion
#region Method To Increase Product
public bool IncreaseProduct(int ProductID, decimal IncreaseQty)
{
bool success = false;

SqlConnection conn = new SqlConnection(myconnstrng);

try
{
decimal currentQty = GetProductQty(ProductID);

decimal NewQty = currentQty + IncreaseQty;

success = UpdateQuantity(ProductID, NewQty);


}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return success;
}
#endregion
#region Method To Decrease Product
public bool DecreaseProduct(int ProductID, decimal Qty)
{
bool success = false;

SqlConnection conn = new SqlConnection(myconnstrng);


try
{
decimal currentQty = GetProductQty(ProductID);
decimal NewQty = currentQty - Qty;
success = UpdateQuantity(ProductID, NewQty);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return success;
}

#endregion
}
}

transactionDAL.cs

using AnyStore.BLL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.DAL
{
class transactionDAL
{
//Create a connection string var
static string myconnstrng =
ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

#region Insert Transction Method


public bool Insert_Transaction(transactionsBLL t, out int transactionID)
{
//Create a boolean value and set its default value to false
bool isSuccess = false;
//Set the out transactionID value to negative 1 i.e -1
transactionID = -1;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
string sql = "INSERT INTO tbl_transactions (type, dea_cust_id, grandTotal,
transaction_date, tax, discount, added_by) VALUES (@type, @dea_cust_id, @grandTotal,
@transaction_date, @tax, @discount, @added_by); SELECT @@IDENTITY; ";
SqlCommand cmd = new SqlCommand(sql, conn);

cmd.Parameters.AddWithValue("@type", t.type);
cmd.Parameters.AddWithValue("@dea_cust_id", t.dea_cust_id);
cmd.Parameters.AddWithValue("@grandTotal", t.grandTotal);
cmd.Parameters.AddWithValue("@transaction_date", t.transaction_date);
cmd.Parameters.AddWithValue("@tax", t.tax);
cmd.Parameters.AddWithValue("@discount", t.discount);
cmd.Parameters.AddWithValue("@added_by", t.added_by);

conn.Open();

//Execute the Query


object o = cmd.ExecuteScalar();

//If the Query is executed sucessfully then the value will not be null else it will be
null
if(o!=null)
{
transactionID = int.Parse(o.ToString());
isSuccess = true;
}
else
{
isSuccess = false;
}

}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return isSuccess;
}

#endregion
#region Method To Display All The Transaction
public DataTable DisplayAllTransactions()
{
SqlConnection conn = new SqlConnection(myconnstrng);
DataTable dt = new DataTable();

try
{
string sql = "SELECT * FROM tbl_transactions";

SqlCommand cmd = new SqlCommand(sql, conn);


SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return dt;
}

#endregion
#region Method To Display Transaction Based On Transaction Type
public DataTable DisplayTransactionByType(string type)
{
SqlConnection conn = new SqlConnection(myconnstrng);

DataTable dt = new DataTable();

try
{
string sql = "SELECT * FROM tbl_transactions WHERE type ='" + type + "' ";

SqlCommand cmd = new SqlCommand(sql, conn);


SqlDataAdapter adapter = new SqlDataAdapter(cmd);

conn.Open();
adapter.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dt;
}
#endregion
}

transactionDetailDAL.cs

using AnyStore.BLL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.DAL
{
class transactionDetailDAL
{
static string myconnstrng =
ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

#region Insert Method for Transaction Detail


public bool InsertTransactionDetail(transactionDetailBLL td)
{
bool isSuccess = false;

SqlConnection conn = new SqlConnection(myconnstrng);

try
{
string sql = "INSERT INTO tbl_transaction_detail (product_id, rate, qty, total,
dea_cust_id, added_date, added_by) VALUES (@product_id, @rate, @qty, @total,
@dea_cust_id, @added_date, @added_by)";

SqlCommand cmd = new SqlCommand(sql, conn);

cmd.Parameters.AddWithValue("@product_id", td.product_id);
cmd.Parameters.AddWithValue("@rate", td.rate);
cmd.Parameters.AddWithValue("@qty", td.qty);
cmd.Parameters.AddWithValue("@total", td.total);
cmd.Parameters.AddWithValue("@dea_cust_id", td.dea_cust_id);
cmd.Parameters.AddWithValue("@added_date", td.added_date);
cmd.Parameters.AddWithValue("@added_by", td.added_by);

conn.Open();

//Declare the int var and execute query


int rows = cmd.ExecuteNonQuery();

if(rows>0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;
}

#endregion

}
}

userDAL.cs

using AnyStore.BLL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.DAL
{
class userDAL
{
static string myconnstrng =
ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

#region Select Data from Database


public DataTable Select()
{
/* Static Method to connect Database */
SqlConnection conn = new SqlConnection(myconnstrng);
/*To hold the data from database*/
DataTable dt = new DataTable();
try
{
/*SQL Query to Get Data from database*/
String sql = "SELECT * FROM tbl_users";
/*For exucuting Command*/
SqlCommand cmd = new SqlCommand(sql, conn);
/*Getting data from database*/
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
/*Database connection open*/
conn.Open();
/*fill data in our datatable*/
adapter.Fill(dt);

}
catch(Exception ex)
{
/*Throw Message if any error occurs*/
MessageBox.Show(ex.Message);
}
finally
{
/*closing connection*/
conn.Close();
}
/*return the value in datatable*/
return dt;
}
#endregion
#region Insert Data in Database

public bool Insert(userBLL u)


{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);

try
{
String sql = "INSERT INTO tbl_users (first_name, last_name, email, username,
password, contact, address, gender, user_type, added_date, added_by) VALUES
(@first_name, @last_name, @email, @username, @password, @contact, @address,
@gender, @user_type, @added_date, @added_by)";
SqlCommand cmd = new SqlCommand(sql, conn);

cmd.Parameters.AddWithValue("@first_name", u.first_name);
cmd.Parameters.AddWithValue("@last_name", u.last_name);
cmd.Parameters.AddWithValue("@email", u.email);
cmd.Parameters.AddWithValue("@username", u.username);
cmd.Parameters.AddWithValue("@password", u.password);
cmd.Parameters.AddWithValue("@contact", u.contact);
cmd.Parameters.AddWithValue("@address", u.address);
cmd.Parameters.AddWithValue("@gender", u.gender);
cmd.Parameters.AddWithValue("@user_type", u.user_type);
cmd.Parameters.AddWithValue("@added_date", u.added_date);
cmd.Parameters.AddWithValue("@added_by", u.added_by);

conn.Open();
int rows = cmd.ExecuteNonQuery();
/*If the query is executed Successfully then the value to rows will be greater than 0
else it will be less than 0*/
if(rows > 0)
{
/*query successful*/
isSuccess = true;
}
else
{
/*Query failed*/
isSuccess = false;
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;

}
#endregion
#region Update data in Database
public bool Update(userBLL u)
{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
string sql = "UPDATE tbl_users SET first_name=@first_name,
last_name=@last_name, email=@email, username=@username, password=@password,
contact=@contact, address=@address, gender=@gender, user_type=@user_type,
added_date=@added_date, added_by=@added_by WHERE id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);

cmd.Parameters.AddWithValue("@first_name", u.first_name);
cmd.Parameters.AddWithValue("@last_name", u.last_name);
cmd.Parameters.AddWithValue("@email", u.email);
cmd.Parameters.AddWithValue("@username", u.username);
cmd.Parameters.AddWithValue("@password", u.password);
cmd.Parameters.AddWithValue("@contact", u.contact);
cmd.Parameters.AddWithValue("@address", u.address);
cmd.Parameters.AddWithValue("@gender", u.gender);
cmd.Parameters.AddWithValue("@user_type", u.user_type);
cmd.Parameters.AddWithValue("@added_date", u.added_date);
cmd.Parameters.AddWithValue("@added_by", u.added_by);
cmd.Parameters.AddWithValue("@id", u.id);

conn.Open();

int rows = cmd.ExecuteNonQuery();


if(rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;

}
#endregion
#region Delete Data from Database

public bool Delete (userBLL u)


{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
string sql = "DELETE FROM tbl_users WHERE id=@id";

SqlCommand cmd = new SqlCommand(sql, conn);


cmd.Parameters.AddWithValue("@id", u.id);
conn.Open();
int rows = cmd.ExecuteNonQuery();
if(rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSuccess;

}
#endregion
#region Search User on Database usingKeywords
public DataTable Search(string keywords)
{
/* Static Method to connect Database */
SqlConnection conn = new SqlConnection(myconnstrng);
/*To hold the data from database*/
DataTable dt = new DataTable();
try
{
/*SQL Query to Get Data from database*/
String sql = "SELECT * FROM tbl_users WHERE id LIKE '%" +keywords+ "%'
OR first_name LIKE '%" +keywords+ "%' OR last_name LIKE '%"+keywords+"%' OR
username LIKE '%"+keywords+"%' ";
/*For exucuting Command*/
SqlCommand cmd = new SqlCommand(sql, conn);
/*Getting data from database*/
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
/*Database connection open*/
conn.Open();
/*fill data in our datatable*/
adapter.Fill(dt);

}
catch (Exception ex)
{
/*Throw Message if any error occurs*/
MessageBox.Show(ex.Message);
}
finally
{
/*closing connection*/
conn.Close();
}
/*return the value in datatable*/
return dt;
}
#endregion
#region Getting User ID from Username
public userBLL GetIDFromUsername (string username)
{
userBLL u = new userBLL();
SqlConnection conn = new SqlConnection(myconnstrng);
DataTable dt = new DataTable();

try
{
string sql="SELECT id FROM tbl_users WHERE username='"+ username + "'";

SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);


conn.Open();

adapter.Fill(dt);
if(dt.Rows.Count>0)
{
u.id = int.Parse(dt.Rows[0]["id"].ToString());

}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return u;
}
#endregion

}
}

FOLDER UI FORME

frmAdminDashboard.cs

using AnyStore.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore
{
public partial class frmAdminDashboard : Form
{
public frmAdminDashboard()
{
InitializeComponent();
}

private void vToolStripMenuItem_Click(object sender, EventArgs e)


{
frmCategories category = new frmCategories();
category.Show();
}

private void userToolStripMenuItem_Click(object sender, EventArgs e)


{
frmUsers user = new frmUsers();
user.Show();
}

private void frmAdminDashboard_FormClosed(object sender, FormClosedEventArgs e)


{
frmLogin login = new frmLogin();
login.Show();
this.Hide();
}

private void frmAdminDashboard_Load(object sender, EventArgs e)


{
lblLoggedInUser.Text = frmLogin.loggedIn;
}

private void productsToolStripMenuItem_Click(object sender, EventArgs e)


{
frmProducts product = new frmProducts();
product.Show();
}

private void dealerAndCustomerToolStripMenuItem_Click(object sender, EventArgs e)


{
frmDeaCust DeaCust = new frmDeaCust();
DeaCust.Show();
}

private void transactionToolStripMenuItem_Click(object sender, EventArgs e)


{
frmTransactions transaction = new frmTransactions();
transaction.Show();
}
}
}

frmCategories.cs

using AnyStore.BLL;
using AnyStore.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.UI
{
public partial class frmCategories : Form
{
public frmCategories()
{
InitializeComponent();
}

private void pictureBoxClose_Click(object sender, EventArgs e)


{
this.Hide();
}

categoriesBLL c = new categoriesBLL();


categoriesDAL dal = new categoriesDAL();
userDAL udal = new userDAL();
private void btnAdd_Click(object sender, EventArgs e)
{
//Get the values from category form
c.title = txtTitle.Text;
c.description = txtDescription.Text;
c.added_date = DateTime.Now;

//Getting ID in added by field


string loggedUser = frmLogin.loggedIn;
userBLL usr = udal.GetIDFromUsername(loggedUser);
//Passign the id of logged in user in added by field
c.added_by = usr.id;
//Creating Boolean method to insert data into database
bool success = dal.Insert(c);

//If the category is inserted successfully then the value of the success will be true else
it will be false
if(success == true)
{
//New category inserted
MessageBox.Show("New Category Inserted Successfully.");
Clear();
//Refresh data grid view
DataTable dt = dal.Select();
dgvCategories.DataSource = dt;
}
else
{
//failed
MessageBox.Show("Failed to Insert New Category.");
}
}
public void Clear()
{
txtCategoryID.Text = "";
txtTitle.Text = "";
txtDescription.Text = "";
txtSearch.Text = "";
}

private void frmCategories_Load(object sender, EventArgs e)


{
//Here write the code to display all the categories in Data Grid View
DataTable dt = dal.Select();
dgvCategories.DataSource = dt;
}

private void dgvCategories_RowHeaderMouseClick(object sender,


DataGridViewCellMouseEventArgs e)
{
//Finding the Row Index of the Row Clicked on Data Grid View
int RowIndex = e.RowIndex;
txtCategoryID.Text = dgvCategories.Rows[RowIndex].Cells[0].Value.ToString();
txtTitle.Text = dgvCategories.Rows[RowIndex].Cells[1].Value.ToString();
txtDescription.Text = dgvCategories.Rows[RowIndex].Cells[2].Value.ToString();

private void btnUpdate_Click(object sender, EventArgs e)


{
//Get the values from the Category form
c.id = int.Parse(txtCategoryID.Text);
c.title = txtTitle.Text;
c.description = txtDescription.Text;
c.added_date = DateTime.Now;
//Getting ID in added by field
string loggedUser = frmLogin.loggedIn;
userBLL usr = udal.GetIDFromUsername(loggedUser);
//Passign the id of logged in user in added by field
c.added_by = usr.id;

//Creating Boolean variable to update categories and check


bool success = dal.Update(c);
//if the categories is updated successfully then the value of success will be true it will
be false
if(success == true)
{
MessageBox.Show("Category Updated Successfully");
Clear();
//Refresh
DataTable dt = dal.Select();
dgvCategories.DataSource = dt;

}
else
{
MessageBox.Show("Failed to update category");
}

private void btnDelete_Click(object sender, EventArgs e)


{
//Get the ID of the Category Which we want to Delete
c.id = int.Parse(txtCategoryID.Text);

//Creating Boolean Variable to Delete the Category


bool success = dal.Delete(c);
//if the id deleted successfully then the value will be tru or false
if(success == true)
{
MessageBox.Show("Category deleted successfully!");
Clear();
DataTable dt = dal.Select();
dgvCategories.DataSource = dt;

}
else
{
MessageBox.Show("Failed to delete category!");
}

private void txtSearch_TextChanged(object sender, EventArgs e)


{
//Get the keywords
string keywords = txtSearch.Text;

//Filter the categories based on keyword


if(keywords!=null)
{
DataTable dt = dal.Search(keywords);
dgvCategories.DataSource = dt;

}
else
{
DataTable dt = dal.Select();
dgvCategories.DataSource = dt;

}
}
}
}

frmDeaCust.cs

using AnyStore.BLL;
using AnyStore.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.UI
{
public partial class frmDeaCust : Form
{
public frmDeaCust()
{
InitializeComponent();
}

private void pictureBoxClose_Click(object sender, EventArgs e)


{
this.Hide();
}

DeaCustBLL dc = new DeaCustBLL();


DeaCustDAL dcDal = new DeaCustDAL();

userDAL uDal = new userDAL();


private void btnAdd_Click(object sender, EventArgs e)
{
dc.type = cmbDeaCust.Text;
dc.name = txtName.Text;
dc.email = txtEmail.Text;
dc.contact = txtContact.Text;
dc.address = txtAddress.Text;
dc.added_date = DateTime.Now;
//Getting the ID to Logged in user and passign its value in dealer or customer module
string loggedUsr = frmLogin.loggedIn;
userBLL usr = uDal.GetIDFromUsername(loggedUsr);
dc.added_by = usr.id;

//Creating bool var to check whether th dealer or customer is added or not


bool success = dcDal.Insert(dc);
if(success==true)
{
MessageBox.Show("Dealer or Customer Added Successfully.");
Clear();
DataTable dt = dcDal.Select();
dgvDeaCust.DataSource = dt;
}
else
{
MessageBox.Show("Failed to Added Dealer or Customer.");
}

}
public void Clear()
{
txtDeaCustID.Text = "";
txtName.Text = "";
txtEmail.Text = "";
txtContact.Text = "";
txtAddress.Text = "";
txtSearch.Text = "";

private void frmDeaCust_Load(object sender, EventArgs e)


{
DataTable dt = dcDal.Select();
dgvDeaCust.DataSource = dt;
}

private void dgvDeaCust_RowHeaderMouseClick(object sender,


DataGridViewCellMouseEventArgs e)
{
int rowIndex = e.RowIndex;

txtDeaCustID.Text = dgvDeaCust.Rows[rowIndex].Cells[0].Value.ToString();
cmbDeaCust.Text = dgvDeaCust.Rows[rowIndex].Cells[1].Value.ToString();
txtName.Text = dgvDeaCust.Rows[rowIndex].Cells[2].Value.ToString();
txtEmail.Text = dgvDeaCust.Rows[rowIndex].Cells[3].Value.ToString();
txtContact.Text = dgvDeaCust.Rows[rowIndex].Cells[4].Value.ToString();
txtAddress.Text = dgvDeaCust.Rows[rowIndex].Cells[5].Value.ToString();
}

private void btnDelete_Click(object sender, EventArgs e)


{
dc.id = int.Parse(txtDeaCustID.Text);

bool success = dcDal.Delete(dc);

if (success == true)
{
MessageBox.Show("Product successfully deleted!");
Clear();
DataTable dt = dcDal.Select();
dgvDeaCust.DataSource = dt;
}
else
{
MessageBox.Show("Failed to Delete Product!");
}
}

private void txtSearch_TextChanged(object sender, EventArgs e)


{
string keyword = txtSearch.Text;

if(keyword!=null)
{
DataTable dt = dcDal.Search(keyword);
dgvDeaCust.DataSource = dt;
}
else
{
DataTable dt = dcDal.Select();
dgvDeaCust.DataSource = dt;
}
}

private void btnUpdate_Click(object sender, EventArgs e)


{
dc.id = int.Parse(txtDeaCustID.Text);
dc.type = cmbDeaCust.Text;
dc.name = txtName.Text;
dc.email = txtEmail.Text;
dc.contact = txtContact.Text;
dc.address = txtAddress.Text;
dc.added_date = DateTime.Now;

//Getting the ID to Logged in user and passign its value in dealer or customer module
string loggedUsr = frmLogin.loggedIn;
userBLL usr = uDal.GetIDFromUsername(loggedUsr);
dc.added_by = usr.id;

bool success = dcDal.Update(dc);

if(success==true)
{
MessageBox.Show("Dealer or Customer Updated Successfully");
Clear();
DataTable dt = dcDal.Select();
dgvDeaCust.DataSource = dt;

}
else
{
MessageBox.Show("Failed to Update Dealer or Customer.");
}
}
}
}

frmLogin.cs

using AnyStore.BLL;
using AnyStore.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AnyStore.UI
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}

loginBLL loginBLL = new loginBLL();


loginDAL dal = new loginDAL();
public static string loggedIn;

private void pictureBox2_Click(object sender, EventArgs e)


{
this.Close();
}

private void btnLogin_Click(object sender, EventArgs e)


{
loginBLL.username = txtUsername.Text.Trim();
loginBLL.password = txtPassword.Text.Trim();
loginBLL.user_type = cmbUserType.Text.Trim();

/*CHecking the login credentials*/


bool success = dal.loginCheck(loginBLL);
if(success==true)
{
MessageBox.Show("Login Successfull");

/*Need to open Respective Forms based on User Type*/


loggedIn = loginBLL.username;
switch (loginBLL.user_type)
{
case "Admin":
{
//*Display Admin Dashboard*/
frmAdminDashboard admin = new frmAdminDashboard();
admin.Show();
this.Hide();

}
break;
case "User":
{
frmUserDashboard user = new frmUserDashboard();
user.Show();
this.Hide();
}
break;
default:
{
/*Display an error message*/
MessageBox.Show("Invlaid User Type.");
}
break;
}
}
else
{
MessageBox.Show("Login Failed. Try Again");
}
}

}
}

frmProducts.cs

using AnyStore.BLL;
using AnyStore.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.UI
{
public partial class frmProducts : Form
{
public frmProducts()
{
InitializeComponent();
}

private void pictureBoxClose_Click(object sender, EventArgs e)


{
this.Hide();
}

productsBLL p = new productsBLL();


productsDAL pdal = new productsDAL();
userDAL udal = new userDAL();
categoriesDAL cdal = new categoriesDAL();
private void frmProducts_Load(object sender, EventArgs e)
{
//Creating DataTable to hold the categories from DB
DataTable categoriesDT = cdal.Select();
//Specifiy Data Source for Category Cmb
cmbCategory.DataSource = categoriesDT;
//Specify Display Member and value member for cmb
cmbCategory.DisplayMember = "title";
cmbCategory.ValueMember = "title";

//load all the products in dgv


DataTable dt = pdal.Select();
dgvProducts.DataSource = dt;

private void btnAdd_Click(object sender, EventArgs e)


{
//Get all the vallues iz forme
p.name = txtName.Text;
p.category = cmbCategory.Text;
p.description = txtDescription.Text;
p.rate = decimal.Parse(txtRate.Text);
p.qty = 0;
p.added_date = DateTime.Now;
//getting un logged in user
String loggedUsr = frmLogin.loggedIn;
userBLL usr = udal.GetIDFromUsername(loggedUsr);

p.added_by = usr.id;

bool success = pdal.Insert(p);


if (success == true)
{
MessageBox.Show("Product Added Successfully.");
Clear();
//refresh dgv
DataTable dt = pdal.Select();
dgvProducts.DataSource = dt;
dgvProducts.ForeColor = Color.Black;

}
else
{
MessageBox.Show("Failed to Add New Product");
}
}
public void Clear()
{
txtID.Text = "";
txtName.Text = "";
txtDescription.Text = "";
txtRate.Text = "";
txtSearch.Text = "";
}

private void dgvProducts_RowHeaderMouseClick(object sender,


DataGridViewCellMouseEventArgs e)
{
int rowIndex = e.RowIndex;
txtID.Text = dgvProducts.Rows[rowIndex].Cells[0].Value.ToString();
txtName.Text = dgvProducts.Rows[rowIndex].Cells[1].Value.ToString();
cmbCategory.Text = dgvProducts.Rows[rowIndex].Cells[2].Value.ToString();
txtDescription.Text = dgvProducts.Rows[rowIndex].Cells[3].Value.ToString();
txtRate.Text = dgvProducts.Rows[rowIndex].Cells[4].Value.ToString();
}

private void btnUpdate_Click(object sender, EventArgs e)


{

p.id = int.Parse(txtID.Text);
p.name = txtName.Text;
p.category = cmbCategory.Text;
p.description = txtDescription.Text;
p.rate = decimal.Parse(txtRate.Text);
p.added_date = DateTime.Now;

String loggedUsr = frmLogin.loggedIn;


userBLL usr = udal.GetIDFromUsername(loggedUsr);

p.added_by = usr.id;

bool success = pdal.Update(p);


if (success == true)
{
MessageBox.Show("Product successfully updated!");
Clear();
DataTable dt = pdal.Select();
dgvProducts.DataSource = dt;
}
else
{
MessageBox.Show("Failed to Update Product!");
}
}

private void btnDelete_Click(object sender, EventArgs e)


{

//Get the id of the pr to be deleted


p.id = int.Parse(txtID.Text);

//Create bool to check


bool success = pdal.Delete(p);

if (success == true)
{
MessageBox.Show("Product successfully deleted!");
Clear();
DataTable dt = pdal.Select();
dgvProducts.DataSource = dt;
}
else
{
MessageBox.Show("Failed to Delete Product!");
}
}

private void txtSearch_TextChanged(object sender, EventArgs e)


{
//Get the keywords from form
string keywords = txtSearch.Text;

if(keywords!=null)
{
//Search the products
DataTable dt = pdal.Search(keywords);
dgvProducts.DataSource = dt;
}
else
{
//Display all the products
DataTable dt = pdal.Select();
dgvProducts.DataSource = dt;
}
}
}
}
frmPurchaseAndSales.cs

using AnyStore.BLL;
using AnyStore.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Windows.Forms;

namespace AnyStore.UI
{
public partial class frmPurchaseAndSales : Form
{
public frmPurchaseAndSales()
{
InitializeComponent();
}

private void pictureBoxClose_Click(object sender, EventArgs e)


{
this.Hide();
}
DeaCustDAL dcDAL = new DeaCustDAL();
productsDAL pDAL = new productsDAL();
userDAL uDAL = new userDAL();
transactionDAL tDAL = new transactionDAL();
transactionDetailDAL tdDAL = new transactionDetailDAL();

DataTable transactionDT = new DataTable();


private void frmPurchaseAndSales_Load(object sender, EventArgs e)
{
//Get the transactionType value from frmUserDashboard
string type = frmUserDashboard.transactionType;
//Set the value on lblTop
lblTop.Text = type;

//specify columns for our transactiondatatable


transactionDT.Columns.Add("Product Name");
transactionDT.Columns.Add("Rate");
transactionDT.Columns.Add("Quantity");
transactionDT.Columns.Add("Total");

private void txtSearch_TextChanged(object sender, EventArgs e)


{
//get the keyword from txtbox
string keyword = txtSearch.Text;
if(keyword=="")
{
//clear
txtName.Text = "";
txtEmail.Text = "";
txtContact.Text = "";
txtAddress.Text = "";
return;
}

//write to ger the deta and set the value ot ctsd


DeaCustBLL dc = dcDAL.SearchDealerCustomerForTransaction(keyword);

txtName.Text = dc.name;
txtEmail.Text = dc.email;
txtContact.Text = dc.contact;
txtAddress.Text = dc.address;

private void txtSearchProduct_TextChanged(object sender, EventArgs e)


{
string keyword = txtSearchProduct.Text;

if(keyword=="")
{
txtProductName.Text = "";
txtInventory.Text = "";
txtRate.Text = "";
txtQty.Text = "";
return;
}

productsBLL p = pDAL.GetProductsForTransaction(keyword);

txtProductName.Text = p.name;
txtInventory.Text = p.qty.ToString();
txtRate.Text = p.rate.ToString();
}

private void btnAdd_Click(object sender, EventArgs e)


{
//get product name, rate and qty customer wants to buy
string productName = txtProductName.Text;
decimal Rate = decimal.Parse(txtRate.Text);
decimal Qty = decimal.Parse(txtQty.Text);

decimal Total = Rate * Qty; //total =ratexqty


//display th subtotoal
//get the subtotal value from txt
decimal subTotal = decimal.Parse(txtSubTotal.Text);
subTotal = subTotal + Total;

//check whether the product is selected or not


if(productName == "")
{
MessageBox.Show("Select the product first. Try again!");
}
else
{
transactionDT.Rows.Add(productName,Rate,Qty, Total);

//Show in dgv
dgvAddedProducts.DataSource = transactionDT;
//displax sub
txtSubTotal.Text = subTotal.ToString();

//clear tx6
txtSearchProduct.Text = "";
txtProductName.Text = "";
txtInventory.Text = "0.00";
txtRate.Text = "0.00";
txtQty.Text = "0.00";

}
}

private void txtDiscount_TextChanged(object sender, EventArgs e)


{
//get the value from discount txt
string value = txtDiscount.Text;

if(value=="")
{
MessageBox.Show("Please Add Discount First");
}
else
{
//get the discount id decimal value
decimal subTotal = decimal.Parse(txtSubTotal.Text);
decimal discount = decimal.Parse(txtDiscount.Text);

//calculate the grandtotal based on discount


decimal grandTotal = ((100 - discount) / 100) * subTotal;

txtGrandTotal.Text = grandTotal.ToString();

}
}

private void txtVat_TextChanged(object sender, EventArgs e)


{
//check if the grandTotal has value or not if it has not value then calculate the discount
first
string check = txtGrandTotal.Text;
if(check == "")
{
MessageBox.Show("Calculate the discount and set the Grand total first");
}
else
{
//calculate VAT
//gettin the vaat percent first
decimal previousGT = decimal.Parse(txtGrandTotal.Text);
decimal vat = decimal.Parse(txtVat.Text);
decimal grandTotalWithVAT = ((100 + vat)/100)*previousGT;

txtGrandTotal.Text = grandTotalWithVAT.ToString();
}
}

private void txtPaidAmount_TextChanged(object sender, EventArgs e)


{
//get the paid amount and grand total7
decimal grandTotal = decimal.Parse(txtGrandTotal.Text);
decimal paidAmount = decimal.Parse(txtPaidAmount.Text);

decimal returnAmount = paidAmount - grandTotal;


txtReturnAmount.Text = returnAmount.ToString();
}

private void btnSave_Click(object sender, EventArgs e)


{
//get the values from purchaseSales form first
transactionsBLL transaction = new transactionsBLL();

transaction.type = lblTop.Text;
//get the id of dealer or cuszomer
//lets get name of the d or c first
string deaCustName = txtName.Text;

DeaCustBLL dc = dcDAL.GetDeaCustIDFromName(deaCustName);

transaction.dea_cust_id = dc.id;
transaction.grandTotal = Math.Round(decimal.Parse(txtGrandTotal.Text),2);
transaction.transaction_date = DateTime.Now;
transaction.tax = decimal.Parse(txtVat.Text);
transaction.discount = decimal.Parse(txtDiscount.Text);
string username = frmLogin.loggedIn;
userBLL u = uDAL.GetIDFromUsername(username);

transaction.added_by = u.id;
transaction.transactionDetails = transactionDT;

//lets create a boolean variable and set its value to false


bool success = false;

//actual code to insert transaction add transaction details


using (TransactionScope scope = new TransactionScope())
{
int transactionID = -1;
bool w = tDAL.Insert_Transaction(transaction, out transactionID);

//use for loop insert transaction detailsa


for(int i=0;i<transactionDT.Rows.Count;i++)
{
//get all the details of the product
transactionDetailBLL transactionDetail = new transactionDetailBLL();
//get product name and convert it to id
string ProductName = transactionDT.Rows[i][0].ToString();
productsBLL p = pDAL.GetProductIDFromName(ProductName);

transactionDetail.product_id = p.id;
transactionDetail.rate = decimal.Parse(transactionDT.Rows[i][1].ToString());
transactionDetail.qty = decimal.Parse(transactionDT.Rows[i][2].ToString());
transactionDetail.total = Math.Round(decimal.Parse(transactionDT.Rows[i]
[3].ToString()),2);
transactionDetail.dea_cust_id = dc.id;
transactionDetail.added_date = DateTime.Now;
transactionDetail.added_by = u.id;

//Insert Transaction Details inside the database


bool y = tdDAL.InsertTransactionDetail(transactionDetail);
success = w && y;

if (success == true)
{
scope.Complete();
MessageBox.Show("Transaction Completed Successfully!");
dgvAddedProducts.DataSource = null;
dgvAddedProducts.Rows.Clear();

txtSearch.Text = "";
txtName.Text = "";
txtEmail.Text = "";
txtContact.Text = "";
txtAddress.Text = "";
txtSearchProduct.Text = "";
txtProductName.Text = "";
txtInventory.Text = "0";
txtRate.Text = "0";
txtQty.Text = "0";
txtSubTotal.Text = "0";
txtDiscount.Text = "0";
txtVat.Text = "0";
txtGrandTotal.Text = "0";
txtPaidAmount.Text = "0";
txtReturnAmount.Text = "0";
}
else
{
MessageBox.Show("Transaction Failed!");
}
}
}
}

frmTransactions.cs

using AnyStore.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.UI
{
public partial class frmTransactions : Form
{
public frmTransactions()
{
InitializeComponent();
}

transactionDAL tdal = new transactionDAL();


private void pictureBoxClose_Click(object sender, EventArgs e)
{
this.Hide();
}

private void frmTransactions_Load(object sender, EventArgs e)


{
DataTable dt = tdal.DisplayAllTransactions();
dgvTransactions.DataSource = dt;
}

private void cmbTransactionType_SelectedIndexChanged(object sender, EventArgs e)


{
string type = cmbTransactionType.Text;

DataTable dt = tdal.DisplayTransactionByType(type);
dgvTransactions.DataSource = dt;
}

private void btnAll_Click(object sender, EventArgs e)


{
DataTable dt = tdal.DisplayAllTransactions();
dgvTransactions.DataSource = dt;
}
}
}

frmUserDashboard.cs

using AnyStore.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore
{
public partial class frmUserDashboard : Form
{
public frmUserDashboard()
{
InitializeComponent();
}

//Set a public static method to specify whether the form is purchase or sales
public static string transactionType;
private void frmUserDashboard_FormClosed(object sender, FormClosedEventArgs e)
{

frmLogin login = new frmLogin();


login.Show();
this.Hide();
}

private void frmUserDashboard_Load(object sender, EventArgs e)


{
lblLoggedInUser.Text = frmLogin.loggedIn;
}

private void dealerAndCustomerToolStripMenuItem_Click(object sender, EventArgs e)


{
frmDeaCust DeaCust = new frmDeaCust();
DeaCust.Show();

private void purchaseToolStripMenuItem_Click(object sender, EventArgs e)


{
//set value on transactionType static method
transactionType = "Purchase";
frmPurchaseAndSales purchase = new frmPurchaseAndSales();
purchase.Show();

private void salesFormsToolStripMenuItem_Click(object sender, EventArgs e)


{
//set the value to transactionType method to sales
transactionType = "Sales";

frmPurchaseAndSales sales = new frmPurchaseAndSales();


sales.Show();

}
}
}

frmUsers.cs

using AnyStore.BLL;
using AnyStore.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore.UI
{
public partial class frmUsers : Form
{
public frmUsers()
{
InitializeComponent();
}

userBLL u = new userBLL();


userDAL dal = new userDAL();

private void pictureBoxClose_Click(object sender, EventArgs e)


{
this.Close();

private void lblGender_Click(object sender, EventArgs e)


{

private void btnAdd_Click(object sender, EventArgs e)


{
/*Getting Data From UI*/

u.first_name = txtFirstName.Text;
u.last_name = txtLastName.Text;
u.email = txtEmail.Text;
u.username = txtUsername.Text;
u.password = txtPassword.Text;
u.contact = txtContact.Text;
u.address = txtAddress.Text;
u.gender = cmbGender.Text;
u.user_type = cmbUserType.Text;
u.added_date = DateTime.Now;

/*Getting username of the logged in user*/


string loggedUser = frmLogin.loggedIn;
userBLL usr = dal.GetIDFromUsername(loggedUser);
u.added_by = usr.id;

/*Inserting Data into Database*/


bool success = dal.Insert(u);
/*If the data is successfully inserted then the value od success will be true else it will
be fale*/
if(success==true)
{
MessageBox.Show("User successfully created");
clear();
}
else
{
MessageBox.Show("Failed to add new user");

}
DataTable dt = dal.Select();
dgvUsers.DataSource = dt;

private void frmUsers_Load(object sender, EventArgs e)


{
DataTable dt = dal.Select();
dgvUsers.DataSource = dt;

}
private void clear()
{
txtUserID.Text = "";
txtFirstName.Text = "";
txtLastName.Text = "";
txtUsername.Text = "";
txtPassword.Text = "";
txtContact.Text = "";
txtAddress.Text = "";
txtEmail.Text = "";
cmbGender.Text = "";
cmbUserType.Text = "";

private void dgvUsers_RowHeaderMouseClick(object sender,


DataGridViewCellMouseEventArgs e)
{
/*Get the index of particular row*/
int rowIndex = e.RowIndex;
txtUserID.Text = dgvUsers.Rows[rowIndex].Cells[0].Value.ToString();
txtFirstName.Text = dgvUsers.Rows[rowIndex].Cells[1].Value.ToString();
txtLastName.Text = dgvUsers.Rows[rowIndex].Cells[2].Value.ToString();
txtEmail.Text = dgvUsers.Rows[rowIndex].Cells[3].Value.ToString();
txtUsername.Text = dgvUsers.Rows[rowIndex].Cells[4].Value.ToString();
txtPassword.Text = dgvUsers.Rows[rowIndex].Cells[5].Value.ToString();
txtContact.Text = dgvUsers.Rows[rowIndex].Cells[6].Value.ToString();
txtAddress.Text = dgvUsers.Rows[rowIndex].Cells[7].Value.ToString();
cmbGender.Text = dgvUsers.Rows[rowIndex].Cells[8].Value.ToString();
cmbUserType.Text = dgvUsers.Rows[rowIndex].Cells[9].Value.ToString();

private void btnUpdate_Click(object sender, EventArgs e)


{
/*get the valuea from user UI*/
u.id = Convert.ToInt32(txtUserID.Text);
u.first_name = txtFirstName.Text;
u.last_name = txtLastName.Text;
u.email = txtEmail.Text;
u.username = txtUsername.Text;
u.password = txtPassword.Text;
u.contact = txtContact.Text;
u.address = txtContact.Text;
u.gender = cmbGender.Text;
u.user_type = cmbUserType.Text;
u.added_date = DateTime.Now;
u.added_by = 1;
/*Updatind data into database*/
bool success = dal.Update(u);

/*if data is updated successfully then the value of success will be true or false*/
if(success ==true)
{
MessageBox.Show("User successfully updated");
clear();
}
else
{
MessageBox.Show("Failed to update user");
}
/*Refreshing data grid view*/

DataTable dt = dal.Select();
dgvUsers.DataSource = dt;

private void btnDelete_Click(object sender, EventArgs e)


{
/*Getting User ID from Form*/
u.id = Convert.ToInt32(txtUserID.Text);

bool success = dal.Delete(u);


/* if data is deleted then the value of success will be true else it will be false*/
if (success == true)
{
MessageBox.Show("User deleted successfully");
}
else
{
MessageBox.Show("Failed to delete user");
}

DataTable dt = dal.Select();
dgvUsers.DataSource = dt;
}

private void txtSearch_TextChanged(object sender, EventArgs e)


{
/*Get Keyword from text box*/
string keywords = txtSearch.Text;

/*Check if the keywords has value or not*/


if(keywords!=null)
{
/*Show user based on keywords*/
DataTable dt = dal.Search(keywords);
dgvUsers.DataSource = dt;
}
else
{
/*show all users from the database*/
DataTable dt = dal.Select();
dgvUsers.DataSource = dt;
}
}
}
}

Program.cs

using AnyStore.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyStore
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmLogin());
}
}
}

You might also like