Monday, August 14, 2017

How to show rotating ads using Sql server database in

In this article I am going to explain how to show rotating ads using Sql server database in without page refresh.

Adrotator control is used to show custom Ads (advertisements) on build websites. When users click on any ad they will be redirect to link assign for that. We can use both Xml and database as data source. Xml and database table must have following properties and columns: ImageUrl, NavigateUrl, AlternateText, Impressions, Keyword, Width and Height.

In this example I am going to use sql server database as adrotator data source. I have create a table Ads and insert some dummy records into it.

How to show rotating ads using Sql server database in

Complete source of webform

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
     <asp:Timer ID="Timer1" Interval="1000" runat="server"/>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                 <asp:AdRotator ID="AdRotator1" runat="server" Target="_blank"/>
                            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

Add namespaces

C# Code
using System.Configuration;
using System.Data.SqlClient;
using System.Data; Code
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Bind data to Adrotator
Create a method to get data from database and call it on page load.

C# Code
  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            AdRotator1.DataSource = BindAds();
    public DataTable BindAds()
            SqlDataAdapter adp = new SqlDataAdapter("Select * from Ads", con);
            DataTable dt = new DataTable();
            return dt;
    protected void Timer1_Tick(object sender, EventArgs e)
        AdRotator1.DataSource = BindAds();
    } Code
  Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            AdRotator1.DataSource = BindAds()
        End If
    End Sub
   Public Function BindAds() As DataTable
        Dim adp As New SqlDataAdapter("Select * from Ads", con)
        Dim dt As New DataTable()
        Return dt
    End Function

    Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        AdRotator1.DataSource = BindAds()
    End Sub

No comments:

Post a Comment