Click here to Skip to main content
13,298,965 members (41,663 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked
Posted 5 Apr 2008

Flash ASP.NET AdRotator using Database

, 5 Apr 2008
Rate this:
Please Sign up or sign in to vote.
Implement a Flash ASP.NET AdRotator to fetch ads from a database.



This article explains the concept of the ASP.NET AdRotator control fetching ad information for Flash from a database and rotating the ads with Previous/Next button clicks. This article also explains how to store Flash (.swf) files in a database and display the ads in a specific order.

Advantages of using a database

  • Instead of storing files in XML, the database is easier to maintain ads.
  • Easy to update or maintain the active list of ads using a flag in the database.
  • Add any number fields to store specific/company information about the ads.

Understanding the AdRotator

You can use the AdRotator control to display a randomly selected advertisement banner on a Web page. The displayed advertisement changes whenever the page refreshes.

Advertisement information is stored in a separate XML file. The XML file allows you to maintain a list of advertisements and their associated attributes. Attributes include the path to an image to display, the URL to link to when the control is clicked, the alternate text to display when the image is not available, a keyword, and the frequency of the advertisement. Information in this file is not validated by the AdRotator control. To prevent ads from executing malicious scripts, you should always check the data before releasing it, or accept ad information only from trusted sources.

The XML file contains the following predefined attributes. Only the ImageUrl attribute is required.

ImageUrlThe URL of the image to display.
HeightThe height of the image, in pixels (optional).
WidthThe width of the image, in pixels (optional).
NavigateUrlThe URL of the page to navigate to when the AdRotator control is clicked.
AlternateTextThe text to display if the image is unavailable. On some browsers, this text is displayed as a ToolTip.
KeywordThe category for the advertisement. This is used by the AdRotator control to filter the list of advertisements for a specific category.
ImpressionsA value that indicates how often an advertisement is displayed in relation to other advertisements in the XML file.

Use the above attributes to create fields in the database.

Create Table AdsDataTable 
  OrderId int,
 br br br br br br br br br br   AlternateText VarChar(200), 
  ImageUrl VarChar(200), 
  NavigateUrl VarChar(200),
  IsAdActive bit 

You can create as many number of fields to store metadata...for my use, I have created only the above fields.


The basic AdRotator control in .NET does not have support for Flash. So I have modified it to support Flash. The DLL I am using, AdRotator.dll, is included in the sample bin directory, with source code.

The AdRotator uses myAds.xml as the data source which is an XML file...

<asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/myAds.xml"/>

Replace AdvertisementFile with DataSource in the code-behind file Page_Load event:

FlashAdRotator1.DataSource = GetAds(AdCount); 

Create Next and Previous buttons....

protected void NextBtn_Click(object sender, ImageClickEventArgs e)
   if (ViewState["Count"]!= null) 
   AdCount = (int)ViewState["Count"];
   AdCount = AdCount + 1; 
   ViewState["Count"]= AdCount; 
   NextBtn.Enabled = AdCount == TotalAds ? false : true; 
   PrevBtn.Enabled = AdCount >= 1 ? true : false; 
   FlashAdRotator1.DataSource = GetAds(AdCount);

protected void PrevBtn_Click(object sender, ImageClickEventArgs e)
  if (ViewState["Count"] != null)
     AdCount = (int)ViewState["Count"];
  AdCount = AdCount - 1; 
  ViewState["Count"] = AdCount; 
  PrevBtn.Enabled = AdCount == 1 ? false : true; 
  NextBtn.Enabled = AdCount <= TotalAds ? true : false; 
  FlashAdRotator1.DataSource = GetAds(AdCount); 

Fetch ads from the database:

private DataTable GetAds(int AdNo)
 string sql = "select AlternateText,ImageUrl, " + 
              "NavigateUrl from AdsDataTable where OrderId = " + 
 SqlConnection con = new SqlConnection(
 SqlDataAdapter adsa = new SqlDataAdapter(sql, con);
 DataTable AdsTable = new DataTable(); adsa.Fill(AdsTable);
 return AdsTable;

This way, users can make use of a database to store ads and categorize them based on their preference. Also, if you don't like to click, you can add a timer control to this and set the seconds to rotate the ads automatically.


The sample code will work in VS2005 with AJAX. Also I have attached the sample database MDF/LDF file.


  • April 5th 2008. Initial version published.


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


About the Author

Pramod S Kumar
Software Developer (Senior) NYCEDC
United States United States
Pramod has been a software developer for more than 6 years now mostly working on Ajax ASP.NET web applications.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey16-Feb-12 1:34
membermanoj kumar choubey16-Feb-12 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 5 Apr 2008
Article Copyright 2008 by Pramod S Kumar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid