AspdotnetCodes.com
ASP.NET 3.5 Web Hosting – Click Here
 
Articles Subscribe for our Articles Updates
Books
Resources
Downloads
Free Tech Magazines
Archives
Softwares
Newsletter
Suggest Us
Link to Us
 Asp.Net AdRotator control with Database Integration
Posted by Moderator1 on 6/30/2007 12:11:33 PM Category: Asp.Net 2.0
Total Views : 11862
Add to my favorites
Email to friend
  
Introduction
This article explains the concept of Asp.Net AdRotator control fetching ad information from the database and rotates the ads on certain time interval. This article also gives a tip to fetch ad information from an XML file.
The AdRotator Control presents ad images each time a user enters or refreshes a webpage. When the ads are clicked, it will navigate to a new Web location. Each time the page is loaded into the browser, an ad is randomly selected from a predefined list. Previously the predefined list means an XML File, which contains the information about the ads to be displayed. But in Asp.Net 2.0 we can maintain the list in any data source. In this article, we explained the AdRotator control to fetch ads information from both database and XML file, and display those ads in randomly with the help of Timer control. You can say it as an AJAX AdRotator Control.

AdRotator with XML File

Open a project as Asp.Net Ajax enabled website in Microsoft Visual Studio 2005, drag and drop an UpdatePanel and add an AdRotator control and Timer Control into it. Set the UpdateMode property of UpdatePanel to conditional. Set the Timer Control Interval property to your desired time. The Interval is in milliseconds. Double click in the Timer Control, it will create a Timer1_Tick event in your code-behind. Add a single line of code as follows
 
protected void Timer1_Tick(object sender, EventArgs e)
{
    UpdatePanel1.Update();
}

Now add an XML file into your project, name it as Ads.xml and specify the AdvertisementFile property of the AdRotator control to point this XML file. So your AdRotator control’s html code will like like

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

Now we will fill the Ads.xml file with advertisement information. The AdvertisementFile must have the following attributes.
1. ImageUrl: The location of the ad image file.
2. NavigateUrl: The Web location to navigate to when the image ad is clicked.
3. AlternateText: The text to provide as the ALT attribute of the image control when the ad is rendered. This can also be seen as ToolTip.
4. Keyword: Here you can specify some keyword related with the ads image to fetch.
5. Impressions: An integer number to specify the weight of the ad. If the number is larger, then more time the image ad will appear.

The ImageUrl is the only mandatory attribute, rest is optional. Let us have a look on the XML Advertisement file we create for you.

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
  <Ad>
    <ImageUrl>http://www.anyserver.com/ads/859866.jpg</ImageUrl>
    <NavigateUrl> http://www.anyserver.com</NavigateUrl>
    <AlternateText>anyserver.com</AlternateText>
    <Keyword>Servers</Keyword>
    <Impressions>80</Impressions>
  </Ad>
  <Ad>   
    <ImageUrl>http://www.yourserver.com/ads/309548.gif</ImageUrl>
    <NavigateUrl>http://www.yourserver.com</NavigateUrl>
    <AlternateText>YourServer.com</AlternateText>
    <Keyword>MyServer</Keyword>
    <Impressions>80</Impressions>
  </Ad>
</Advertisements>
That’s it. Easy and save your project. View your project in browser, you can see the rotation of ads in your webpage, at the interval you fixed in the Timer Control.

Disadvantage of AdRotator with XML File

One of the main disadvantage of AdRotator when functioning with XML file is real time maintainance. Yes if you want to add any new ads or you want to remove any existing ads is not an easy job when your ads is large in number. Everytime to have to find and replace or remove the appropriate tags from the xml file and then you have to upload to the server which is time consuming and hectic. To overcome this difficulty, Asp.Net 2.0 introduce the concept of builing AdRotator control with database. Let us all jump to study how it is easy to integrate an AdRotator with database rather than an XML file.

AdRotator with Database

AdRotator control can be bind with any data source. For this article, we shall take MS SQL Server as our data source. We need to create a Table with the following columns in it.
Create Table YourAdRotatorTableName
(
  AlternateText VarChar(100),
  ImageUrl VarChar(100),
  NavigateUrl VarChar(200)
)
These columns in the table are already fixed with the AdRotator control’s attributes. So we no need to specify it in the property tab of the AdRotator control. In case, if you change the column name in the database table, then you have to specify it property in the AlternateTextField, ImageUrlField, NavigateUrlField and KeywordFilter attributes of the AdRotator control. But I don’t know why ImpressionField is missing in the property tab.

To start with, add another Aspx file to your project, add a ScriptManager and an UpdatePanel. Drag and drop an AdRotator with a Timer Control into the UpdatePanel. Set the Interval property to your desired time, which is in milliseconds. In the code-behind file, let us write a method to fetch ads information from the above table.
 
private DataTable FetchAdsFromDB()
{
    string sql = "select * from YourAdRotatorTableName";
    SqlDataAdapter da = new SqlDataAdapter(sql, );
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
}
The above method fetches ads information from the table and returns it as a database. You have to mention the DataSource of the AdRotator to this method in your Page_Load event.

AdRotator1.DataSource = FetchAdsFromDB();
AdRotator1.DataBind();

We should not forget the Timer Control and the AJAX functionality to be implemented with this AdRotator. So double click on the Timer control to create an event Timer1_Tick in your code-behind file. Just bind your FetchAdsFromDB method again to the AdRotator control. Save the changes and run your application, you can see the Ads Rotating in your webpage.

protected void Timer1_Tick(object sender, EventArgs e)
{
    AdRotator1.DataSource = FetchAdsFromDB();
    AdRotator1.DataBind();
}


Advantages of AdRotator with Database

The main advantage of AdRotator with database is easy maintainance of ads. You can activate or deactivate any time you want. You can manage n-number of Ads in you application with any ads size.

Click here to view our Sample Ads
 
 
Viewer's Comments
Posted by Shivani on 7/10/2007 12:37:41 AM
Excellent
 
Posted by dfhdhd on 7/10/2007 5:31:37 AM
xfhhjfgjfgjfj
 
Posted by Velu on 8/6/2007 8:15:52 AM
Excellent but
 
Posted by sai on 8/14/2007 2:40:02 AM
I Feel good
 
Posted by Adan Brown on 9/11/2007 3:56:49 PM
Can the sample application be downloaded?
 
Posted by Adan Brown on 9/11/2007 3:58:30 PM
Also, I believe that it would probably be better if instead of fetching the database on every postback, if the images where retrieved once and changed on the client side. Excellent article!
 
Posted by Meera on 11/30/2007 5:03:36 AM
Good Concept, Nice Explanation
 
Posted by Aparna on 11/30/2007 5:04:40 AM
This article explains the concept of Asp.Net AdRotator control fetching ad information from the database and rotates the ads on certain time interval. This article also gives a tip to fetch ad information from an XML file.
 
Posted by Foong on 11/30/2007 1:04:12 PM
XML is still faster in terms of page loading as compared to sql datatable and yet you are retreiving the data everytime page load. Slow loading... The alternative of your mentioned disadvantage above is to export your sql data table into xml file once you updated your table, and then load the rotator from the xml file. This can be done with a button click event. Just do a search online sql 2 xml
 
Posted by Badajoz on 12/4/2007 6:11:34 PM
Foong - ever heard of caching dude? I do hope you dont write websites without it otherwise you'll advocate serializing you DB into text ... ;)
 
Posted by Roland on 12/4/2007 11:52:13 PM
Very good article. Is there VB equivalent? I have visual studio 2005 and currently learning ASP 2.0 and VB.net
 
Posted by tcm on 12/14/2007 9:32:58 AM
Great, but no Impressions Support over DB
 
Posted by Sandesh Bharambe on 12/26/2007 1:23:31 PM
Its is very good and very helpful to us Thank you
 
Posted by vijay on 1/5/2008 5:50:52 AM
very helpful to me
 
Posted by Swati on 1/8/2008 6:00:39 AM
This is excellent article....hope will get similar such stuff
 
Posted by Naresg on 1/18/2008 3:42:05 AM
good 60% how to bind design time?
 
Posted by sibin on 3/6/2008 11:58:14 AM
excellent
 
Posted by abraham on 3/26/2008 10:18:22 AM
attention please.. If youre using kaspersky antivirus then it must be closing because newer displaying any images :)
 
Posted by lucky on 4/8/2008 1:06:02 AM
it was good
 
Posted by sameer on 4/9/2008 2:35:49 AM
Nice Article
 
Posted by Jibi Thomas on 4/24/2008 6:12:25 AM
Good Article, it solved my problem
 
Posted by Jibi Thomas on 4/24/2008 6:12:45 AM
Good Article, it solved my problem
 
Posted by Jibi Thomas on 4/24/2008 6:14:48 AM
Good Article, it solved my problem
 
Posted by Jibi Thomas on 4/24/2008 6:16:19 AM
Good Article, it solved my problem
 
Posted by Ramesh KR.Verma on 4/30/2008 12:36:12 AM
Good Article,i solve my problem by this Article
 
Posted by tghtrghtrg on 5/5/2008 12:07:19 AM
hjhgfnhgf
 
Posted by tghtrghtrg on 5/5/2008 12:07:46 AM
567y56y656y5hyu
 
Posted by tghtrghtrg on 5/5/2008 12:07:59 AM
xtryt5rhytrhytry
 
Posted by Rahul on 5/16/2008 2:49:48 AM
Not fully tested, There is popup for using timer inside updatepannel,While i all ready did....
 
Posted by vijen on 5/20/2008 7:12:17 AM
i need some spicial code through i want to move my image in my whole axpx.
 
Posted by dgd on 5/27/2008 5:15:40 AM
dfgdsfgsdfghjfgh
 
Posted by madhu on 6/4/2008 2:39:40 AM
Article is really very good and very helpful, but at the same time I am expecting the same concept develop in VS2003 and asp.net 1.1, because here adrotator is not supporting datasource property and also timer control. If u have any solution regarding please send it to the given emailid.
 
Posted by tarun on 6/25/2008 12:17:07 PM
this is a very good article about adrotator.
 
Posted by Usha on 6/27/2008 4:56:17 AM
This is good
 
 Rating & Comments
A word 'Excellent' means lot to the author of this article. You can give comments about this article but not the author.
Rate this Article:   
Name: