Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Adrotator.JPG

Introduction

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.

Attribute Description
ImageUrl The URL of the image to display.
Height The height of the image, in pixels (optional).
Width The width of the image, in pixels (optional).
NavigateUrl The URL of the page to navigate to when the AdRotator control is clicked.
AlternateText The text to display if the image is unavailable. On some browsers, this text is displayed as a ToolTip.
Keyword The category for the advertisement. This is used by the AdRotator control to filter the list of advertisements for a specific category.
Impressions A 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.

Implementation

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); 
FlashAdRotator1.DataBind();

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);
   FlashAdRotator1.DataBind(); 
}

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); 
  FlashAdRotator1.DataBind(); 
}

Fetch ads from the database:

private DataTable GetAds(int AdNo)
{ 
 string sql = "select AlternateText,ImageUrl, " + 
              "NavigateUrl from AdsDataTable where OrderId = " + 
              AdNo.ToString(); 
 SqlConnection con = new SqlConnection(
   ConfigurationManager.ConnectionStrings["AdsConnectionString"].ToString()); 
 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.

Platform/Installation

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

History

  • April 5th 2008. Initial version published.

License

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

Share

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.

Comments and Discussions

 
GeneralMy vote of 3 PinmemberMember 1041470922-May-14 21:06 
GeneralMy vote of 5 Pinmembermanoj kumar choubey16-Feb-12 0:34 
GeneralMy vote of 3 Pinmembermilibhagat14-Jul-10 23:35 
presentation is nice well i have not seen it working yet.hopefully this may works upto my expectation
GeneralMy vote of 1 Pinmembervaleramukesh@gmail.com4-Apr-10 22:44 
GeneralExcellent Helper on the CodeProject PinmemberIlyas Shigri15-Feb-10 1:58 
GeneralRe: Excellent Helper on the CodeProject Pinmemberprasadraja073-Jun-10 13:36 
GeneralBrowser (iexplore) eats up memory each time the swf is loaded/unloaded Pinmemberdenniswong2881-Jul-09 4:15 
JokeClick on flash adrotator solution for asp .NET Pinmemberneviofr14-Mar-09 12:45 
QuestionIs it possible to XML file instead??? Pinmemberalex_brambila2-Feb-09 11:02 
GeneralIt is very good!! PinmemberJLKEngine00823-Sep-08 18:51 
GeneralWhen I click the flash, it does not take me to the navigation URL PinmemberJun Wu21-Apr-08 6:05 
GeneralAdRotator.dll Pinmemberclenz7-Apr-08 4:48 
GeneralRe: AdRotator.dll PinmemberPramod S Kumar7-Apr-08 6:00 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 5 Apr 2008
Article Copyright 2008 by Pramod S Kumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid