Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Alternative to Microsfot Ad Rotator

2.60/5 (3 votes)
6 Sep 2005CPOL 1   131  
A class that returns the ad details from an XML file.

Sample Image - AlternativeToMsAdRotator_img.jpg

Introduction

This is a simple ad rotator class that reads an XML file and returns the details of the ad. Microsoft Ad Rotator control is good, but it is not very flexible to use. I wanted a way to be able to have access to the URL, image, or text of my ad at certain times which Ad Rotator can't do. Also, the MS Ad Rotator stretches the image, which doesn't look really good all the time.

What is required?

The XML file which has the details of the ads, an Image control, a Label, and a Hyperlink control.

Using the Code

C#
namespace RahmanAdRotator
{
    public class AdRotator 
    {
        private string url;
        private string img;
        private string text;
        private string xfile;
        private int thisAd;

        public AdRotator(string xmlFile,int frequency)
        {
        }

        //construction that gets the xml file from the user
        public AdRotator(string xmlFile)
        {
            xfile=xmlFile;
            generateRandom();
            readXML();
        }

        public string getURL()
        {
            return url;
        }

        public string getImage()
        {
            return img;
        }

        public string getText()
        {
            return text;
        }
        //this sub generate a random no as the random Ad
        private void generateRandom()
        {
            int ctr = totalRecords();
            Random rnd = new Random();
            thisAd = rnd.Next(1,ctr+1);
        }
        //this sub counts total number of records from the xml file.
        private int totalRecords()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(xfile);
            int ctr=ds.Tables[0].Rows.Count;
            return ctr;
        }
        //this sub reads the xml file and loops through the records 
        //and matchs the random no with the record. If matched then return it.
        private void readXML()
        {
            DataSet ds = new DataSet();

            ds.ReadXml(xfile);

            int ctr=1;
            //These Column names are standard Microsoft column 
            //names for ad rotator control eg ImageURL    
            foreach(DataRow row in ds.Tables[0].Rows)
            {
                if (ctr==thisAd)
                {
                    img = row["ImageURL"].ToString();
                    url = row["NavigateUrl"].ToString();
                    text = row["AlternateText"].ToString();
                    break;
                }
                else
                {
                    ctr = ctr+1;
                }
            }
        }
    }
}

Testing the Code

C#
private void Page_Load(object sender, System.EventArgs e)
{
    //Set your own path!
    string imgPath = @"c:\inetpub\wwwroot\MobileShop\MobileImages\Nokia\";
    //Create an instance of the Adrotator and pass the path of the xml file
    AdRotator ad = new AdRotator(@"c:\inetpub\wwwroot\MobileShop\Adverts.xml");
    //use the methods to get the details of the add
    Image1.ImageUrl = imgPath + ad.getImage();
    Label1.Text = ad.getText();
}

Improvements

I ignored the frequency of the ad as Microsoft has it in the Ad Rotator control and it can be added. There could be a lot more options that could be added! It is just an example of doing it!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)