This article has been
excerpted from book "A Programmer's Guide to ADO.NET in C#".
Today, a guest book one of the basic requirements of Webmaster to gather
information from a Web site about its visitors. In this example, I'll show you
how to create a guest book using ASP.NET and C#. In this application, I'll use a
Microsoft Access 2000 database to store the data submitted by site visitors. The
database's name is GuestBook.mdb. You can create this database in Access 2000.
This database has only one table, Guest. Figure 7-37 shows the table schema. As
you can see, ID is a unique autonumber field. The Name address, Email, and
Comments fields represent the name, address, e-mail address, and comments of a
visitor.
Figure 7-37. Table schema of Guest table of GuestBook.mdb
This is simple tutorial that will guide you though 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 C# > Web Application template from the available template, as
shown in figure 7-38.
Figure 7-38. Creating the MyGuestBook ASP.NET Application project
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. In this application, I've renamed WebForm1.aspx to
MyGuestBook.aspx. You can rename a page by right –clicking on the .aspx file in
the Solution Explorer and selecting the Rename option, as shown in Figure 7-39.
Figure 7-39. Renaming WebForm.aspx to MyGuestBook.aspx
Next, add a few Web controls to the form. In my form, I've added the controls
listed in Table 7-6
Table 7-6. Web Controls my Guest Book Page
CONTROL |
TYPE |
DESCRIPTION |
NameTextBox |
<asp:Text Box> |
Name text box |
AddressTextBox |
<asp:Text Box> |
Address text box |
EmailTextBox |
<asp:Text Box> |
Email Text Box |
CommentTextBox |
<asp:Text Box> |
Comments text box |
Button1 |
<asp:Button> |
Button control
saves data to the database and calls Thanks.aspx |
Button2 |
<asp:Button> |
Calls View Guest
Book.aspx |
Lablel1-Lable4 |
<asp:Lable> |
Four label
controls |
As you can see from Table 7-6 added four text boxes, two buttons, and four
labels and rename them accordingly by setting each one's properties. For
example, I set the Components Text Box control's TextMode property to Multiple.
By changing the properties of the controls, MyGuestBook.aspx form looks like
figure 7-40.
Figure 7-40. My guest book submission page
Now double-click on the sing In Guest Book button and write the code in listing
7-6.
Listing 7-6. Source code for adding guest data to the database
protected void
Button1_Click(object sender,
EventArgs e)
{
// set Access connection and select
strings
string
strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;"
+
" Data Source=C:/GuestBook.mdb";
string strSQL =
"INSERT INTO Guest " +
"( Name,Address,Email,Comments)" +
"VALUES ('" + NameTextBox.Text.ToString() + " ',' "
+
AddressTextBox.Text.ToString() +
" ', '"
+
EmailTextBox.Text.ToString()
+
" ',' " + CommentsTextBox.Text.ToString() +
" ')";
// Create oleDbDataAdapter
OleDbConnection
myConn = new OleDbConnection(strDSN);
//Create ole Db Command And call
ExecuteNonQuery to execute
// a SQL statement
OleDbCommand myCmd
= new OleDbCommand(strSQL, myConn);
try
{
myConn.Open();
myCmd.ExecuteNonQuery();
}
catch (Exception
exp)
{
Console.WriteLine("Error:
{0}", exp.Message);
}
myConn.Close();
// open Thans.aspx page after adding
entries to the guest book
Response.Redirect("Thanks.aspx");
}
As you can see from Listing 7-6, you write the data entered into the Web form to
an Access database. My 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 you continue the program by opening the
thanks.aspx page in your browser. I'll discuss this page a little bit further
along in the "Thank.aspx" section.
Now add the code in listing 7-7 to the click event of the View Guest Book
button. The View Guest book click opens the ViewGuestBook.aspx page the browser.
Listing 7-7. Source code for opening ViewGuestBook.aspx
protected
void Button2_Click(object
sender, EventArgs e)
{
// View ViewGuestBook.aspx page
Response.Redirect("ViewGuestBook.aspx");
}
Adding Forms to the Guest Book
Other than MyGuestBook.aspx page, I'll add two more Web Forms to the project.
The first form I'll add is called ViewGuestBook.aspx, and the second form is
Thanks.aspx. The ViewGuestBoook.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.
To add a new Web form, right-click on your project and select Add > Add Web
Form (see Figure 7-41).
Figure 7-41. Adding a new Web page to the project
Clicking on this menu item opens a form, which lets you pick different types of
items for your project. Choose the Web Form template and type Thanks.aspx and
then click Open. Then do the same for the ViewGuestBook.aspx page to add these
two web forms to the project (see figure 7-42 and 7-43).
Figure 7-42. Adding Thanks.aspx to the project
Figure 7-43. Adding ViewGuestBook.aspx to the project
ViewGuestBook.aspx
The ViewGuestBook.aspx from contains two controls, a DataGrid control and a
button control (see Table 7-7).
Table 7-7. Web Controls of View Guest Book.aspx
CONTROL |
TYPE |
DESCRIPTION |
DataGrid1 |
<asp:DataGrid> |
Displays guest
book entries from the database |
Button1 |
<asp:button> |
Navigates to the
home page |
The DataGrid control displays the guest book entire from the database. The code
for populating the DataGrid from the database is on the Page_Load event of the
form.
I've used OleDbDataAdapter and DataSet to get the data from the database. As
discussed, the Data Source 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 7-8 shows the Page_load event code.
Listing 7-8. PageLoad event handler code of ViewGuestBook.aspx
protected
void Page_Load(object
sender, EventArgs e)
{
// Create a connection object
OleDbConnection
conn = new OleDbConnection();
conn.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=C:/GuestBook.mdb";
string sql = "SELECT
* FROM Guest";
// Create a data adapter
OleDbDataAdapter
da = new OleDbDataAdapter(sql, conn);
// Create and fill dataset and bind it
to the data grid
DataSet ds = new
DataSet();
da.Fill(ds,
"Guest");
DataGrid1.DataSource = ds.Tables["Guest"].DefaultView;
DataGrid1.DataBind();
}
The button–click event handler opens the how page, and the code looks like the
following:
protected void
Button3_Click(object sender,
EventArgs e)
{
Response.Redirect("http://www.c-sharpcorner.com");
}
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 simple message. The
buttons are responsible for navigating through the ViewGuestBook.aspx page or
the site home page. Table 7-8 lists the controls for Thank.aspx.
Table 7-8 Controls of Thanks. aspx page
CONTROL |
TYPE |
DESCRIPTION |
ViewGuestBookButton |
<asp:button> |
Calls
ViewGuestBook.aspx page |
GoHomeButton |
<asp:button> |
Navigates the
browser to the site home page |
The Thanks.aspx page looks like figure 7-44.
Figure 7-44. Thank you!
Listing 7-9 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
http://www.c-sharpcorner.com. Obviously, you can call your Web site's home page.
The View Guest Book button calls ViewGuestBook.aspx.
Listing 7-9. The Go Home and View Book buttons code
protected void
Button3_Click(object sender,
EventArgs e)
{
Response.Redirect("http://www.c-sharpcorner.com");
}
private void
ViewGuestBookButton_Click(object sender, System.EventArgs
e)
{
Response.Redirect("ViewGuestBook.aspx");
}
private void
GoHomeButton_Click(object sender, System.EventArgs
e)
{
Response.Redirect("http://www.c-sharpcorner.com");
}
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 that you usually do in a guest book. The output of the program looks
like figure 7-45.
Figure 7-45. Welcome to my guest book
As you can see from figure 7-45, I added a new record by filling data in the
field and clicking the Sign In GuestBook button. The Next page displayed is the Thanks page, which looks like figure 7-46.
Figure 7-46. The Thanks page of the guest book
Now, if you View the Guest Book, it look like figure 7-47. As you'll notice, I
have couple of extra records in my guest book.
Figure 7-47. My Guest book entries
Conclusion
Hope this article would have helped you in understanding Creating a Guest Book
in ASP.NET . See other articles on
the website also for further reference.
|
This essential guide
to Microsoft's ADO.NET overviews C#, then leads you toward deeper
understanding of ADO.NET. |