Click here to Skip to main content
15,879,474 members
Articles / Web Development / ASP.NET

Flash ASP.NET AdRotator using Database

Rate me:
Please Sign up or sign in to vote.
3.52/5 (14 votes)
5 Apr 2008CPOL3 min read 184.7K   3.9K   53   14
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.

AttributeDescription
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.

SQL
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.NET
<asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/myAds.xml"/>

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

C#
FlashAdRotator1.DataSource = GetAds(AdCount); 
FlashAdRotator1.DataBind();

Create Next and Previous buttons....

C#
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:

C#
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)


Written By
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

 
Questiongood, thanks Pin
Member 1134525221-Sep-17 3:21
Member 1134525221-Sep-17 3:21 
GeneralMy vote of 3 Pin
Member 1041470922-May-14 21:06
Member 1041470922-May-14 21:06 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey16-Feb-12 0:34
professionalManoj Kumar Choubey16-Feb-12 0:34 
GeneralMy vote of 3 Pin
milibhagat14-Jul-10 23:35
milibhagat14-Jul-10 23:35 
GeneralMy vote of 1 Pin
mukeshvalera4-Apr-10 22:44
mukeshvalera4-Apr-10 22:44 
GeneralExcellent Helper on the CodeProject Pin
Ilyas Shigri15-Feb-10 1:58
Ilyas Shigri15-Feb-10 1:58 
GeneralRe: Excellent Helper on the CodeProject Pin
prasadraja073-Jun-10 13:36
prasadraja073-Jun-10 13:36 
GeneralBrowser (iexplore) eats up memory each time the swf is loaded/unloaded Pin
denniswong2881-Jul-09 4:15
denniswong2881-Jul-09 4:15 
JokeClick on flash adrotator solution for asp .NET Pin
neviofr14-Mar-09 12:45
neviofr14-Mar-09 12:45 
QuestionIs it possible to XML file instead??? Pin
alex_brambila2-Feb-09 11:02
alex_brambila2-Feb-09 11:02 
GeneralIt is very good!! Pin
JLKEngine00823-Sep-08 18:51
JLKEngine00823-Sep-08 18:51 
GeneralWhen I click the flash, it does not take me to the navigation URL Pin
Jun Wu21-Apr-08 6:05
Jun Wu21-Apr-08 6:05 
GeneralAdRotator.dll Pin
clenz7-Apr-08 4:48
clenz7-Apr-08 4:48 
GeneralRe: AdRotator.dll Pin
Pramod S Kumar7-Apr-08 6:00
Pramod S Kumar7-Apr-08 6:00 

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.