Tuesday, October 26, 2010

Get product by category using commerce server

                         Recently we got a problem with in our application that is some product's bread crumb not displaying properly .I have investigated and find the problem.
Problem:       The problem is when product is associated with two categories it is always fetching from primary parent  category .
       
Example :
             when product associated with two categories A and category B .It always fetch from category A even though we request for category B product .
                    If you don't have much knowledge on Commerce server API then get from knowledge  from here.....
                             
  Runtime Commerce Server APIs (those that are used to retrieve info for use in a e-commerce website)
    * ProductCatalog: Getting products from a catalog along with its child products
    * CatalogContext.GetCatalogs: Getting a set of catalogs from the current context
    * CatalogContext.GetCategory: Getting the details of a category including child categories
    * CatalogContext.GetProduct: Getting the details of a product and product family including the variants
    * CatalogSearch: Searching the catalog with the basic properties specified
   
Here is the code for Fetching product based on product id and Category
/// <summary>
       /// Method:GetProduct
       /// Description: This method is used to get the Product Information based on productId and CategoryName .
       /// </summary>
       /// <returns>datatable</returns> 
       public static DataTable  GetProductByCategory(string currentCatalog, string categoryName, string productId, string language)
       {
           ProductCatalog virtualCatalog = CommerceContext.Current.CatalogSystem.GetCatalog(currentCatalog);
           Category virtualCatalogCategory = virtualCatalog.GetCategory(categoryName);
           DataSet dsProducts =  virtualCatalogCategory.GetProducts();  
           DataView dvFilteredProducts = dsProducts.Tables[0].DefaultView;
           dvFilteredProducts.RowFilter = "ProductId='" + productId + "'";
           return dvFilteredProducts.ToTable();
       }

Friday, October 15, 2010

Getting Browser's height and width using JavaScript which works in (IE,Firefox browsers )

                                            Getting the Browser's height and width is quite useful in many scenarios .Recently i  have got a requirement in one of the  project that is setting the background image based on  the browser width and height . But the problem is there are so many different types of browsers out there and you don't know which one the end user will be using. Another issue is the height and the width of different laptops and desktops available in the market which are having different resolutions .

                                        So most probably you are fine tuning your application based on the server or the PC or the laptop that you are using to develop. It will look perfectly fine until one day you decide to demo to your colleagues using a different laptop. All the layouts and alignments will be wrong. This happens if you hard code the height and width of the objects that you require in your site.

                                                  The best way for such applications is to get the height and the width of the client dynamically (using JavaScript) and then setting the height and the width of the objects dynamically. You can use the following JavaScript example to get the height and the width of the client.

                                             In my project requirement I need to set the back ground  right side  image based on browser width and height.So i have done this .please modify code according to your requirement.

<script type="text/javascript" language="javascript">
       window.onresize = SetGal
        function SetGal() {
       // debugger;
           var myWidth;
           var myHeight;
 
           if (typeof (window.innerWidth) == 'number') {
 
              //alert('non ie');
 
               myWidth = window.innerWidth;
               myHeight = window.innerHeight;
 
           }
           else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
            {
 
                //alert('myWidth');
 
               myWidth = document.documentElement.clientWidth;
               myHeight = document.documentElement.clientHeight;
 
           }
           else if (document.body && (document.body.clientWidth || document.body.clientHeight))
            {
 
               myWidth = document.body.clientWidth-80;
               myHeight = document.body.clientHeight;
 
           }
           //alert('gal_bg_2');
          // alert(myWidth);
           //  alert(myHeight);
           
           
      document.getElementById("gal_bg_2").style.height = myHeight + 'px';  // this element i am dynamicall setting based on browser width and height 
      document.getElementById("gal_bg_2").style.width = myWidth + 'px'; 
      
       }
 
   </script>

Wednesday, October 6, 2010

Prevent browser caching of web pages in asp.net which works in all browsers (IE/Firefox..)

In this article i am going to explain how to prevent the browser caching of web pages in asp.net .It is the one of the biggest issue
every developer face . 

      Why browser caching ?
                           To speed up user experience on the web, most browsers implement a technology called caching. Caching allows information such as WebPages, images, and so on, to be saved on a user’s computer. If the user calls for a previously requested webpage, the browser is able to access the information more quickly by recalling it from a cache, rather than making another request to the site itself.
                         One side it is a advantage but when you display sensitive information it will be a big drawback .Recently we have found one problem in our current project .that is when user log in and after some operations sing out .if user click on back button it is still displaying logged in user .Hmmm..... We have tried different ways to handle the issue .But  we have faced issues with Firefox .

                        So i have decided to write logic in master page load event .And i have added some login in logout page .Here is the code.

Place  this code in master page  in load event
HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(false);  
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);  
            HttpContext.Current.Response.Cache.SetNoStore();
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
            Response.Cache.SetValidUntilExpires(true);
 




         In Logout page Load   add this code  
  


Response.AddHeader("Pragma", "no-cache");
            Response.CacheControl = "no-cache";
            Response.Cache.SetAllowResponseInBrowserHistory(false);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            Response.Expires = -1;
            Session.Abandon();
            ClientScript.RegisterClientScriptBlock(this.GetType(),"signout", "DisableHistory()", true );


  
write this code in logout mark up page






function DisableHistory() {
 
           window.history.forward(1);
       }
       function RedirectToHome() {
 
           setTimeout("window.location = 'Index.aspx'",0);
       }
     
   </script>
 

call this  RedirectToHome method in body onload of logout page 




<body onload ="RedirectToHome();">
 
Run the application.Have a fun …