Guest Book Solution You can also view part I of this article which describes the setting up of this application with Visual Studio 2008 and IIS 7, running on Vista. SQL Server 2005 was used as the database. I had originally blogged on this using VS 2005 but feedback indicated that quite a few people are now using VS 2008, so I upgraded it to VS 2008, which I am currently running on VMWare Workstation... the best $190 I ever spent. This is part II and the full code will be available for download in part III, real soon!

Speaking of feedback, I have been having problems with the comments system on this blog for some time now. So, pretty soon I am upgrading the blog engine and switching to a database version. The plan is to eventually make it a real "Community" site. Watch this space...

The Guest Book application is written in C# and is pretty straightforward. Here I will walk through the creation of the back-end which will allow the site administrator to moderate the comments posted to the guest book by site visitors.

First, we need to set up our membership system. The membership system is where most people seem to get stuck. There are several ways of achieving the same thing and I think that this is what causes much of the confusion. Typically, in an application such as this you would use a CAPTCHA control to combat the spammers, so we are not going to force users to register in order to sign the guest book. We will need an administration area for our admin forms, so we will create an "admin" role. The way I go about this is to create a new folder and call it Admin. Add a config file  to this new folder and set up the authorization rules as shown below.

Admin Web.config


Now, we're going to cheat a little. I'm going to do the TV chef and slip in a little something I pre-prepared earlier in the kitchen (actually gleaned from several of Scott Guthrie's blog postings). I always keep a sample Web.config file handy that I know is set up correctly for a basic membership system. I start this way rather than jumping in right off with the WSAT wizard because I want to have one single database for both my application and membership data. Tidier. So, I manually set up the database connection in the project Web.config file, along with entries for the membership and authentication sections. See the snippets below, and don't forget to include the roleManager tag!

Project Web.config


Now that the membership database connection is configured the way we want it, we can run the aspnet_regsql tool from the command line. This will create the actual membership database objects for us as part of our Guests database. Just navigate from the start menu to SQL Server Management Studio and locate the command line tool under the Configuration Tools folder. Simply enter aspnet_regsql at the prompt and you will be presented with the SQL Server Setup Wizard. Here are some screenshots showing the correct selections to make:

SQL Server Setup Wizard


SQL Server Setup Wizard


SQL Server Setup Wizard


If you go to SSMS and refresh the database, you should now be able to expand the tables icon, and you will see that the wizard had created everything we need for our membership system. At this stage, you are ready to run the WSAT tool and start setting up the admin role and adding the admin user. I will outline this process in the final installment, as well as the creation of our administration forms. Stay tuned!

Membership Database Objects



ASP.NET 2.0 Guest Book Application

by agrace 4. February 2008 12:45

I recently had to create a Guest Book application for our local Tourism website. Googling a decent code sample didn't yield much so I created it from scratch. I'm making the code available at the bottom of this post. In an effort to keep things simple, I am leaving out the back-end administration since it basically uses the same database table. It also means that I do not have to walk through the membership sytem at this time. If I get requests, I may create another post on the admin part and update the code sample accordingly.

Add Comment


This is a Web Application project written in C#. There is a data access layer and a business layer. This may appear redundant for such a simple example as this but it does make for good coding habits! Ideally, one might also create a base page class and inherit from that, thus avoid unnecessary re-factorings later on. The App-Code folder is used to hold the business and data classes. Classes placed here are dynamically compiled which means we don't have to compile them separately.

Guest Book Message


If you place the source files for your classes in the App_Code folder, you must also add a reference to the System.Design assembly in the compilation section of the Web.config file. This reference requires the fully qualified assembly name of the System.Design assembly. You can get the information required for the fully qualified name by running the Global Assembly Cache Tool (Gacutil.exe) that ships with the Windows Software Development Kit (SDK), using the following syntax:

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>gacutil /l System.Design

I still make use of the SqlHelper class which came with the original "Data Access Application Block". Although this has been superceded by the Enterprise Library, I think it's much more appropriate for smaller projects. You can find out more here. When trying to incorporate the SqlHelper class into the project, I received the following error:

error CS0103: The name 'SqlHelper' does not exist in the current context

After you exhaust all that Google has to offer, you can try what I did and hack the project file by changing:

<content Include="App_Code\Data\SqlHelper.cs" />
<compile Include="App_Code\Data\SqlHelper.cs" />

Happy Days :-) 

Guest Book


Note that in the real-world you would use a CAPTCHA control. For simplicity I left it out here, but I originally used Peter Kellner's Ultimate CAPTCHA Control. It's easy to use and Peter was on hand to offer email assistance!

Feel free to download the code and use it any way you will! (74.71 kb) (3.13 kb)


Events and Delegates - Part II

by agrace 8. September 2007 09:24

Hungry Geek You can read part one of this topic here. This part presents a simple example of how to publish and subscribe to an event that does not send data. Note that events such as button clicks, that generate no data can avail of the EventHandler delegate provided in the class library.

Publish an Event - Five Steps:

* Define type inherited from EventArgs to contain any event-related data. If there is no data, use pre-defined EventArgs type

* Define delegate that specifies prototype of event handler. If the events generates no data, use pre-defined EventHandler delegate

* Define an event based on the delegate type

* Define a protected, virtual method to raise the event

* When the event occurs, call the protected, virtual method above

class ButtonPublisher
     public delegate void EventHandler(object sender, EventArgs e);
     public event EventHandler Click;

     // Each class that publishes an event provides a
     // protected, virtual method to raise that event.
     // The "On" name prefix makes this easy to spot.
     protected void OnClick()
         // Check for subscibers
         if (Click != null)
             // Notify them
             Click(this, null);

     // Raise the event
     public void PerformClick()


Subscribe to an Event - Three Steps:

* Implement an event handler with same signature specified by the event's delegate object

* Create a delegate object specified for the event. This delegate refers to the event handler method (see below)

this.Click += new EventHandler(ButtonClick_Handler);


* Subscribe to event by attaching event handler to the event (see above). Use the += operator so as not to cancel any previously registered delegates

class Subscriber
     static void Main(string[] args)
         ButtonPublisher button = new ButtonPublisher();
         // Create new delegate object and subscribe to event
         button.Click += new ButtonPublisher.EventHandler(ButtonClick_Handler);

     static public void ButtonClick_Handler(object sender, EventArgs e)
         Console.WriteLine("Button Clicked");

         // Keep console window open long enough to read!


Happy coding!

Tags: ,