More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Saturday, 2 July 2016 9:59:00 PM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 519

Tracking Advertisements with the AdRotator Control and database

In Walkthrough: Displaying and Tracking Advertisements with the AdRotator Control, it uses a web form Page_Load handler to track an ad and redirect to target url. We could use an http handler instead.

Also mentioned in the article:
Quote:
In a production application, do not use an XML file to track ad responses, because it will not scale to support many users. This walkthrough uses an XML file for ad responses only to simplify the walkthrough and to make is to that you do not have to configure a database.


So in this post, we'll do:
1. replace web form Page_Load handler with an http handler - AdHandler.ashx?name=ad_name&target=target_url;
2. use database instead of xml file to track ads for multi-user heavy-load commercial applications.

Note we can create advertisements in either xml file or database.

Code:
<%@ WebHandler Language="C#" Class="AdHandler" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

public class AdHandler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        string name = context.Request.QueryString["name"];
        string url = context.Request.QueryString["target"];
        try
        {
            AddHit(name);
        }
        catch
        {
        }
        finally
        {
            context.Response.Redirect(url);
        }
    }
    public int AddHit(string name)
    {
        SqlConnection con = new SqlConnection("myConnectionString");
        //can also get connection string from web.config
       // SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        SqlCommand com = new SqlCommand("AddHit", con);
        com.CommandType = System.Data.CommandType.StoredProcedure;
        com.Parameters.Add(new SqlParameter("@name", name));
        int res = com.ExecuteNonQuery();
        con.Close();
        return res;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}


In database, create an AdHits table which just contains 2 columns: name, ad name string, and hits, ad clicks, an integer.

Create a stored procedure
Code:
ALTER PROCEDURE [dbo].[AddHit]
	@name varchar(50) = NULL
AS
	/* SET NOCOUNT ON */
	IF exists (Select name FROM AdHits where name = @name)
              UPDATE AdHits SET Hits = Hits+1 where name = @name
       ELSE  
       INSERT INTO AdHits values (@name,1)

	RETURN


Syntax track ad responses,
AdHandler.ashx?name=ad_name&target=target_url

Edited by user Sunday, 3 July 2016 7:15:31 AM(UTC)  | Reason: Not specified

Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.