Creating a Guest Book in ASP.NET


Today, a guest book is a common tool used on Web sites to gather information about visitors. In this example, you'll see how to create a guest book using ASP.NET and VB .NET. In this application, you'll use a Microsoft Access 2000 database to store the data submitted by site visitors. You can use even use a SQL Server database. The only reason this example uses an Access database is because it's easier to create and use than SQL Server. The database name is GuestBook.mdb. You can create this database in Access 2000. This database has only one table, Guest. Figure 14-30 shows the table schema. As you can see, ID is a unique auto number field. The Name, Address, Email, and Comments fields represent the name, address, email address, and comments of a visitor (respectively).

click to expand
Figure 14-30: Table schema of Guest table of GuestBook.mdb

The following sections provide a simple tutorial that guide you through creating a guest book for your Web site step by step.

To create a guest book, first you create an ASP.NET Web Application project using the Visual Basic Web Application template from the available templates. Name the project MyGuestBook.

Default Web Form: MyGuestBook.aspx

When you create a new Web application project, the wizard adds one default Web Form to your project called WebForm1.aspx. You can see this page when you run your application. For this example, rename WebForm1.aspx to MyGuestBook.aspx. You can rename a page by right-clicking the .aspx file in the Solution Explorer and selecting the Rename option.

Next, add a few Web controls to the form. For this form, add the controls listed in Table 14-6.

Table 14-6: Web Controls on MyGuestBook.aspx Page

CONTROL

TYPE

DESCRIPTION

NameTextBox

<asp:TextBox>

Name text box

AddressTextBox

<asp:TextBox>

Address text box

EmailTextBox

<asp:TextBox>

Email text box

CommentsTextBox

<asp:TextBox>

Comments text box

Button1

<asp:Button>

Saves data to the database and calls Thanks.aspx

Button2

<asp:Button>

Calls ViewGuestBook.aspx

As you can see from Table 14-6, we added some text to the page, four text boxes, and two buttons and then renamed them accordingly by setting each one's properties. For example, we set the Comments TextBox control's TextMode property to Multiple. By changing the properties of the controls, MyGuestBook.aspx form looks like Figure 14-31.

click to expand
Figure 14-31: MyGuestBook.aspx submission page

Now double-click the Sign In Guest Book button and write the code in Listing 14-15. Don't forget to import System.Data.OleDb namespace in your application.

Listing 14-15: Adding Guest Data to the Database

start example
 Private Sub Button1_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles Button1.Click     ' set Access connection and select strings     Dim strDSN As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _           "Data Source=C:\\GuestBook.mdb"     Dim strSQL As String = "INSERT INTO Guest" & _                "(Name, Address, Email, Comments)" & _                "VALUES('" + NameTextBox.Text.ToString() + "','" & _                 AddressTextBox.Text.ToString() + "','" & _                 EmailTextBox.Text.ToString() & _                 "','" + CommentsTextBox.Text.ToString() + "')"     ' create OleDbDataAdapter     Dim conn As OleDbConnection = New OleDbConnection(strDSN)     ' Create OleDbCommand and call ExecuteNonQuery to execute     ' a SQL statement     Dim myCmd As OleDbCommand = New OleDbCommand(strSQL, conn)     Try       conn.Open()       myCmd.ExecuteNonQuery()     Catch exp As Exception       Console.WriteLine("Error: {0}", exp.Message)     Finally       conn.Close()       conn.Dispose()     End Try     ' Open Thanks.aspx page after adding entries to the guest book     Response.Redirect("Thanks.aspx")   End Sub 
end example

As you can see from Listing 14-15, you write the data entered into the Web Form to an Access database. Our database resides in the C:\ root dir. Obviously, if your database is somewhere else, you need to change this database path. After writing to the database's Guest table we continue the program by opening the Thanks.aspx page in your browser. (We discuss this page a little bit further in the "Thanks.aspx" section.)

Now, add the code in Listing 14-16 to the click event of the View Guest Book button. The View Guest Book click event opens the ViewGuestBook.aspx page in the browser.

Listing 14-16: Opening ViewGuestBook.aspx

start example
 Private Sub Button2_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles Button2.Click     ' View CiewGuestBook.aspx page     Response.Redirect("ViewGuestBook.aspx")   End Sub 
end example

Adding Forms to the Guest Book

Other than the MyGuestBook.aspx page, you'll add two more Web Forms to the project. The first form you'll add is called ViewGuestBook.aspx, and the second form is Thanks.aspx. The ViewGuestBook.aspx form reads the data from the database and enables you to view the contents in a DataGrid on a Web page. The Thanks.aspx form is, as you may have guessed, a simple "thank you" Web page shown to the guest, thanking them for registering on the site. Follow these steps: To add a new Web form, right-click your project and select Add Add Web Form. Clicking this menu item opens a form, which lets you pick different types of items for your project. Choose the Web Form template, name it Thanks.aspx, and then click Open.

Using the same method, add one more Web page, ViewGuestBook.aspx, to the project.

ViewGuestBook.aspx

The ViewGuestBook.aspx form contains two controls, a DataGrid control and a Button control (see Table 14-7).

Table 14-7: Web Controls of ViewGuestBook.aspx

CONTROL

TYPE

DESCRIPTION

DataGrid1

<asp:DataGrid>

Displays guest book entries from the database

Button1

<asp:button>

Navigates to the home page

After setting the properties of controls, the page looks like Figure 14-32.

click to expand
Figure 14-32: The ViewGuestBook.aspx page

The DataGrid control displays the guest book entries from the database. The code for populating the DataGrid from the database is on the Page_Load event of the form. The Back to Home Page button sends the browser to the site home page.

We've used the OleDbDataAdapter and DataSet to get the data from the database. As discussed, the DataSource property of the DataGrid takes care of rest. You just need to set the DataSource property as the DefaultView of the DataSet, like so:

 DataGrid1.DataSource = ds.Tables("Guest").DefaultView 

Listing 14-17 shows the Page_Load event and the Back to Home Page button click code.

Listing 14-17: Page_Load Event Handler Code of ViewGuestBook.aspx

start example
 Private Sub Page_Load(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles MyBase.Load     ' Create a connection object     Dim conn As OleDbConnection = New OleDbConnection()     conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _                 "Data Source=C:\\GuestBook.mdb"     Dim sql As String = "SELECT * FROM Guest"     ' Create a data adapter     Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql, conn)     ' Create and fill data set and bind it to the data grid     Dim ds As DataSet = New DataSet()     da.Fill(ds, "Guest")     DataGrid1.DataSource = ds.Tables("Guest").DefaultView     DataGrid1.DataBind()   End Sub Private Sub Button1_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles Button1.Click     Response.Redirect("http://www.c-sharpcorner.com") End Sub 
end example

Thanks.aspx

The Thanks.aspx page is merely a confirmation page that the user receives after adding data to the guest book. It has two buttons and a simple message. The buttons are responsible for navigating you through the ViewGuestBook.aspx page or to the site home page. Table 14-8 lists the controls for Thanks.aspx.

Table 14-8: Controls of Thanks.aspx Page

CONTROL

TYPE

DESCRIPTION

Button1

<asp:button>

Calls ViewGuestBook.aspx page

Button2

<asp:button>

Navigates the browser to the site home page

The Thanks.aspx page looks like Figure 14-33.

click to expand
Figure 14-33: Thank you page

Listing 14-18 shows the My Home Page button and the View Guest Book button-click code. As you can see from the listing, the My Home Page button click calls www.c-sharpcorner.com. Obviously, you can call your Web site's home page. The View Guest Book button click calls ViewGuestBook.aspx.

Listing 14-18: My Home Page and View Guest Book Buttons

start example
 Private Sub Button1_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles Button1.Click     Response.Redirect("http://www.c-sharpcorner.com/")   End Sub   Private Sub Button2_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles Button2.Click     Response.Redirect("ViewGuestBook.aspx") End Sub 
end example

Note

Don't forget to add a reference to the System.Data.OleDb namespace in the ViewGuestBook.aspx and MyGuestBook.aspx pages.

Compiling and Running the Guest Book Project

Now you're all set to compile and run the project. You should be able to do everything you usually do in a guest book. The output of the program looks like Figure 14-34.

click to expand
Figure 14-34: Welcome to my guest book

As you can see from Figure 14-34, we added a new record by filling data in the fields and clicking the Sign In Guest Book button. The next page displayed is the Thanks.aspx page, which looks like Figure 14-35.

click to expand
Figure 14-35: The Thanks.aspx page of the guest book

Now, if you view the guest book, it looks like Figure 14-36. As you'll notice, there are a couple of extra records in this guest book.

click to expand
Figure 14-36: My guest book entries




Applied ADO. NET(c) Building Data-Driven Solutions
Applied ADO.NET: Building Data-Driven Solutions
ISBN: 1590590732
EAN: 2147483647
Year: 2006
Pages: 214

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net