'D:Guestguestd - MDF' 'Guestlog' 'D:Guestguestl - LDF': / Domain Name Might Be Your Comp Uter Name
'D:Guestguestd - MDF' 'Guestlog' 'D:Guestguestl - LDF': / Domain Name Might Be Your Comp Uter Name
NET Guestbook
(Page 1 of 8 )
We are going to see the steps involved in designing a guestbook using ASP.NET and SQLServer as the
backend. Guestbooks are an important tool to know about your site’s visitors and their opinions. I would like
to give you an idea of a basic guestbook web application in this article.
Requisites:
1) Windows 2000 or Windows XP or Windows 2003
2) Microsoft SQL Server 7.0 or 2000
3) Internet Information Server
4) Knowledge about VB.NET.
5) Basics in SQL
We need a database to save the posted comments. The database can be anything from a simple text file to
a sophisticated database engine. In this article I’ve used SQL Server 2000 (MSDE).
Warning: Do not use the below script on a production database server. Use it on a SQL server that does
not contain any sensitive data.
Sql Script:
filename='d:guestguestD.mdf')
log on
(name='guestLog',
filename='d:guestguestL.ldf')
go
use guestbook
use guestbook
exec sp_grantDBaccess 'YourDomainNameaspnet' /*domain name might be your comp
uter name */
exec sp_addsrvrolemember 'YourDomaiNameaspnet','dbcreator'
exec sp_helpuser
filename='d:guestguestD.mdf')
log on
(name='guestLog',
filename='d:guestguestL.ldf')
go
use guestbook
use guestbook
exec sp_grantDBaccess 'YourDomainNameaspnet' /*domain name might be your comp
uter name */
exec sp_addsrvrolemember 'YourDomaiNameaspnet','dbcreator'
exec sp_helpuser
go
create table feedback(id int primary key identity(1,1),name varchar(50)not nul
l ,email varchar(200)not null,comments varchar(1000)not null,rating int not nu
ll) --create table
select * from feedback -- display the empty table
go
/* sample feedbacks */
insert into feedback values('Abhiram','[email protected]','I like this
site very much.It was not flashy , but informative',3)
insert into feedback values('Samu','[email protected]','I don''t think this
site is worth a visit',1)
insert into feedback values('Kannan','[email protected]','Exactly what are you t
rying to say?',0)
insert into feedback values('Anand','[email protected]','Na! this site is a crap'
,1)
insert into feedback values('Lancer','[email protected]','cool site buddy'
,3)
go
select * from feedback -- display the populated table
go
select count(*) from feedback -- Number of records in the table
go
--Note:Bold letters represents comments
(Page 3 of 8 )
Populate the drop down box as shown in the below figure. To open the ListItem CollectionEditor, select the
Item property in the property sheet. ListItem editor will appear as show below.
Use Add item to add Text and Value. Make sure you always enter only integers in the Value field and user-
friendly remarks in the Text Field.
All the aforementioned controls are available in the studio toolbox.
Validators
The good news about ASP.NET is that there is no need for a developer to code validators, instead of that,
Microsoft has introduced a set of validators for those mundane tasks. I’ve included ASP:
RequiredFieldValidator in this web app. The main function of this validator is to check whether there are
inputs in the bounded controls. In our case , all the textboxes. Drag and Drop four Required field validators
and bind through the controls as mentioned below.
(Page 4 of 8 )
Please refer to the following link about adding new data sources to the server explorer. After connecting to
the database, drill down to the feedback table node. Drag and drop the feedback table to the
postcomments.aspx design view. The IDE will design the SqlDataAdapter and SqlConnection components
and place it on the component tray as shown:
Right click the SqlDataAdapter1 and click the Generate Dataset option. Accept the dataset name and click
OK.
We are now finished connecting to the guestbook database. It’s time for coding!
Inserting Data
After successfully connecting to the database, we have to fill the dataset with the data posted by the
visitors. We do this by using the fill method of SqlDataAdapter inside the Page_Load event handler.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventAr
gs) Handles MyBase.Load
Line1: SqlDataAdapter1.Fill (DataSet11)
End Sub
Line1: The adapter fills the dataset with the data from the table
(Page 5 of 8 )
When the user clicks the Link Label (llPost), data posting starts. We handle it by implementing the
LLPOST.CLICK event handler as given below:
Private Sub llPost_Click(ByVal sender As System.Object, ByVal e As System.Even
tArgs) Handles llPost.Click
Line1: Dim insertData As DataRow = DataSet11.Tables("feedback").NewRow
'create a new row
'populate the datarow with data from the form
Line2: insertData("name") = txtName.Text
Line3: insertData("email") = txtEmail.Text
Line4: insertData("comments") = txtComments.Text
Line5: insertData("rating") = CInt(ddnRating.SelectedItem.Value)
Line6: DataSet11.Tables("feedback").Rows.Add(insertData) 'add the popu
lated row to the dataset
Try
Line7: SqlDataAdapter1.Update(DataSet11.Tables("feedback")) 'update
the database
'always close the connection
Line8: SqlConnection1.Close()
Line9: Response.Redirect("viewcomments.aspx", True)
Line10: Catch ex As SqlClient.SqlException
'do something here
Line11: Catch ex As Exception
'do something here
Line12: End Try
End Sub
Line 2 - Line 5: Populates the data row with the inputs gathered from the users
Line 7: User inputs will not be fed to the database unless you call the adapter’s update method. We do it in
this line
Line 8: Always close the connection. Note the fact that we have included Codes inside a try and catch block.
DataList
Drag and drop the DataList control. Set the properties as given below.
Enter the following inside page_load subroutine
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventAr
gs) Handles MyBase.Load
'Put user code to initialize the page here
Line1: SqlDataAdapter1.Fill(DataSet11)
Line2: DataList1.DataBind()
End Sub
Switch to the HTML view (Right click the datalist and select “View HTML source”) and enter the following
code within the ASP:DATALIST tag
<HeaderTemplate>
<H1>Guestbook</H1>
<table>
</HeaderTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
<ItemTemplate>
<TR>
<TD>Name:</TD>
<TD><%#Container.DataItem("name") %></TD>
</TR>
<TR>
<TD>Email:</TD>
<TD><%# COntainer.DataItem("email") %></TD>
</TR>
<TR>
<TD>Rating:</TD>
<TD><%# Container.DataItem("rating")%>/5</TD>
</TR>
<TR>
<TD>Comments:</TD>
<TD><%# Container.DataItem("comments")%></TD>
</TR>
</ItemTemplate>
<AlternatingItemTemplate>
<TR bgcolor="InfoBackground">
<TD>Name:</TD>
<TD><%#Container.DataItem("name") %></TD>
</TR>
<TR bgcolor="InfoBackground">
<TD>Email:</TD>
<TD><%# COntainer.DataItem("email") %></TD>
</TR>
<TR bgcolor="InfoBackground">
<TD>Rating:</TD>
<TD><%# Container.DataItem("rating")%>/5</TD>
</TR>
<TR bgcolor="InfoBackground">
<TD>Comments:</TD>
<TD><%# Container.DataItem("comments")%></TD>
</TR>
</AlternatingItemTemplate>
<SeperatorTemplate>
<tr>
<td colspan=”2”><hr size=2color=”red”></td>
</tr>
Next: Explanation of the Tags >>
(Page 8 of 8 )
<ItemTemplate>
This tag wraps the HTML controls for each row in the data source.
<Header/Footer Template>
These templates are used to render controls and text at the beginning and end of the list. We have coded
<table> in the Header template and </table> in the footer template so that all the rows get contained
within a single table.
<AlternatingTemplate>
Same as ItemTemplate but used to distinguish every alternating row by means of different color, fonts, etc.,
In our app we used different color.
<SeparatorTemplate>
There many other templates for you to consider as you get more comfortable with them.