-
1 Attachment(s)
OOP using ADO.NET
Hi,
I am new to C# and I am trying to make a windows Attachment 29983application. Three layer architecture I am trying to follow.I have two classes Product and Category.
Here is my code...............
public class Category
{
private int categoryID;
private string categoryCreatedDate;
private string categoryName;
private string categoryDescription;
public int CategoryID
{
get { return categoryID; }
set { categoryID = value; }
}
public string CategoryName
{
get { return categoryName; }
set { categoryName = value; }
}
public string CategoryDescription
{
get { return categoryDescription; }
set { categoryDescription = value; }
}
public string CategoryCreatedDate
{
get { return categoryCreatedDate; }
set { categoryCreatedDate = value; }
}
public virtual Products Products
{
get;
set;
}
}
----------------------
public class Products
{
private int categoryID;
private ProductStatus productStatus;
public int CategoryID
{
get { return categoryID; }
set { categoryID = value; }
}
private int productID;
public int ProductID
{
get { return productID; }
set { productID = value; }
}
private string productName;
public string ProductName
{
get { return productName; }
set { productName = value; }
}
private string productDescription;
public string ProductDescription
{
get { return productDescription; }
set { productDescription = value; }
}
private string productType;
public string ProductType
{
get { return productType; }
set { productType = value; }
}
private string productReason;
public string ProductReason
{
get { return productReason; }
set { productReason = value; }
}
public ProductStatus ProductStatus
{
get { return productStatus; }
set { productStatus = value; }
}
public virtual Category Category
{
get;
set;
}
}
----------------------DAL/CategoryDB----------------
public class CategoryDB
{
private OleDbCommand mycommand;
private dbconnection getConnection;
private List<Category> CategoryList;
public CategoryDB()
{
mycommand = new OleDbCommand();
getConnection = new dbconnection();
CategoryList = new List<Category>();
}
public List<Category> getCategory()
{
OleDbDataReader reader;
mycommand.Connection = getConnection.openConnection();
mycommand.CommandText = "select * from Category";
mycommand.ExecuteNonQuery();
reader = mycommand.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Category categories = new Category();
Products products = new Products();
categories.CategoryName = reader["categoryName"].ToString();
categories.CategoryDescription = reader["categoryDescription"].ToString();
categories.CategoryCreatedDate = reader["categoryCreatedDate"].ToString();
CategoryList.Add(categories);
}
}
return CategoryList;
}
}
-----------------------DAL/ProductDB----------------------------
public class ProductDB
{
private OleDbCommand mycommand;
private dbconnection getConnection;
private List<Products> productList;
public ProductDB()
{
mycommand = new OleDbCommand();
getConnection = new dbconnection();
productList = new List<Products>();
}
public List<Products> getProducts(Category category)
{
OleDbDataReader reader;
mycommand.Connection = getConnection.openConnection();
mycommand.CommandText = "select * from Product where categoryID like @categoryID";
mycommand.Parameters.AddWithValue("@categoryID", category.CategoryID);
mycommand.ExecuteNonQuery();
reader = mycommand.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Products products_obj = new Products();
products_obj.ProductID = Convert.ToInt32(reader["productID"]);
products_obj.ProductDescription = reader["productDescription"].ToString();
products_obj.ProductName = reader["productName"].ToString();
products_obj.ProductType = reader["productType"].ToString();
products_obj.ProductReason = reader["productReason"].ToString();
products_obj.CategoryID =Convert.ToInt32( reader["categoryID"]);
productList.Add(products_obj);
}
}
return productList;
}
}
--------------------------------Presentation Layer---------------
List<Products> product_list = new List<Products>();
List<Category> category_list = new List<Category>();
ProductDB prodDB_obj = new ProductDB();
Products product_obj = new Products();
By doing this I can call each others methods,
Problem is this , how to related objects.
how I can create an object of Category and get all the products in it. as I am using Product property in Category class.
Or the way I am doing is wrong.. please help. thanks in advance.Attachment 29985
-
Re: OOP using ADO.NET
[Moved Thread]
Not related to ASP.NET ....