I know, it is June and I am writing about pumpkins but I like this project and wanted to share it with you. I used to get calls from various departments with quick projects they had and the “Annual Pumpkin Contest” was one of my favorites. It was fun, it was easy and it gave me an opportunity to be a bit more creative in the page design. Each year at Halloween the company would sponsor a pumpkin decorating contest that was judged by the employees. There were four categories – Funniest, Most Creative, Most Original and Most Company-themed. The winner in each category received a gift card and the undying adoration of their fans.
Each year I would get a call from HR asking if I had time to do the voting page and each year I would put together a page with a Halloween-themed banner, some text, a table with a cell for each pumpkin and finally four sets of radio buttons for the voting. There was also a reporting page where the results could be seen -access to this page was restricted. The only thing difficult about this project each year was getting the graphics approved and the copy they wanted on the page.
We discovered the first year that many people were voting many times, even though they were asked to vote only once. I began tracking the users as they voted, but not their votes, in the database and if someone tried voting more than once subsequent votes were not recorded. I never spent much time building the database or trying to create some type of template because it was such a small project and it was always a welcomed diversion from other projects.
One year I got the call and I didn’t have a lot going on so I decided to build this project for the very last time. I was going to make it “smart” and data-driven so that next time, the person running the contest only had to do a couple things and the job was done. The other thing different with this year is that I was going to do it in C# instead of VB. I had been reading and watching video tutorials over the preceding couple months in an effort to learn the language but never had an appropriate project to put my new skills to the test.
I decided to give the administrator a more robust page and have them upload the pumpkin photos to the server themselves. They would also set the start and end dates along with the copy for the page. When the user opened the page, the application would get a list of the photos, lay out a table three (3) columns wide and load the table with pictures and captions. It would then generate the four sets of radio buttons at the bottom of the page for voting.
Let’s take a look at some of the code. The first thing I want to do is query the directory holding the pumpkin photos and put the list into an array, fi. To do this I need to load the System.IO namespace.
using System.IO;
FileInfo[] fi = new DirectoryInfo(Server.MapPath("pumpkins")).GetFiles("*.jpg");
Once I have the list of pumpkins, I am ready to put them on the page. To do this I will use a “Do…While” loop.
do { TableRow row = new TableRow(); TableCell cell = new TableCell(); string strP1 = fi[i].Name.ToString(); cell = new TableCell(); cell.Text = "<img src='pumpkins/" + strP1 + "' width='194px'> <br />Pumpkin " + (i + 1) + ""; row.Cells.Add(cell); try { string strP2 = fi[i + 1].Name.ToString(); cell = new TableCell(); cell.Text = "<img src='pumpkins/" + strP2 + "' width='194px'> <br />Pumpkin " + (i + 2) + ""; row.Cells.Add(cell); } catch { //empty cell cell = new TableCell(); cell.Text = ""; row.Cells.Add(cell); } try { string strP3 = fi[i + 2].Name.ToString(); cell = new TableCell(); cell.Text = "<img src='pumpkins/" + strP3 + "' width='194px'> <br />Pumpkin " + (i + 3) + ""; row.Cells.Add(cell); } catch { //empty cell cell = new TableCell(); cell.Text = ""; row.Cells.Add(cell); } tblPumpkins.Rows.Add(row); i = i + 3; } while (i <= fi.Length - 1);
All that we have left are the voting buttons at the bottom of the page.
for (int orig = 0; orig < fi.Length; orig++) { rdoMostOriginal.Items.Add("Pumpkin " + (orig + 1)); rdoCreative.Items.Add("Pumpkin " + (orig + 1)); rdoFunniest.Items.Add("Pumpkin " + (orig + 1)); rdoTheme.Items.Add("Pumpkin " + (orig + 1)); }
That covers most of the user page, with the exception of the “Submit vote” button. I will cover the administrative and reporting code in an upcoming article.
If you would like to know more about this project or have one you would like to discuss, please write to me at joe@joevalencia.site90.com.
Filed under: ASP.NET, C#, Excel, SQL Server, VB.NET, Web design, Web development Tagged: pumpkin contest, pumpkin decorating contest, radio buttons
