Normally, the asp.net AdRotator sever control reads the advertisement information from a xml file but if the rest of your data driven web site is getting its data from a SQL database, then you might be tempted to move your ads to the database as well.
To get started, you will need to create a new table in SQL, lets just call it "Advertisements". Remember to set the ID column to auto increment.
Next, we can add a new web page to our site and drag an AdRotator onto it. Switch over to your code behind and add the following to your page load event:
AdRotator1.DataSource = LoadAds();
AdRotator1.DataBind();
Then also add the following method to your page (remember to modify the connection string):
private DataTable LoadAds()
{ DataTable ret = new DataTable();
SqlConnection conn = new SqlConnection("Your Connection String..."); try
{ SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT ImageUrl, NavigateUrl, AlternateText, " +
"Keyword FROM Advertisements ";
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(ret);
}
catch (Exception ex)
{ // Handle the exception
}
finally
{ if (conn.State == ConnectionState.Open)
conn.Close();
}
return ret;
}
Now, create 2 or 3 images which you will be using as advertisements, create a new folder inside your web site call "Advertisements" and copy the images you created into the folder.
Now the only thing that's left to do is create our database records:
If you run the page now, you will see a new advertisement each time it is refreshed.
The cool thing about getting the data from your database is you can add some meta data to each advertisement and even a IsActive bit field where you can quickly and easily enable or disable some of the advertisements.