CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2012
    Posts
    1

    Question 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
    Attached Images Attached Images  

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: OOP using ADO.NET

    [Moved Thread]
    Not related to ASP.NET ....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured