if strBanner_Url = "" then
As with all good programs, scripts and components, we will include some basic error checking and handling procedures. In the first couple of lines, we will use our upload objects file collection property to check if a banner image has been uploaded. If one hasn't (if objUpload.Files.Count = 0) then we will show an error message and terminate the script.
We will then get the image filename from the Files collection of our upload object and also the name and url that we typed in for this banner. Remember that we can't just access these using the Request.Form collection because we have set our forms enctype attribute to “multipart/form-data” and this blocks all access to the Request.Form collection when a page is processed. Instead, our upload component handles the variables parsed from the Request.Form collection for us, and stores them in its Form collection.
objConn.Open "Provider=SQLOLEDB; Data Source=localhost; Initial Catalog=myAdStuff; UId=sa; Pwd="
objRS.Open "select * from banners", objConn, adOpenDynamic, adLockPessimistic
The next part of our ASP script uses our database connection object (objConn) to connect to our database. If your database is not on the same machine as your ASP scripts, then change the Data Source=localhost attribute to match the name of your SQL Server.
When we've connected to our database, we will open our recordset object (objRS). There are a number of ways to open a recordset object, but we will use the easiest one to keep this example simple. The first parameter, “select * from banners” can be any valid SQL query. It's just used to interact with the database for now. The next parameter, “objConn”, sets a reference to our database connection object, meaning that our recordset object will use objConn to execute its queries. Next, the “adOpenDynamic” cursor types tells SQL Server that we will want to have “dynamic” access to our database: both read and write, not just read, which is adOpenForwardOnly. The last parameter sets the lock type on our database.
objRS("bannerName") = strBanner_Name
objRS("bannerUrl") = strBanner_Url
objRS("bannerImage") = strBanner_Image
objRS("bannerImpressions") = 0
objRS("bannerClickThrus") = 0
The code above is the main part of our ASP script. This code tells SQL server that we want to add a new record to our database (objRS.AddNew). We then set the field name and value pairs that we want to add to our new record (bannerName, bannerUrl, bannerImage, bannerImpressions and bannerClickThrus) and call the Update method to save the record. Notice that we don't have to specify the values of the bannerId field because it is an auto-incrementing field.
set objRS = nothing
set objConn = nothing
Lastly, we close the recordset and connection objects, set them to nothing (so the resources they used are freed up), and send a re-direct response code to our browser. See how the Response.Redirect command is telling our browser to go to banners.asp? This page doesn't exist, but will be used to show a list of all of the banners in our schedule. Now that we can add a banner, lets create the code to list our banners and also to delete banners when we no longer want them in our schedule.