Home arrow ASP arrow Page 9 - Make Revenue With Your Own Banner Management System

Make Revenue With Your Own Banner Management System

In this article Mitchell shows us how to develop a simple ad campaign and management system using ASP and an SQL Server database.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 33
November 01, 2001
  1. · Make Revenue With Your Own Banner Management System
  2. · Adding the banners
  3. · Saving the banners
  4. · The ASP to add the banner
  5. · The ASP to add the banner (contd)
  6. · Listing the banners
  7. · Deleting a banner
  8. · Displaying and tracking banners, impressions and click-thrus
  9. · The code in detail
  10. · Handling the banner clicks
  11. · Conclusion

print this article

Make Revenue With Your Own Banner Management System - The code in detail
(Page 9 of 11 )

Let's start with the function declaration:

function GetBanner()

dim objConn

dim objRS

dim strBanner_VirtualPath

dim strBannerHTML

dim intNumBanners

dim intRandom

set objConn = Server.CreateObject("ADODB.Connection")

set objRS = Server.CreateObject("ADODB.Recordset")

strBanner_VirtualPath = "/banners/"

objConn.Open "Provider=SQLOLEDB; Data

Source=localhost; Initial Catalog=myAdStuff; UId=sa; Pwd="

objRS.ActiveConnection = objConn

objRS.LockType = adLockReadOnly

objRS.CursorType = adUseForwardOnly

This function is responsible for the retrieval and display of a banner at random from our database. Firstly, we declare a new database connection and recordset. After this, we set the virtual path for the location of our banners folder in the strBanner_VirtualPath variable. Lastly, we open a connection to our database and set the ActiveConnection of our recordset object to point to this connection.

objRS.Open "select count(*) from banners"

intNumBanners = objRS.Fields(0).Value

if intNumBanners = 0 then

exit function

end if


intRandom = int(rnd(1)*intNumBanners)


This next part of our code is where the random number generation takes place. Firstly, we use our recordset object to get a count of the number of records in our banners table. The query we have used (select count(*) from banners) returns a table with one field containing one value. If we executed this query using query analyser when we had three banners in our table, we would achieve this result:


After we store the return value into the variable intNumBanners, we run a quick check to make sure that there is at least one banner in our table. If there isn't, we exit the function and return nothing.

Now comes the random banner generation. First, we call the Randomize function which basically tells our ASP engine this: "When we call the Rnd() function, make sure we get a random number every time". It sounds confusing, but really, it isn't. Then we use the Rnd() function to generate a random integer between 1 and the number of records in our database (in my case, 3), followed by closing our recordset object.

objRS.Open "select * from banners"

objRS.Move intRandom

strBannerHTML = "<a href='bannerclick.asp?bannerId=" &

objRS("bannerId") & "'><img border='0' src='" &

strBanner_VirtualPath & objRS("bannerImage") & "'></a>"

Response.Write strBannerHTML

Because we closed our recordset object previously, we must open it again. However, this time we select all the rows in the banners table as our query. Because we're using a recordset with a dynamic cursor (objRS.CursorType = adUseDynamic), we have the ability to move to any row in that recordset. objRS.Move intRandom uses our randomly generated number as the row to move to. So If we generated a random number of 3, we would move to the third row in our recordset (remember that recordsets are like a zero-based array, so we'd be moving to row number three but the row would have an index of 2).

The last line is used to build the HTML string that we will output in our browser. The code uses simple anchor and image tags to display our banner with a link. Notice the URL in the anchor tag (bannerclick.asp?bannerId=" & objRS("bannerId")). This is the next and final page that we will discuss. It's the page that handles the addition of click-thrus and re-directs the user to the URL that corresponds to that particular banner. More on that later, though.

objConn.Execute "UPDATE banners SET bannerImpressions =

bannerImpressions + 1 WHERE bannerId = " & objRS("bannerId")


set objConn = nothing

set objRS = nothing

end function

The final part of our GetBanner function uses our connection object to increase the impression count for the randomly selected banner, and frees up the memory used by our connection and recordset objects.

I understand the function, now what?

So, now that we've stored our GetBanner() function in bannercode.asp, how do we display the banners on our Website? It's a simple matter of adding two lines of code to any existing ASP page. Lets look at an example:

<!-- #INCLUDE file="bannercode.asp" -->



<title> My Test Banner </title>






As you can see from the example above, we simply use an SSI (Server-Side include) to reference the file that holds our GetBanner function. Then we simply call this function with either the <%= GetBanner() %> method or <% Response.Write GetBanner() %> method.

In the next and final part of the tutorial, we'll create the bannerclick.asp page that handles the click-thrus and re-directions when a banner is clicked from our site.
blog comments powered by Disqus

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials