Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 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 : 121095
Add to my favorites
Email to friend
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)

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" ?>
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();
    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();

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();

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
Posted by dfhdhd on 7/10/2007 5:31:37 AM
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
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
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 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 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
Posted by Nixson on 7/22/2008 7:21:01 AM
Hi, This is really a good concept. I tried with it , but i am able to get the alternate text and the navigate url correctly but the picture is not displayed. I have the image url as c:/images/france in the database(local machine) but it does not diplay the image but the alternate text is displayed correctly. Please give me sme idea regarding this
Posted by Ram on 8/18/2008 2:58:40 AM
Don't know whether the ads will be displayed with out entie page refresh..any way Good Article
Posted by annapurna on 8/22/2008 5:07:40 AM
very nice n very useful to me.but how can we find the id of the uploaded image
Posted by amandeep on 8/28/2008 4:38:57 AM
sir ur code is verywell but it is not working properly i tried to impliment in my website but its giving error
Posted by viv on 10/1/2008 5:22:01 AM
how to compare current date and selected date from calendar control?
Posted by s.venkatesh on 11/2/2008 3:10:24 AM
excellent pls give in more detailed format... thank you...
Posted by kishor on 11/19/2008 5:08:10 PM
noooooooooooooooooooot goooooooooooooooooooood plz give me the most good explanation about adrotator control........ thank u
Posted by A. D.Glogo on 12/13/2008 1:24:20 PM
Very very good help for me! Saves me the trouble and horrors of using Flash movies...ha ha ha. I am so happy :). Remember to add a script manager control from toolbox/Ajax Extensions before running the page. You don't have a single problem ~_~
Posted by Hanne on 12/18/2008 2:40:45 PM
badly need your help. It's never just a game when you're winning. I am from Japan and know bad English, please tell me whether I wrote the following sentence: "Video making, not resume writing, might be the most necessary skill for job seekers today." :-) Thanks in advance. Hanne.
Posted by gunageorge on 1/10/2009 5:54:55 AM
Good , Excellent.... but
Posted by durgesh on 1/22/2009 4:56:53 AM
best solution
Posted by Raghav on 2/13/2009 3:24:18 AM
Really it is good article. Keep it up.
Posted by J.Balaji on 2/19/2009 12:21:42 AM
Its Good But, i want to try in my website
Posted by Yoofi on 2/19/2009 8:39:09 AM
This is an excellent article and I used it to help me come up with something better. Instead of hitting the database all the time to generate the ad -- keep the xml. Have all your ads in the database as this article shows, but have an admin section with a button to generate the xml needed for the <asp:adrotator>. Use a timer control and subscribe to the ad being created. Update the db for the # of times the add has been used. If an ad has expired, auto generate a new xml (from the ad subsc
Posted by dazzy on 2/23/2009 2:21:38 PM
Its an amazing article that is written in a very understandable way and an easy one for a novice like me. Thanks a lot!
Posted by Ramesh on 3/1/2009 8:32:29 AM
how to display ads in sequence
Posted by khalid on 4/3/2009 5:03:47 AM
good how i can use the adrotator with out ajax control
Posted by bhushan on 4/6/2009 6:03:21 AM
thank for help.....
Posted by Alexandre on 4/14/2009 3:12:03 PM
how to display ads in sequence (2)
Posted by ARUNKUMAR.G on 4/23/2009 4:39:59 AM
sir it was really very good.....
Posted by Ramanathan on 4/24/2009 1:13:37 AM
My problem is: database using Adrotator can not display the image
Posted by kumar on 5/23/2009 5:31:30 AM
How to use <asp:AdRotator samples
Posted by developer on 5/26/2009 1:50:04 AM
very usefull for the beginners
Posted by Madhusmita on 5/27/2009 7:55:14 AM
Really very useful for the Beginners
Posted by Chandan on 6/3/2009 7:25:11 AM
Excelent...Thank u...
Posted by piyush madhukar on 7/1/2009 10:02:02 PM
thanks. it helps me a lot
Posted by guna on 7/17/2009 5:09:13 AM
Hi, This article very useful to me. Thanks a lot. thanks, guna
Posted by doremi on 7/23/2009 7:32:52 AM
the article seems to explain things easily, but the coding is somehow not working.. too bad.
Posted by nik on 8/15/2009 6:18:50 AM
What a bad coding such a poor article.....
Posted by pradeep kumar maurya on 9/17/2009 6:58:16 AM
good but how can i got it on his email by request. plz reply
Posted by ulysses on 2/28/2010 7:34:30 AM
Excelent Thank you
Posted by Anjani on 3/19/2010 4:46:35 AM
Thanks it was very useful
Posted by Moe on 4/18/2010 3:55:21 PM
thanks for the information =) but the images is not showing i am using ASP.NET 3.5 framework plz any help would be great
Posted by jeevi on 6/30/2010 9:38:27 AM
Excelent ! Thank you.
Posted by pradeep on 10/17/2010 2:41:21 AM
very very thank you..............these element are very good
Posted by raj on 11/24/2010 3:16:54 AM
i think i want something better than this. no doubt its too goog for learnng purpose but not good enough for comersial web development. Gud Job :d
Posted by davinder on 12/6/2010 12:41:27 PM
It is very useful.. but can u plz show what to write in code behind
Posted by sandi on 1/5/2011 11:10:22 PM
plz tell me that when my images save in file and url save in database then how we can fetch the images from file for adrotator......plz tell me any one.....
Posted by prisilla on 2/14/2011 9:39:10 AM
it is good,but when i run it this error come ,how can i slove it? ID 'UpdatePanel1' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.
Posted by jaheena on 7/1/2011 2:45:45 AM
This is good, but how can insert url and other information into databse. this page doesn't contain such information.
Posted by divya on 7/15/2011 5:17:46 AM
Hi, This is really a excellent concept. I tried but i am able to get the alternate text and the navigate url correctly but the picture is not displayed. Please give me some idea regarding this
Posted by Kranti on 9/27/2011 4:08:14 AM
I am having banner management in my website for that i have used adrotator as i am getting imgeurl & navigateurl.but, if i wan to display advertisement using script also then how to handle that in adrotator & how to manage percentagetime for script in aspnet3 or is there any other solution.
Posted by Relica Watches on 10/17/2011 1:09:58 AM
Day emperor through earthly mirror, find the little friends is in. Although together in a
Posted by Hemali on 2/29/2012 1:14:14 AM
Hi, Very nice article. Bt m having problem tht i put this code in Default.aspx which is connected to master page, it displays the image bt rotation of image is not working. Pls help me. Pls pls...
Posted by Pelchat on 4/25/2012 9:48:07 PM
There is an issue with the "Impressions" field when getting values the database. After many tests, my ads where displayed without using the Impressions value. With a little bit of ILSpy, I found that only field value of string, datetime or decimal where bind from the AdRotator datasource. So to make the AdRotator use the Impressions value , I used a string field for the Impressions value in my database. Voilà !
Posted by Bharath Kumar on 5/8/2012 2:10:55 AM
Thank you very much
Posted by kumar on 12/19/2012 5:04:46 PM
Excellent Approach!
Posted by Mayuresh on 5/7/2013 2:40:30 AM
its not working
Posted by Brendz on 5/7/2013 3:34:02 PM
Used Adrotator with database, Modified a bit using OleDbConnection n OleDbDataAdapter for retrieving from an Access Database Table. Worked fine with display of pictures. ImageUrl bound well with the AdRotator not NavigateUrl. Moreover debuged the application with a break point in Timer1_Tick event. Unfortunately the timer_tick event never fired after interval was done.
Posted by jayas on 3/15/2014 12:52:33 PM
adrotator is not displaying image <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:Timer ID="Timer1" runat="server" Interval="2000" ontick="Timer1_Tick1"> </asp:Timer> <asp:UpdatePanel ID="BannerPanel" runat="server" UpdateMode
Posted by opuoip on 4/14/2014 3:11:56 AM
Posted by Ashif Ali on 8/2/2014 11:04:13 AM
Good....but image not display
Posted by Ashif Ali on 8/2/2014 11:04:13 AM
Good....but image not display
Posted by Sandhya on 7/11/2016 4:40:50 AM
Good Article and it is useful, thanks
 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:
Email Id:  
We never display your email id anywhere.
Comment/Question: Max. 500 letters
Twenty-One plus Six Equals To
Sponsored by
Programmers Heaven C# School Book - Free 338 Page eBook
The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks - Free 158 Page Preview
Simply SQL - Free 111 Page Preview!