Visual Basic Guestbook – Part 2

What with job hunting and doing this and that around the house, I haven’t had much opportunity to write this post and I apologise for the delay.

If you’ll remember, in Part 1, we created the database the guestbook would use and populated it with an entry that will aid us during testing.
In Part 2, we’ll be creating the forms that will allow us to view and sign the guestbook. We’ll deal with deleting entries in the next section.

View Guestbook Entries
On our home page for the guestbook (I’m using Default.aspx), we want to view the guestbook. The best way to do that is by using a Data Repeater which basically displays the data in the database table repeatedly (as it’s name suggests). So the first thing we need to do is add a SQL Data Source to the page, so drag one onto the page and configure the data source using the wizard. For this example, a standard connection string will do (i.e. Data Source=.\SQLEXPRESS; Initial Catalog=Guestbook; Integrated Security=True;). You can view the connection string if you like and proceed in the wizard to select all fields within the table in our database.

Now drag a Data Repeater onto the page and switch to your code view.
We could use the <HeaderTemplate></HeaderTemplate> tags within the data repeater to place our sign and delete links, but I found that if you do it this way, the links become part of the data repeater and you can’t use them in your code file..

The code for your links and data repeater should look like this:
<a href=”Sign.aspx”>Sign Guestbook</a> | <a onclick=”EntryDelete”>Delete Entry</a>
<asp:Data Repeater runat=”server” id=”rptGuestbook” Data Source=”SqlDataSource1″>
        <table width=”400px”>
                <td width=”10%”><%#Container.DataItem(“PostID”) %></td>
                <td width=”40%”>Posted By: <%#Container.DataItem(“UserName”) %></td>
                <td width=”25%”><a href=”mailto: <%#Container.DataItem(“Email”) %></td>
                <td width=”25%”><a href=”<%#Container.DataItem(“URL”) %></td>
            <tr><td colspan=”4″><%#Container.DataItem(“Message”) %></td></tr>

Basically all we’re doing here is to bind the information within the cells to the data in the database. This is made possible by the fact that we linked the Data Repeater to the SQL Data Source we linked to the database.

Sign Guestbook
Now we get to the fun part. Add a page to your project and call it Sign.aspx. Add 4 labels4 textboxes, and 1 button to the page. Arrange them logically and name them as shown below:

Labels Textboxes
lblName txtName
lblEmail txtEmail
lblAddress txtAddress
lblMessage txtMessage

Remember to make txtMessage a multiline textbox.

Now right-click on btnSign and add the following code to the event handler that is created:

Dim constr As String = “Data Source=.\SQLEXPRESS;” + _
                                  “Initial Catalog=Guestbook;” + _
                                  “Integrated Security=True;”

Dim con As New SqlConnection(constr)

Dim sql As String = “INSERT INTO UserPosts(UserName, Email, URL, Message) Values(@UserName, @Email, @URL, @Message)

Dim cmd As New SqlCommand(sql, con)
cmd.Parameters.Add(“@UserName”, SqlDbType.varchar, 50)
cmd.Parameters(“@UserName”).Value = txtName.Text
cmd.Parameters.Add(“@Email”, SqlDbType.varchar, 50)
cmd.Parameters(“@Email”).Value = txtEmail.Text
cmd.Parameters.Add(“@URL”, SqlDbType.varchar, 50)
cmd.Parameters(“@URL”).Value = txtAddress.Text
cmd.Parameters.Add(“@Message”, SqlDbType.text)
cmd.Parameters(“@Message”).Value = txtMessage.Text


Please remember to add your imports at the very top of your code file:
Imports System.Data
Imports System.Data.SqlClient

I haven’t tested this code yet, but I adapted if from a Visual Basic cheat sheet I downloaded as part of training material from Microsoft. To be 100% honest, I’m not entirely sure the application will accept the parameter added for the message field. Although, if this is a problem, just replace the data type in the database with varchar(1000). This might help to keep the database size down.

So that’s it for this section. I’ll try to get the next one out to you soon. I’m just working through the finer details myself 🙂



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s