Saturday, March 13, 2010

Rotating ads automatically for every 10 minutes in ASP.Net C#

I got an assignment to add a feature that rotates ads on ByJoel.com home page. This website is built on ASP.net and items displayed are not only just images but also information stored in database.


Whats the need to do this??
- Because when person visits www.ByJoel.com he see one set of items and if he hit the home page again after 10 minutes he sees different items in different departments. This features keeps the website refreshed with different items every 10 minutes As this is a E-commerce website so this feature is useful to attract customers

Logic:
There is UserControl file which sets the number of items to be displayed on home page ex: OnSale.ascx.cs, (C sharp codes are in separate file)
There is variable "howMany" which sets the number of items,


if (howMany == 8) 

How to fetch the time from server and extracting minutes and converting it to String??

DateTime tm = DateTime.Now;  // fetch the server time
curtime = String.Format("{0:mm}", tm); //select only minutes Note: "curtime" variable has to declared in this class 
time = Convert.ToInt32(curtime);  // converting to string note: "time" variable declared before.








Declare another string variable called tempTime.

if (time >= 00 && time <= 10) // time between 1 to 10 minutes at any hour. 
              tempTime = 1;
            else






                if (time >= 11 && time <= 20)
                    tempTime = 2;
                else
                    if (time >= 21 && time <= 30)
                        tempTime = 3;
                    else
                        if (time >= 31 && time <= 40)
                            tempTime = 4;
                        else
                            if (time >= 41 && time <= 50)
                                tempTime = 5;
                            else
                                   tempTime = 6;
        

Add time variables are in Access.cs file, this file used to access stored procedure for our website.

Then go to stored procedure and add the queries for different time slot. you can add any query to pull the desired items, TOP(8) means we will get only top 8 rows.

ex:
IF (@tempTime = 1)
 SELECT ASIN, StockStatus, ShowOnHomePage
 FROM OnSale
 WHERE (ShowOnHomePage > 0)
 ORDER BY ShowOnHomePage
 
IF (@tempTime = 2)
 SELECT TOP (8) DepartmentID, ASIN, Page, StockStatus
 FROM OnSale
 WHERE (DepartmentID = 1) AND (Page = 4)
 ORDER BY StockStatus DESC  
 
IF (@tempTime = 3)
 SELECT TOP (8) DepartmentID, ASIN, StockStatus
 FROM OnSale
 WHERE (DepartmentID = 2)
 ORDER BY StockStatus DESC
 
IF (@tempTime = 4)
 SELECT TOP (8) DepartmentID, ASIN, Page, StockStatus
 FROM OnSale
 WHERE (DepartmentID = 1) AND (Page = 5)
 ORDER BY StockStatus DESC
 
IF (@tempTime = 5)
 SELECT TOP (8) DepartmentID, ASIN, Page, StockStatus 
 FROM OnSale
 WHERE (DepartmentID = 1) AND (Page = 2) AND (SubPage = 3)
 ORDER BY StockStatus DESC
 
IF (@tempTime = 6)
 SELECT TOP (8) ASIN,  StockStatus
 FROM OnSale
 WHERE(TopPicks > 0)
 ORDER BY StockStatus DESC




Out put:
first 10 minutes:




Next 10 minutes :

No comments:

Post a Comment