In this article, I came up with a database logic layer for a food court project and its functionalities, and these functionalities were developed by using c# programming. Before continuing this article I strongly recommend going with my previous article i.e Overview of Food Court and Creating Database Tables and Stored Procedures.
Food Item Types Management
In the database logic file, we have two functionalities on food item types modules i.e InsertFoodItemType and GetFoodItemType functions.
The InsertFoodItemType method is used to add new food item type data into the database table i.e FoodItemTypeMaster table.
The GetFoodItemType method is used to fetch all the records from the food item type database table and return them in the form of a dataset object.
Adding Namespace
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using FoodCourtBusinessObject;
Establishing SQL Connection
If we need to perform any operation on a database we need to open a connection with that database, below code does this work.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCon"].ToString());
This InsertFoodItemType method is used to add new food item types to the database table i.e fooditemtype. This method accepts a parameter called BusinessObjects class object and it contains food item type name value to store this data in the database table.
namespace FoodCourtDatabaseLogic
{
public class DataBaseLogic
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCon"].ToString());
#region ADD Food Item Type to Database
public int InsertFoodItemType(BusinessObject boObj)
{
SqlCommand cmd = new SqlCommand("Insert_FoodItemTypeMaster", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@fitemname", boObj.FoodItemTypeName);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
#endregion
}
}
The below code is used to fetch all the food item type data and bind that data into the dataset because this GetFoodItemType method returns the dataset object as output.
#region Bind Food Item Type to Form
public DataSet GetIFoodtemType()
{
SqlCommand cmd = new SqlCommand("proc_getFoodItemType", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
#endregion
Food Items Management
In this food items database logic, we have various functionalities which perform operations on food item tables, like adding food items, getting all food items and getting food item prices and quantity.
#region ADD Food Items to Database
public int InsertFoodItems(BusinessObject boObject)
{
SqlCommand cmd = new SqlCommand("proc_InsertFoodItem", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@itemname", boObject.FoodItemName);
cmd.Parameters.AddWithValue("@itemqty", boObject.Quantity);
cmd.Parameters.AddWithValue("@itemprice", boObject.Price);
cmd.Parameters.AddWithValue("@fitemtypeno", boObject.FoodItemTypeNo);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
#endregion
The above InsertFoodItems method is used to add new food items to the database table, if it has inserted data successfully into the database then it will return 1 as output if in case failed its will return -1.
#region Bind Food Item Based on Food Item Types
public DataSet GetFoodItemsName(BusinessObject Obj)
{
SqlCommand cmd = new SqlCommand("proc_getFoodItemNames", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FoodItemType", Obj.FoodItemTypeNo);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
#endregion
The above GetFoodItemsName method is used to fetch all the food item name data by passing a particular food item type ID, and binding that fetched data into the dataset object and the return type of this method is dataset.
#region Get Price of Food Item
public double GetFooditemPrice(BusinessObject boObj)
{
SqlCommand cmd = new SqlCommand("proc_getItemPrice", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@itemno", boObj.FoodItemNo);
cmd.Parameters.AddWithValue("@itemtypeno", boObj.FoodItemTypeNo);
con.Open();
double price = Convert.ToDouble(cmd.ExecuteScalar());
con.Close();
return price;
}
#endregion
In this above code snippet, a method is defined and that method return type i.e GetFoodItemPrice method is double because this method returns only the price value of a particular food item while passing fooditem id and fooditemtype id.
In this below code snippet, DisplayQtyPriceFoodItem method is used to fetch the price and quantity of the particular food item data while passing fooditem ID value and fetched data binding into the dataset because the return type of this method is dataset.
#region Display Quantity and Price In Food Item
public DataSet DisplayQtyPriceFoodItem(BusinessObject QtyPricObject)
{
SqlCommand cmd = new SqlCommand("proc_getFoodItem_Qty_Price", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@itemno", QtyPricObject.FoodItemNo);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
#endregion
To get more information regarding blood donar project source code like Business Object, Database & Stored Procedure, Database Logic and User Interface.
0 Comments