Click here to Skip to main content
15,886,860 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.9K   3.9K   53  
Implement a Flash ASP.NET AdRotator to fetch ads from a database.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Flash : System.Web.UI.Page
{
    //Total No of ads to display
    static int TotalAds = 5;

    /// <summary>
    /// Initial/First ad to display
    /// </summary>
    int _adCount = 1;
    public int AdCount
    {
        get { return _adCount; }
        set { _adCount = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           PrevBtn.Enabled = false;
           FlashAdRotator1.DataSource = GetAds(AdCount);
           FlashAdRotator1.DataBind();
           
        }
    }
    /// <summary>
    /// Fetch ads from database
    /// </summary>
    /// <param name="Count"></param>
    /// <returns></returns>
    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; 
    }
   
    //UpdatePanel1.Update(); 
    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();
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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