How to create a guest book in ASP.Net?

A guest book for a website is a very important feature to know the feedback of the users who are using the website. This feedback from the users helps in refining the content or the services the website is intended for. Hence many of the websites which provide distinct services go for guest book features in their websites.

When you open up a guest book you should be aware that you will not only get accolades for the services but also curses when you do not provide proper services. There are usually many so called bad guys who scold you and also write some filthy stuff in your guest book. You should know how to handle such messages. You can simply delete such messages from the guest book. Hence a guest book should have a feature for you to delete unwanted messages or messages that spoil your reputation.

Creating a guest book is easy if you use ASP.Net since you can avoid a lot of coding that is done if you are using other technologies. You can use a simple Access database to hold the messages that are written by the users of the guest book. All you have to do is to read the message from the database and display it in your webform. To display them it is better to have a datagrid in the webform so that you can incorporate paging to display limited number of messages in a particular page. When you are using a database you are certain to use the ADO.Net for inserting, reading, updating, and deleting the records in the database. For the sake of simplicity we will see some code that is involved in inserting the records using ADO.Net and some code to read the records and bind them to the datagrid so that the guest book can be viewed.

You need to create an aspx page for the user to enter information about him and his comments about the site. For this purpose you need to have text box controls and label controls in your aspx page. For writing the comments you can have multi-line text box control. To have multiple lines in the textbox you can set the ‘TextMode’ property of the textbox to ‘MultiLine’. RequiredField validators can be used if do not want the user to leave any field blank. Once you create this page for entering data, you have to write the code to insert this data in to the database. Assuming that you have created the required fields in the database, the following code gives you an idea of how to insert the data in to the Guest Book database.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If IsPostBack Then
Dim Name As String = TextBox1.Text
Dim Country As String = TextBox2.Text
Dim Comment As String = TextBox3.Text
Dim conn As OleDbConnection = New OleDbConnection("----your connectionstring here----")
Dim qry As String
Dim cmd As OleDbCommand

qry = "Insert into Guest_Book (name,country,comment) VALUES ('" & Name & "',''" & Country & "','" & Comment & "'" & ")"
cmd = New OleDbCommand(qry, conn)

Try
conn.Open()
cmd.ExecuteNonQuery()
Catch e As Exception
Throw e
Finally
If conn.State = ConnectionState.Open Then
conn.Close()
End If
End Try
End If

Response.Redirect("viewGB.aspx")

End Sub

When the user is redirected to the view page to view the guest book the data from the MSAccess database is retrieved and bound to a datagrid in the view page. A sample code for this process in the view page is given below:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindDG()
End Sub
Public Sub BindDG()
Dim ds As DataSet
Dim cnn As OleDbConnection
Dim cmd As OleDbDataAdapter
Dim qry As String
qry = "select * from Guest_Book"
conn = New OleDbConnection("-your connectionstring here-")
cmd = New OleDbDataAdapter(qry, conn)

ds = New DataSet()
cmd.Fill(ds, "Guest_Book")
DataGrid1.DataSource = ds.Tables("Guest_Book").DefaultView
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindDG()
End Sub

By changing the “qry” string you can achieve task like deleting records from the database and this can be done by the owner of the website. If you work around the DataGrid during the design time you can set properties so that you can sort the records displayed in the grid of the view page. Thus a simple guest book can be created using the concepts of ADO.Net very easily in ASP.Net.

| What is Private Access Modifier in C#? | What is Protected Access Modifier in C#? | What is Protected Internal Access Modifier in C#? | What is Public Access Modifier in C#? | What is the difference between virtual and abstract keywords in .NET? | What is the importance of Microsoft Application Blocks in .NET Architecture? | What is the need for Factory Method in C# | What is the purpose of ArrayList in .NET? | What is the purpose of Datareader in ADO.NET? | What is the purpose of Dataset in ADO.NET? | What is the purpose of finally block in C#? | What is the purpose of interlocked class in .NET? | What is the purpose of main() function in C# | What is the purpose of ManualResetEvent in .NET? | What is the purpose of sealed method in C#? | What is the purpose of Thread.Join() method in .NET? | What is the purpose of Thread.Sleep() method in .NET? | What is the purpose of throw keyword in C#? | What is the usage of ENUM in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.