p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 (http://p2p.wrox.com/forumdisplay.php?f=264)
-   -   Additional page to my store (http://p2p.wrox.com/showthread.php?t=63309)

retroviz November 5th, 2007 07:28 PM

John,

I expect you have failed to update one of your department methods with the additional CategoryID field. I have done this quite a few times in the past where you may add a property to your class but then forget to pass the variable in your associated methods i.e. you either miss a value or try and cast as the wrong datatype.

I would just double check your departments methods and make sure you have included the value for CategoryID where appropriate.

Hope that helps.



d-print November 6th, 2007 05:29 AM

i have been looking through my methods now for about 4 hours but again to no avail, i just cannot see the problem. i have posted my StoreProvider below to see if anyone here can spot an error in my methods.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace MB.TheBeerHouse.DAL
{
   public abstract class StoreProvider : DataAccess
   {
      static private StoreProvider _instance = null;
      /// <summary>
      /// Returns an instance of the provider type specified in the config file
      /// </summary>
      static public StoreProvider Instance
      {
         get
         {
            if (_instance == null)
               _instance = (StoreProvider)Activator.CreateInstance(
                  Type.GetType(Globals.Settings.Store.ProviderType)) ;
            return _instance;
         }
      }

      public StoreProvider()
      {
         this.ConnectionString = Globals.Settings.Store.ConnectionString;
         this.EnableCaching = Globals.Settings.Store.EnableCaching;
         this.CacheDuration = Globals.Settings.Store.CacheDuration;
      }

      // methods that work with stCategories
      public abstract List<StCategoryDetails> GetStCategories();
      public abstract StCategoryDetails GetStCategoriesByID(int stCategoryID);
      public abstract bool DeleteStCategories(int stCategoryID);
      public abstract bool UpdateStCategories(StCategoryDetails stCategories);
      public abstract int InsertStCategories(StCategoryDetails stCategories);

      // methods that work with departments
      public abstract List<DepartmentDetails> GetDepartments();
      public abstract DepartmentDetails GetDepartmentByID(int departmentID);
      public abstract DepartmentDetails GetDepartmentByStCategoryID(int stCategoryID);
      public abstract bool DeleteDepartment(int departmentID);
      public abstract bool UpdateDepartment(DepartmentDetails department);
      public abstract int InsertDepartment(DepartmentDetails department);

      // methods that work with order statuses
      public abstract List<OrderStatusDetails> GetOrderStatuses();
      public abstract OrderStatusDetails GetOrderStatusByID(int orderStatusID);
      public abstract bool DeleteOrderStatus(int orderStatusID);
      public abstract bool UpdateOrderStatus(OrderStatusDetails orderStatus);
      public abstract int InsertOrderStatus(OrderStatusDetails orderStatus);

      // methods that work with shipping methods
      public abstract List<ShippingMethodDetails> GetShippingMethods();
      public abstract ShippingMethodDetails GetShippingMethodByID(int orderStatusID);
      public abstract bool DeleteShippingMethod(int shippingMethodID);
      public abstract bool UpdateShippingMethod(ShippingMethodDetails shippingMethod);
      public abstract int InsertShippingMethod(ShippingMethodDetails shippingMethod);

      // methods that work with products
      public abstract List<ProductDetails> GetProducts(string sortExpression, int pageIndex, int pageSize);
      public abstract List<ProductDetails> GetProducts(int departmentID, string sortExpression, int pageIndex, int pageSize);
      public abstract int GetProductCount();
      public abstract int GetProductCount(int departmentID);
      public abstract ProductDetails GetProductByID(int productID);
      public abstract bool DeleteProduct(int productID);
      public abstract bool UpdateProduct(ProductDetails product);
      public abstract int InsertProduct(ProductDetails product);
      public abstract bool RateProduct(int productID, int rating);
      public abstract bool DecrementProductUnitsInStock(int productID, int quantity);
      public abstract string GetProductDescription(int productID);

      // methods that work with orders
      public abstract List<OrderDetails> GetOrders(int statusID, DateTime fromDate, DateTime toDate);
      public abstract List<OrderDetails> GetOrders(string addedBy);
      public abstract OrderDetails GetOrderByID(int orderID);
      public abstract bool DeleteOrder(int orderID);
      public abstract int InsertOrder(OrderDetails order);
      public abstract bool UpdateOrder(OrderDetails order);

      // methods that work with order items
      public abstract List<OrderItemDetails> GetOrderItems(int orderID);
      public abstract int InsertOrderItem(OrderItemDetails orderItem);

      /// <summary>
      /// Returns a valid sort expression for the products
      /// </summary>
      protected virtual string EnsureValidProductsSortExpression(string sortExpression)
      {
         if (string.IsNullOrEmpty(sortExpression))
            return "tbh_Products.Title ASC";

         string sortExpr = sortExpression.ToLower();
         if (!sortExpr.Equals("unitprice") && !sortExpr.Equals("unitprice asc") && !sortExpr.Equals("unitprice desc") &&
            !sortExpr.Equals("discountpercentage") && !sortExpr.Equals("discountpercentage asc") && !sortExpr.Equals("discountpercentage desc") &&
            !sortExpr.Equals("addeddate") && !sortExpr.Equals("addeddate asc") && !sortExpr.Equals("addeddate desc") &&
            !sortExpr.Equals("addedby") && !sortExpr.Equals("addedby asc") && !sortExpr.Equals("addedby desc") &&
            !sortExpr.Equals("unitsinstock") && !sortExpr.Equals("unitsinstock asc") && !sortExpr.Equals("unitsinstock desc") &&
            !sortExpr.Equals("title") && !sortExpr.Equals("title asc") && !sortExpr.Equals("title desc"))
         {
            sortExpr = "title asc";
         }
         if (!sortExpr.StartsWith("tbh_products"))
            sortExpr = "tbh_products." + sortExpr;
         if (!sortExpr.StartsWith("tbh_products.title"))
            sortExpr += ", tbh_products.title asc";
         return sortExpr;
      }

      /// <summary>
      /// Returns a new StCategoryDetails instance filled with the DataReader's current record data
      /// </summary>
      protected virtual StCategoryDetails GetStCategoriesFromReader(IDataReader reader)
      {
          return new StCategoryDetails(
             (int)reader["StCategoryID"],
             (DateTime)reader["AddedDate"],
             reader["AddedBy"].ToString(),
             reader["Title"].ToString(),
             (int)reader["Importance"],
             reader["Description"].ToString(),
             reader["ImageUrl"].ToString());
      }

      /// <summary>
      /// Returns a new DepartmentDetails instance filled with the DataReader's current record data
      /// </summary>
      protected virtual DepartmentDetails GetDepartmentFromReader(IDataReader reader)
      {
         return new DepartmentDetails(
            (int)reader["DepartmentID"],
            (DateTime)reader["AddedDate"],
            reader["AddedBy"].ToString(),
            reader["Title"].ToString(),
            (int)reader["Importance"],
            reader["Description"].ToString(),
            reader["ImageUrl"].ToString(),
            (int)reader["StCategoryID"]);
      }

      /// <summary>
      /// Returns a collection of StCategoryDetails objects with the data read from the input DataReader
      /// </summary>
      protected virtual List<StCategoryDetails> GetStCategoriesCollectionFromReader(IDataReader reader)
      {
          List<StCategoryDetails> stCategories = new List<StCategoryDetails>();
          while (reader.Read())
              stCategories.Add(GetStCategoriesFromReader(reader) );
          return stCategories;
      }

      /// <summary>
      /// Returns a collection of DepartmentDetails objects with the data read from the input DataReader
      /// </summary>
      protected virtual List<DepartmentDetails> GetDepartmentCollectionFromReader(IDataReader reader)
      {
         List<DepartmentDetails> departments = new List<DepartmentDetails>();
         while (reader.Read())
            departments.Add(GetDepartmentFromReader(reader));
         return departments;
      }

      /// <summary>
      /// Returns a new OrderStatusDetails instance filled with the DataReader's current record data
      /// </summary>
      protected virtual OrderStatusDetails GetOrderStatusFromReader(IDataReader reader)
      {
         return new OrderStatusDetails(
            (int)reader["OrderStatusID"],
            (DateTime)reader["AddedDate"],
            reader["AddedBy"].ToString(),
            reader["Title"].ToString());
      }

      /// <summary>
      /// Returns a collection of OrderStatusDetails objects with the data read from the input DataReader
      /// </summary>
      protected virtual List<OrderStatusDetails> GetOrderStatusCollectionFromReader(IDataReader reader)
      {
         List<OrderStatusDetails> orderStatuses = new List<OrderStatusDetails>();
         while (reader.Read())
            orderStatuses.Add(GetOrderStatusFromReader(reader) );
         return orderStatuses;
      }

      /// <summary>
      /// Returns a new ShippingMethodDetails instance filled with the DataReader's current record data
      /// </summary>
      protected virtual ShippingMethodDetails GetShippingMethodFromReader(IDataReader reader)
      {
         return new ShippingMethodDetails(
            (int)reader["ShippingMethodID"],
            (DateTime)reader["AddedDate"],
            reader["AddedBy"].ToString(),
            reader["Title"].ToString(),
            (decimal)reader["Price"]);
      }

      /// <summary>
      /// Returns a collection of ShippingMethodDetails objects with the data read from the input DataReader
      /// </summary>
      protected virtual List<ShippingMethodDetails> GetShippingMethodCollectionFromReader(IDataReader reader)
      {
         List<ShippingMethodDetails> shippingMethods = new List<ShippingMethodDetails>();
         while (reader.Read())
            shippingMethods.Add(GetShippingMethodFromReader(re ader));
         return shippingMethods;
      }

      /// <summary>
      /// Returns a new ProductDetails instance filled with the DataReader's current record data
      /// </summary>
      protected virtual ProductDetails GetProductFromReader(IDataReader reader)
      {
         return GetProductFromReader(reader, true);
      }
      protected virtual ProductDetails GetProductFromReader(IDataReader reader, bool readDescription)
      {
         ProductDetails product = new ProductDetails(
            (int)reader["ProductID"],
            (DateTime)reader["AddedDate"],
            reader["AddedBy"].ToString(),
            (int)reader["DepartmentID"],
            reader["DepartmentTitle"].ToString(),
            reader["Title"].ToString(),
            null,
            reader["SKU"].ToString(),
            (decimal)reader["UnitPrice"],
            (int)reader["DiscountPercentage"],
            (int)reader["UnitsInStock"],
            reader["SmallImageUrl"].ToString(),
            reader["FullImageUrl"].ToString(),
            (int)reader["Votes"],
            (int)reader["TotalRating"]);

         if (readDescription)
            product.Description = reader["Description"].ToString();

         return product;
      }

      /// <summary>
      /// Returns a collection of ProductDetails objects with the data read from the input DataReader
      /// </summary>
      protected virtual List<ProductDetails> GetProductCollectionFromReader(IDataReader reader)
      {
         return GetProductCollectionFromReader(reader, true);
      }
      protected virtual List<ProductDetails> GetProductCollectionFromReader(IDataReader reader, bool readDescription)
      {
         List<ProductDetails> products = new List<ProductDetails>();
         while (reader.Read())
            products.Add(GetProductFromReader(reader, readDescription));
         return products;
      }

      /// <summary>
      /// Returns a new OrderItemDetails instance filled with the DataReader's current record data
      /// </summary>
      protected virtual OrderItemDetails GetOrderItemFromReader(IDataReader reader)
      {
         return new OrderItemDetails(
            (int)reader["OrderItemID"],
            (DateTime)reader["AddedDate"],
            reader["AddedBy"].ToString(),
            (int)reader["OrderID"],
            (int)reader["ProductID"],
            reader["Title"].ToString(),
            reader["SKU"].ToString(),
            (decimal)reader["UnitPrice"],
            (int)reader["Quantity"]);
      }

      /// <summary>
      /// Returns a collection of OrderItemDetails objects with the data read from the input DataReader
      /// </summary>
      protected virtual List<OrderItemDetails> GetOrderItemCollectionFromReader(IDataReader reader)
      {
         List<OrderItemDetails> orderItems = new List<OrderItemDetails>();
         while (reader.Read())
            orderItems.Add(GetOrderItemFromReader(reader));
         return orderItems;
      }

      /// <summary>
      /// Returns a new OrderDetails instance filled with the DataReader's current record data
      /// </summary>
      protected virtual OrderDetails GetOrderFromReader(IDataReader reader)
      {
         return new OrderDetails(
            (int)reader["OrderID"],
            (DateTime)reader["AddedDate"],
            reader["AddedBy"].ToString(),
            (int)reader["StatusID"],
            reader["StatusTitle"].ToString(),
            reader["ShippingMethod"].ToString(),
            (decimal)reader["SubTotal"],
            (decimal)reader["Shipping"],
            reader["ShippingFirstName"].ToString(),
            reader["ShippingLastName"].ToString(),
            reader["ShippingStreet"].ToString(),
            reader["ShippingPostalCode"].ToString(),
            reader["ShippingCity"].ToString(),
            reader["ShippingState"].ToString(),
            reader["ShippingCountry"].ToString(),
            reader["CustomerEmail"].ToString(),
            reader["CustomerPhone"].ToString(),
            reader["CustomerFax"].ToString(),
            (reader["ShippedDate"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["ShippedDate"]),
            reader["TransactionID"].ToString(),
            reader["TrackingID"].ToString());
      }

      /// <summary>
      /// Returns a collection of OrderDetails objects with the data read from the input DataReader
      /// </summary>
      protected virtual List<OrderDetails> GetOrderCollectionFromReader(IDataReader reader)
      {
         List<OrderDetails> orders = new List<OrderDetails>();
         while (reader.Read())
            orders.Add(GetOrderFromReader(reader));
         return orders;
      }
   }
}


cheers
john


retroviz November 6th, 2007 06:01 AM

John,

The cause of this error may not be in your StoreProvider class. As previously said, it is more likely one of the methods from either your BLL or DAL classes causing this. I do not understand why you are having this problem when viewing products as this should not have changed. What happens when you manually enter the URL i.e. "products.aspx?DepartmentID=#" Do you still get the error. I suggest you put a pause/break in your code and Debug. You can step through by pressing F8 and this should show the method thats causing the problem.


d-print November 6th, 2007 07:10 AM

ok, i have passed the ID and i get the same error, i have tried tracing the error in the method but again i am struggling. your years experience far out strips my 3 months experience so i wondered if you could have a look at my methods? i have posted my Department.cs her and also my DepartmentDetails on another post.
Can you see the error?
i wondered if it was to do with having a DepartmentsDetails section twice in my StoreProvider?

DEPARTMENT.CS:-

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using MB.TheBeerHouse.DAL;

namespace MB.TheBeerHouse.BLL.Store
{
   public class Department : BaseStore
   {
      private string _title = "";
      public string Title
      {
         get { return _title; }
         set { _title = value; }
      }

      private int _stCategoryID = 0;
      public int StCategoryID
      {
          get { return _stCategoryID; }
          set { _stCategoryID = value; }
      }

      private int _importance = 0;
      public int Importance
      {
         get { return _importance; }
         private set { _importance = value; }
      }

      private string _description = "";
      public string Description
      {
         get { return _description; }
         set { _description = value; }
      }

      private string _imageUrl = "";
      public string ImageUrl
      {
         get { return _imageUrl; }
         set { _imageUrl = value; }
      }

      private List<Product> _allProducts = null;
      public List<Product> AllProducts
      {
         get
         {
            if (_allProducts == null)
               _allProducts = Product.GetProducts(this.ID, "", 0, BizObject.MAXROWS);
            return _allProducts;
         }
      }

      public Department(int id, int stCategoryid, DateTime addedDate, string addedBy, string title, int importance, string description, string imageUrl)
      {
         this.ID = id;
         this.StCategoryID = id;
         this.AddedDate = addedDate;
         this.AddedBy = addedBy;
         this.Title = title;
         this.Importance = importance;
         this.Description = description;
         this.ImageUrl = imageUrl;
      }

      public bool Delete()
      {
         bool success = Department.DeleteDepartment(this.ID);
         if (success)
            this.ID = 0;
         return success;
      }

      public bool Update()
      {
         return Department.UpdateDepartment(this.ID, this.StCategoryID, this.Title, this.Importance, this.Description, this.ImageUrl);
      }

      /***********************************
      * Static methods
      ************************************/

      /// <summary>
      /// Returns a collection with all the departments
      /// </summary>
      public static List<Department> GetDepartments()
      {
         List<Department> departments = null;
         string key = "Store_Departments";

         if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
         {
            departments = (List<Department>)BizObject.Cache[key];
         }
         else
         {
            List<DepartmentDetails> recordset = SiteProvider.Store.GetDepartments();
            departments = GetDepartmentListFromDepartmentDetailsList(records et);
            BaseStore.CacheData(key, departments);
         }
         return departments;
      }

      /// <summary>
      /// Returns a Department object with the specified ID
      /// </summary>
      public static Department GetDepartmentByID(int departmentID)
      {
         Department department = null;
         string key = "Store_Department_" + departmentID.ToString();

         if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
         {
            department = (Department)BizObject.Cache[key];
         }
         else
         {
            department = GetDepartmentFromDepartmentDetails(SiteProvider.St ore.GetDepartmentByID(departmentID));
            BaseStore.CacheData(key, department);
         }
         return department;
      }

      /// <summary>
      /// Updates an existing department
      /// </summary>
      public static bool UpdateDepartment(int id, int stCategoryID, string title, int importance, string description, string imageUrl)
      {
         DepartmentDetails record = new DepartmentDetails(id, DateTime.Now, "", title, importance, description, imageUrl, stCategoryID);
         bool ret = SiteProvider.Store.UpdateDepartment(record);
         BizObject.PurgeCacheItems("store_department");
         return ret;
      }

      /// <summary>
      /// Deletes an existing department
      /// </summary>
      public static bool DeleteDepartment(int id)
      {
         bool ret = SiteProvider.Store.DeleteDepartment(id);
         new RecordDeletedEvent("department", id, null).Raise();
         BizObject.PurgeCacheItems("store_department");
         return ret;
      }

      /// <summary>
      /// Creates a new department
      /// </summary>
      public static int InsertDepartment(string title, int importance, string description, string imageUrl, int stCategoryID)
      {
          DepartmentDetails record = new DepartmentDetails(0, DateTime.Now,
            BizObject.CurrentUserName, title, importance, description, imageUrl, stCategoryID);
         int ret = SiteProvider.Store.InsertDepartment(record);
         BizObject.PurgeCacheItems("store_department");
         return ret;
      }

      /// <summary>
      /// Returns a Department object filled with the data taken from the input DepartmentDetails
      /// </summary>
      private static Department GetDepartmentFromDepartmentDetails(DepartmentDetai ls record)
      {
         if (record == null)
            return null;
         else
         {
            return new Department(record.ID, record.StCategoryID, record.AddedDate, record.AddedBy, record.Title, record.Importance, record.Description, record.ImageUrl);
         }
      }

      /// <summary>
      /// Returns a list of Department objects filled with the data taken from the input list of DepartmentDetails
      /// </summary>
      private static List<Department> GetDepartmentListFromDepartmentDetailsList(List<De partmentDetails> recordset)
      {
         List<Department> departments = new List<Department>();
         foreach (DepartmentDetails record in recordset)
            departments.Add(GetDepartmentFromDepartmentDetails (record));
         return departments;
      }

      internal static List<Department> GetDepartments(int p, string p_2, int p_3, int p_4)
      {
          throw new Exception("The method or operation is not implemented.");
      }


      /// <summary>
      /// Returns a Department object with the specified StCategoryID
      /// </summary>
      public static Department GetDepartmentByStCategoryID(int stCategoryID)
      {
          Department department = null;
          string key = "Store_Department_" + stCategoryID.ToString();

          if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
          {
              department = (Department)BizObject.Cache[key];
          }
          else
          {
              department = GetDepartmentFromDepartmentDetails(SiteProvider.St ore.GetDepartmentByStCategoryID(stCategoryID));
              BaseStore.CacheData(key, department);
          }
          return department;
      }


  }
}


JOHN


d-print November 6th, 2007 07:10 AM

DEPARTMENTDETAILS.CS:-

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MB.TheBeerHouse.DAL
{
   public class DepartmentDetails
   {
      public DepartmentDetails() { }

      public DepartmentDetails(int id, DateTime addedDate, string addedBy, string title, int importance, string description, string imageUrl, int stCategoryID)
      {
         this.ID = id;
         this.AddedDate = addedDate;
         this.AddedBy = addedBy;
         this.Title = title;
         this.Importance = importance;
         this.Description = description;
         this.ImageUrl = imageUrl;
         this.StCategoryID = id;
      }

      private int _id = 0;
       public int ID
       {
           get { return _id;}
           set { _id = value;}
       }

      private DateTime _addedDate = DateTime.Now;
      public DateTime AddedDate
      {
         get { return _addedDate; }
         set { _addedDate = value; }
      }

      private string _addedBy = "";
      public string AddedBy
      {
         get { return _addedBy; }
         set { _addedBy = value; }
      }

      private string _title = "";
      public string Title
      {
         get { return _title; }
         set { _title = value; }
      }

      private int _importance = 0;
      public int Importance
      {
         get { return _importance; }
         set { _importance = value; }
      }

      private string _description = "";
      public string Description
      {
         get { return _description; }
         set { _description = value; }
      }

      private string _imageUrl = "";
      public string ImageUrl
      {
         get { return _imageUrl; }
         set { _imageUrl = value; }
      }

      private int _stCategoryid = 0;
      public int StCategoryID
      {
          get { return _stCategoryid; }
          set { _stCategoryid = value; }
      }
   }
}

JOHN


d-print November 6th, 2007 09:30 AM

I have gone through all of my coding for the deps page & all of the classes etc and it appears that the error is being thrown by the ProductListing Control, inparticular the Dropdown menu that allows you to have a list of all departments. 'GetDepartments'
there appears to be an error with the page trying to get the ID of both the department & category.

Does this sound likely to you?

John


retroviz November 6th, 2007 03:10 PM

Your GetDepartments method should not have changed so not sure why you it is requesting the CategoryID on your product listing page. If you create a new .aspx page and add a DropDownList control and bind it to an object datasource using that method, do you get the same error?


d-print November 7th, 2007 04:57 AM

Retroviz,

I Have to say a massive thank-you to you for all of your help on this one.

my pages work fine now and thanks to you last post i realised where i was going wrong.
Although i was able to F5 and debug my application, the database i have the web.config file pointed to is on a live server so i was missing an StCategoryID from one of my stored procedures.

doh!

anyhow, thank-you once again :)
Kind Regards
John

P.S thanks for the book info, i have now completed about half of it and some of the functions are really cool. cheers



retroviz November 7th, 2007 06:01 PM

Hi John,

No problem, glad to help. At least if I ever want to add a shop I will already know what to do ;)

Post the URL of your site once you have it up and running so we can all see.

Another book I would recommend is Professional ASP.net 2.0 (also by Wrox). This is more of a reference book but it does cover almost every aspect of ASP.net 2.0. I have certainly found this valuable learning tool in the past.

Cheers


leonardop November 23rd, 2007 11:32 AM

My first post !!!![^]
Could you share the procedures it completes and the files?

Regards
leonardo



All times are GMT -4. The time now is 05:48 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.