I am trying to use the above link but i am stuck when i am trying to convert guestbook class into vb code,
when i add these line in guestbook.aspx.vb
Public name As TextBox
Public location As TextBox
Public email As TextBox
Public website As TextBox
Public comment As TextBox
Public lbName As Label
Public lbLocation As Label
Public lbComment As Label
Public [error] As String
it gives me this error :
name is already declared as 'protected Dim with Events name As System.Web.UI.WebControls TextBox' in this class.
After viewing the article it is clear that the article was written for ASP.NET 1._
In your code I see your declaring a textbox (name as textbox). In ASP.NET 1._ you needed to do this for server controls that were in your html. In ASP.NET 2.0 you do not need to do this; it is handled for you. If your developing in ASP.NET 2.0, then you
should be able to remove all of the following from you code:
Public name As TextBox
Public location As TextBox
Public email As TextBox
Public website As TextBox
Public comment As TextBox
Public lbName As Label
Public lbLocation As Label
Public lbComment As Label
If you get stuck trying to convert this code, you could use my comments page for my blog as a model for a guestbook. Reviewing the source code for my site, you would need to do the following:
Create a new table similar to blog_Comments, change the names to reflect a guestbook, and add any additional columns you wish to use.
Copy the CommentManager.vb and change everything in it to align with your new guestbook table in the database.
Copy all of the comment stored procedures and change their names and code to align with everything in the Guestbook.vb class that you created from the CommentManager.vb
Copy the Comments.aspx and rename it to Guestbook.aspx. Change the names of controls to match Guestbook. Modify and add any additional items to the datalist. Modify the code behind to reference Guestbook instead of Comments.
I was trying to work on the same lines but its not working ,though i am not getting any errors.
The problem is when I enter name, comment etc and click on submit button it doesnot do anything, it was supposed to show the submit msg but it does nothing.
Now i will go and check your website and start again from there as suggested in ur reply.
Question 1. The objectdatasource you pasted in, is how I get the comments. That objectdatasource calls the GetComments (SelectMethod="GetComments") procedure in the CommentManager.vb (TypeName="CommentManager").
Question 2. The comments are added in the database by: When the Leave Comment button is clicked, it calls the click event of that button. In the click event of that button there is some code including:
CommentManager is the CommentManager class in the CommentManager.vb. AddComment is a procedure in the CommentManager class. mintEntryID is the Blog entry id the comment is associated with. (You will not need this for the Guestbook)
Question 3. This objectdatasource is only for displaying the Blog Entry title associated with the comments.
You'll notice in both objectdatasource tags there is a querystring parameter that is the Blog EntryID passed when they clicked the comments link for the Blog Entry. You will not need to pass parameters as you will not be associating the Guestbook items with
anything. You really only need an objectdatasource to get your guestbook records. Then you just layout the datalist they way you want it to look.
Let me see if I can help further. What fields or columns in the database table do you think you need for the guestbook?
This should get you going. You'll need to clean it up as there are a lot of references to comments.
Make a copy of Comments and rename it to Guestbook.aspx
Guestbook.aspx.vb - Replace the btnLeaveComment_Click with this one.
Protected Sub btnLeaveComment_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLeaveComment.Click
If Page.IsValid Then
Dim strUserName As String
If (User.Identity.Name Is Nothing Or User.Identity.Name = "") Then
strUserName = txtUserName.Text.Trim
Else
strUserName = User.Identity.Name
End If
Try
'Add the comment to the database using CommentManager class function AddComment
GuestbookManager.AddGuestbookEntry(strUserName, txtEmail.Text.Trim, HttpUtility.HtmlEncode(txtCommentText.Text.Trim))
'The EmailUser needs to be changed to grab the admin user name from somewhere else.
'This should not be hard coded.
Globals.EmailUser("admin", Membership.GetUser("admin").Email, "New Comment Posted!", txtCommentText.Text.Trim)
Catch ex As Exception
'The SendError needs to be changed to grab the admin user name from somewhere else.
'This should not be hard coded.
Globals.SendError("admin", "Error Sending Comment!", "An error was encountered in the btnLeaveComment_Click event while trying to call the AddComment() or EmailUser." & vbCrLf & ex.Message)
End Try
'Reset the Add Comment form
txtCommentText.Text = ""
txtUserName.Text = ""
DataList1.DataBind()
End If
End Sub Protected Sub btnLeaveComment_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLeaveComment.Click
If Page.IsValid Then
Dim strUserName As String
If (User.Identity.Name Is Nothing Or User.Identity.Name = "") Then
strUserName = txtUserName.Text.Trim
Else
strUserName = User.Identity.Name
End If
Try
'Add the comment to the database using CommentManager class function AddComment
GuestbookManager.AddGuestbookEntry(strUserName, txtEmail.Text.Trim, HttpUtility.HtmlEncode(txtCommentText.Text.Trim))
'The EmailUser needs to be changed to grab the admin user name from somewhere else.
'This should not be hard coded.
Globals.EmailUser("admin", Membership.GetUser("admin").Email, "New Comment Posted!", txtCommentText.Text.Trim)
Catch ex As Exception
'The SendError needs to be changed to grab the admin user name from somewhere else.
'This should not be hard coded.
Globals.SendError("admin", "Error Sending Comment!", "An error was encountered in the btnLeaveComment_Click event while trying to call the AddComment() or EmailUser." & vbCrLf & ex.Message)
End Try
'Reset the Add Comment form
txtCommentText.Text = ""
txtUserName.Text = ""
DataList1.DataBind()
End If
End Sub
Guestbook.aspx - Replace the whole html source with this:
<%@ Page Language="VB" MasterPageFile="~/Master Pages/Default.master" AutoEventWireup="false" ValidateRequest="false" CodeFile="Guestbook.aspx.vb" Inherits="PWS_Guestbook" title="Blog Comments" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
<div class="shim solid"></div>
<div class="page" id="Comments">
<br />
<br />
<asp:Table CellPadding="0" CellSpacing="0" CssClass="Border1px" runat="server">
<asp:TableRow>
<asp:TableCell ColumnSpan="2" Height="40" HorizontalAlign="left" CssClass="CommentPageTitle">
<asp:Image ID="Image1" ImageAlign="absmiddle" SkinID="EditIcon" Width="20px" runat="server" /> <asp:Label ID="lblPostMessage" Text="Sign the guestbook" CssClass="PostMessage" runat="server" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Width="50%" VerticalAlign="top" CssClass="TDAddComment">
<asp:Table Width="100%" CellPadding="10" CellSpacing="0" BorderWidth="0" SkinID="CommentBG" CssClass="CommentBG" runat="server">
<asp:TableRow>
<asp:TableCell VerticalAlign="top" HorizontalAlign="left">
<!--span>You can use simple HTML tags, such as <b style="white-space:nowrap;"> <b>, <i>, <a> </b></span-->
<asp:TextBox ID="txtCommentText" TextMode="multiLine" Width="400px" Height="150px" MaxLength="300" EnableViewState="false" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="txtCommentText"
SetFocusOnError="true"
Display="none"
Text="No text has been entered for a comment."
ErrorMessage="No text has been entered for a comment."
runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell VerticalAlign="top" HorizontalAlign="left">
<asp:Label ID="lblUserName" Text="Enter Your Name:" runat="server" />
<br />
<asp:TextBox ID="txtUserName" Width="250" MaxLength="49" EnableViewState="false" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidatorUserName"
ControlToValidate="txtUserName"
SetFocusOnError="true"
Text="***"
ErrorMessage="You must enter a Name."
runat="server" />
<br />
<asp:Label ID="lblEmail" Text="Enter Your Email:" runat="server" />
<br />
<asp:TextBox ID="txtEmail" runat="server" Width="250" MaxLength="49" EnableViewState="false" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell VerticalAlign="top" HorizontalAlign="left">
<asp:Button ID="btnLeaveComment" Text="Sign Guestbook" runat="server" /> <asp:Button ID="btnReturnToBlog" Text="Back to Home Page" CausesValidation="false" runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell VerticalAlign="top">
<asp:ValidationSummary ID="ValidationSummary1"
DisplayMode="BulletList"
HeaderText="Errors Encountered trying to Save:"
runat="server" />
</asp:TableCell>
</asp:TableRow>
</asp:Table></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Width="100%" CssClass="CommentsList" HorizontalAlign="left" VerticalAlign="top">
<asp:DataList ID="DataList1" runat="Server" Width="100%" ItemStyle-VerticalAlign="top" cssclass="CommentView" dataSourceID="ObjectDataSourceGuestbook"
repeatColumns="0" repeatdirection="vertical" onitemdatabound="DataList1_ItemDataBound" EnableViewState="False">
<ItemTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<span class="PostedBy"><%#Eval("Name")%> said...
<asp:ImageButton ID="ImageButton3"
Runat="server"
CommandName="Delete"
CommandArgument='<%# Eval("GuestbookID") %>'
CausesValidation="false"
Visible='<%# CBool(Roles.IsUserInRole("Administrators")) %>'
SkinID="delete"
OnClientClick="return confirm('Are you sure you want to delete this comment?');" /></span>
<br />
<br />
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td><%#Eval("Message")%><br /><br /></td>
</tr>
<tr>
<td class="DatePosted">
<%#Eval("DateCreated", "{0:f}") %></td>
</tr>
</table></td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate><hr class="hrBlogger" /></SeparatorTemplate>
<FooterTemplate></FooterTemplate>
</asp:DataList>
<asp:panel id="Panel1" runat="server" visible="false" CssClass="nullpanel">There are currently no comments for this blog entry.</asp:panel></asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
<br />
</div>
Private _GuestbookID As Integer
Private _Name As String
Private _Email As String
Private _Message As String
Private _DateCreated As DateTime
Public Sub New(ByVal intGuestbookID As Integer, ByVal strName As String, ByVal strEmail As String, ByVal strMessage As String, ByVal dateDateCreated As DateTime)
MyBase.New()
_GuestbookID = intGuestbookID
_Name = strName
_Email = strEmail
_Message = strMessage
_DateCreated = dateDateCreated
End Sub
Public ReadOnly Property GuestbookID() As Integer
Get
Return _GuestbookID
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property
Public ReadOnly Property Email() As String
Get
Return _Email
End Get
End Property
Public ReadOnly Property Message() As String
Get
Return _Message
End Get
End Property
Public ReadOnly Property DateCreated() As DateTime
Get
Return _DateCreated
End Get
End Property
End Class
Public Class GuestbookManager
Public Shared Function GetGuestbook() As Generic.List(Of Guestbook)
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("Personal").ConnectionString)
Using command As New SqlCommand("GetGuestbook", connection)
command.CommandType = CommandType.StoredProcedure
connection.Open()
Dim list As New Generic.List(Of Guestbook)()
Using reader As SqlDataReader = command.ExecuteReader()
Do While (reader.Read())
Dim temp As New Guestbook(CInt(reader("GuestbookID")), CStr(reader("Name")), CStr(reader("Email")), HttpUtility.UrlDecode((reader("Message"))), CDate(reader("DateCreated")))
list.Add(temp)
Loop
End Using
Return list
End Using
End Using
End Function
Public Shared Sub AddGuestbookEntry(ByVal Name As String, ByVal Email As String, ByVal Message As String)
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("Personal").ConnectionString)
Using command As New SqlCommand("AddGuestbookEntry", connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add(New SqlParameter("@Name", Name))
command.Parameters.Add(New SqlParameter("@Email", Email))
command.Parameters.Add(New SqlParameter("@Message", Message))
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
End Sub
Public Shared Sub DeleteComment(ByVal CommentID As Integer)
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("Personal").ConnectionString)
Using command As New SqlCommand("DELETE FROM blog_Comment WHERE CommentID =" & CommentID, connection)
command.CommandType = CommandType.Text
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
End Sub
Create a table named Guestbook and add the following columns:
GuestbookID int (make it a primary key and set it as an Identity column)
Name nvarchar(50)
Email nvarchar(50) Allow Nulls
Message nvarchar(50)
DateCreated datetime Allow Nulls (add this (getdate()) into the "Default Value or Binding" property)
Then create a stored procedure named GetGuestbook with no parameters, and add the following:
SELECT
*
FROM [Guestbook]
ORDER BY [Guestbook].[DateCreated]
RETURN
Then create a stored procedure named AddGuestbookEntry with 3 parameters, and add the following:
@Name
nvarchar(50),
@Email
nvarchar(50),
@Message
nvarchar(MAX)
AS
INSERT INTO [Guestbook] (
[Name],
[Email],
[Message] )
VALUES (
@Name,
@Email,
@Message )
RETURN
You will also need a storeed procedure for deleting a guestbook entry in case someone post an indecent entry.
gridview
Participant
949 Points
400 Posts
GuestBook page in Personal website
Jul 27, 2006 01:32 AM|LINK
I want to add GuestBook page in Personal website , i searched this forum for the same and found a post where this link was suggested :
http://www.sitepoint.com/article/create-own-guestbook-asp-net
I am trying to use the above link but i am stuck when i am trying to convert guestbook class into vb code,
when i add these line in guestbook.aspx.vb
Public name As TextBox
Public location As TextBox
Public email As TextBox
Public website As TextBox
Public comment As TextBox
Public lbName As Label
Public lbLocation As Label
Public lbComment As Label
Public [error] As String
it gives me this error :
name is already declared as 'protected Dim with Events name As System.Web.UI.WebControls TextBox' in this class.
Any help please
jwadsworth
Contributor
2378 Points
542 Posts
Re: GuestBook page in Personal website
Jul 27, 2006 09:35 PM|LINK
After viewing the article it is clear that the article was written for ASP.NET 1._
In your code I see your declaring a textbox (name as textbox). In ASP.NET 1._ you needed to do this for server controls that were in your html. In ASP.NET 2.0 you do not need to do this; it is handled for you. If your developing in ASP.NET 2.0, then you should be able to remove all of the following from you code:
Public name As TextBoxPublic location As TextBox
Public email As TextBox
Public website As TextBox
Public comment As TextBox
Public lbName As Label
Public lbLocation As Label
Public lbComment As Label
If you get stuck trying to convert this code, you could use my comments page for my blog as a model for a guestbook. Reviewing the source code for my site, you would need to do the following:
Extended Personal Site Starter kit
gridview
Participant
949 Points
400 Posts
Re: GuestBook page in Personal website
Jul 28, 2006 04:35 AM|LINK
Thanks for your reply.
I was trying to work on the same lines but its not working ,though i am not getting any errors.
The problem is when I enter name, comment etc and click on submit button it doesnot do anything, it was supposed to show the submit msg but it does nothing.
Now i will go and check your website and start again from there as suggested in ur reply.
gridview
Participant
949 Points
400 Posts
Re: GuestBook page in Personal website
Jul 28, 2006 05:10 PM|LINK
i am going through ur code as suggested.
i have few questions :
1.You are displaying comments entered by using ? :
<asp:ObjectDataSource ID="ObjectDataSourceComments"
Runat="server"
TypeName="CommentManager"
SelectMethod="GetComments">
<SelectParameters>
<asp:QueryStringParameter
Direction="Input"
Name="entryid"
QueryStringField="entryid"
Type="Int32" />
</SelectParameters>
2. Currently i couldnot understand how ur adding comments in database?
maybe figure it out in some time.
3.You are retrieving comments about particular Blog Topic by using ? :
<asp:ObjectDataSource ID="ObjectDataSourceEntry"
Runat="server"
TypeName="BlogManager"
SelectMethod="GetBlogEntry">
<SelectParameters>
<asp:QueryStringParameter
Direction="Input"
Name="entryid"
QueryStringField="entryid"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
and I dont need to include it in GuestBook ?
jwadsworth
Contributor
2378 Points
542 Posts
Re: GuestBook page in Personal website
Jul 28, 2006 05:53 PM|LINK
Question 1. The objectdatasource you pasted in, is how I get the comments. That objectdatasource calls the GetComments (SelectMethod="GetComments") procedure in the CommentManager.vb (TypeName="CommentManager").
Question 2. The comments are added in the database by: When the Leave Comment button is clicked, it calls the click event of that button. In the click event of that button there is some code including:
CommentManager.AddComment(mintEntryID, strUserName, HttpUtility.HtmlEncode(txtCommentText.Text.Trim))
CommentManager is the CommentManager class in the CommentManager.vb. AddComment is a procedure in the CommentManager class. mintEntryID is the Blog entry id the comment is associated with. (You will not need this for the Guestbook)Question 3. This objectdatasource is only for displaying the Blog Entry title associated with the comments.
You'll notice in both objectdatasource tags there is a querystring parameter that is the Blog EntryID passed when they clicked the comments link for the Blog Entry. You will not need to pass parameters as you will not be associating the Guestbook items with anything. You really only need an objectdatasource to get your guestbook records. Then you just layout the datalist they way you want it to look.
Let me see if I can help further. What fields or columns in the database table do you think you need for the guestbook?
Extended Personal Site Starter kit
gridview
Participant
949 Points
400 Posts
Re: GuestBook page in Personal website
Jul 28, 2006 05:57 PM|LINK
Thanks for your reply,
I need following fields for the guest book:
1. Name
2.Email address
3.Comments/message
jwadsworth
Contributor
2378 Points
542 Posts
Re: GuestBook page in Personal website
Jul 28, 2006 07:06 PM|LINK
This should get you going. You'll need to clean it up as there are a lot of references to comments.
Make a copy of Comments and rename it to Guestbook.aspx
Guestbook.aspx.vb - Replace the btnLeaveComment_Click with this one.
Protected Sub btnLeaveComment_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLeaveComment.Click
If Page.IsValid Then
Dim strUserName As String
If (User.Identity.Name Is Nothing Or User.Identity.Name = "") Then
strUserName = txtUserName.Text.Trim
Else
strUserName = User.Identity.Name
End If
Try
'Add the comment to the database using CommentManager class function AddComment
GuestbookManager.AddGuestbookEntry(strUserName, txtEmail.Text.Trim, HttpUtility.HtmlEncode(txtCommentText.Text.Trim))
'The EmailUser needs to be changed to grab the admin user name from somewhere else.
'This should not be hard coded.
Globals.EmailUser("admin", Membership.GetUser("admin").Email, "New Comment Posted!", txtCommentText.Text.Trim)
Catch ex As Exception
'The SendError needs to be changed to grab the admin user name from somewhere else.
'This should not be hard coded.
Globals.SendError("admin", "Error Sending Comment!", "An error was encountered in the btnLeaveComment_Click event while trying to call the AddComment() or EmailUser." & vbCrLf & ex.Message)
End Try
'Reset the Add Comment form
txtCommentText.Text = ""
txtUserName.Text = ""
DataList1.DataBind()
End If
End Sub Protected Sub btnLeaveComment_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLeaveComment.Click
If Page.IsValid Then
Dim strUserName As String
If (User.Identity.Name Is Nothing Or User.Identity.Name = "") Then
strUserName = txtUserName.Text.Trim
Else
strUserName = User.Identity.Name
End If
Try
'Add the comment to the database using CommentManager class function AddComment
GuestbookManager.AddGuestbookEntry(strUserName, txtEmail.Text.Trim, HttpUtility.HtmlEncode(txtCommentText.Text.Trim))
'The EmailUser needs to be changed to grab the admin user name from somewhere else.
'This should not be hard coded.
Globals.EmailUser("admin", Membership.GetUser("admin").Email, "New Comment Posted!", txtCommentText.Text.Trim)
Catch ex As Exception
'The SendError needs to be changed to grab the admin user name from somewhere else.
'This should not be hard coded.
Globals.SendError("admin", "Error Sending Comment!", "An error was encountered in the btnLeaveComment_Click event while trying to call the AddComment() or EmailUser." & vbCrLf & ex.Message)
End Try
'Reset the Add Comment form
txtCommentText.Text = ""
txtUserName.Text = ""
DataList1.DataBind()
End If
End Sub
Guestbook.aspx - Replace the whole html source with this:
<%@ Page Language="VB" MasterPageFile="~/Master Pages/Default.master" AutoEventWireup="false" ValidateRequest="false" CodeFile="Guestbook.aspx.vb" Inherits="PWS_Guestbook" title="Blog Comments" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
<div class="shim solid"></div>
<div class="page" id="Comments">
<br />
<br />
<asp:Table CellPadding="0" CellSpacing="0" CssClass="Border1px" runat="server">
<asp:TableRow>
<asp:TableCell ColumnSpan="2" Height="40" HorizontalAlign="left" CssClass="CommentPageTitle">
<asp:Image ID="Image1" ImageAlign="absmiddle" SkinID="EditIcon" Width="20px" runat="server" /> <asp:Label ID="lblPostMessage" Text="Sign the guestbook" CssClass="PostMessage" runat="server" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Width="50%" VerticalAlign="top" CssClass="TDAddComment">
<asp:Table Width="100%" CellPadding="10" CellSpacing="0" BorderWidth="0" SkinID="CommentBG" CssClass="CommentBG" runat="server">
<asp:TableRow>
<asp:TableCell VerticalAlign="top" HorizontalAlign="left">
<!--span>You can use simple HTML tags, such as <b style="white-space:nowrap;"> <b>, <i>, <a> </b></span-->
<asp:TextBox ID="txtCommentText" TextMode="multiLine" Width="400px" Height="150px" MaxLength="300" EnableViewState="false" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="txtCommentText"
SetFocusOnError="true"
Display="none"
Text="No text has been entered for a comment."
ErrorMessage="No text has been entered for a comment."
runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell VerticalAlign="top" HorizontalAlign="left">
<asp:Label ID="lblUserName" Text="Enter Your Name:" runat="server" />
<br />
<asp:TextBox ID="txtUserName" Width="250" MaxLength="49" EnableViewState="false" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidatorUserName"
ControlToValidate="txtUserName"
SetFocusOnError="true"
Text="***"
ErrorMessage="You must enter a Name."
runat="server" />
<br />
<asp:Label ID="lblEmail" Text="Enter Your Email:" runat="server" />
<br />
<asp:TextBox ID="txtEmail" runat="server" Width="250" MaxLength="49" EnableViewState="false" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell VerticalAlign="top" HorizontalAlign="left">
<asp:Button ID="btnLeaveComment" Text="Sign Guestbook" runat="server" /> <asp:Button ID="btnReturnToBlog" Text="Back to Home Page" CausesValidation="false" runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell VerticalAlign="top">
<asp:ValidationSummary ID="ValidationSummary1"
DisplayMode="BulletList"
HeaderText="Errors Encountered trying to Save:"
runat="server" />
</asp:TableCell>
</asp:TableRow>
</asp:Table></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Width="100%" CssClass="CommentsList" HorizontalAlign="left" VerticalAlign="top">
<asp:DataList ID="DataList1" runat="Server" Width="100%" ItemStyle-VerticalAlign="top" cssclass="CommentView" dataSourceID="ObjectDataSourceGuestbook"
repeatColumns="0" repeatdirection="vertical" onitemdatabound="DataList1_ItemDataBound" EnableViewState="False">
<ItemTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<span class="PostedBy"><%#Eval("Name")%> said...
<asp:ImageButton ID="ImageButton3"
Runat="server"
CommandName="Delete"
CommandArgument='<%# Eval("GuestbookID") %>'
CausesValidation="false"
Visible='<%# CBool(Roles.IsUserInRole("Administrators")) %>'
SkinID="delete"
OnClientClick="return confirm('Are you sure you want to delete this comment?');" /></span>
<br />
<br />
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td><%#Eval("Message")%><br /><br /></td>
</tr>
<tr>
<td class="DatePosted">
<%#Eval("DateCreated", "{0:f}") %></td>
</tr>
</table></td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate><hr class="hrBlogger" /></SeparatorTemplate>
<FooterTemplate></FooterTemplate>
</asp:DataList>
<asp:panel id="Panel1" runat="server" visible="false" CssClass="nullpanel">There are currently no comments for this blog entry.</asp:panel></asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
<br />
</div>
<asp:ObjectDataSource ID="ObjectDataSourceGuestbook" Runat="server" TypeName="GuestbookManager"
SelectMethod="GetGuestbook">
</asp:ObjectDataSource>
</asp:Content>
Extended Personal Site Starter kit
jwadsworth
Contributor
2378 Points
542 Posts
Re: GuestBook page in Personal website
Jul 28, 2006 07:08 PM|LINK
Make a copy of CommentManager.vb and rename it to GuestbookManager.vb. Replace the code with the following:
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO
Public Class Guestbook
Private _GuestbookID As Integer
Private _Name As String
Private _Email As String
Private _Message As String
Private _DateCreated As DateTime
Public Sub New(ByVal intGuestbookID As Integer, ByVal strName As String, ByVal strEmail As String, ByVal strMessage As String, ByVal dateDateCreated As DateTime)
MyBase.New()
_GuestbookID = intGuestbookID
_Name = strName
_Email = strEmail
_Message = strMessage
_DateCreated = dateDateCreated
End Sub
Public ReadOnly Property GuestbookID() As Integer
Get
Return _GuestbookID
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property
Public ReadOnly Property Email() As String
Get
Return _Email
End Get
End Property
Public ReadOnly Property Message() As String
Get
Return _Message
End Get
End Property
Public ReadOnly Property DateCreated() As DateTime
Get
Return _DateCreated
End Get
End Property
End Class
Public Class GuestbookManager
Public Shared Function GetGuestbook() As Generic.List(Of Guestbook)
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("Personal").ConnectionString)
Using command As New SqlCommand("GetGuestbook", connection)
command.CommandType = CommandType.StoredProcedure
connection.Open()
Dim list As New Generic.List(Of Guestbook)()
Using reader As SqlDataReader = command.ExecuteReader()
Do While (reader.Read())
Dim temp As New Guestbook(CInt(reader("GuestbookID")), CStr(reader("Name")), CStr(reader("Email")), HttpUtility.UrlDecode((reader("Message"))), CDate(reader("DateCreated")))
list.Add(temp)
Loop
End Using
Return list
End Using
End Using
End Function
Public Shared Sub AddGuestbookEntry(ByVal Name As String, ByVal Email As String, ByVal Message As String)
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("Personal").ConnectionString)
Using command As New SqlCommand("AddGuestbookEntry", connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add(New SqlParameter("@Name", Name))
command.Parameters.Add(New SqlParameter("@Email", Email))
command.Parameters.Add(New SqlParameter("@Message", Message))
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
End Sub
Public Shared Sub DeleteComment(ByVal CommentID As Integer)
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("Personal").ConnectionString)
Using command As New SqlCommand("DELETE FROM blog_Comment WHERE CommentID =" & CommentID, connection)
command.CommandType = CommandType.Text
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
End Sub
End Class
Extended Personal Site Starter kit
jwadsworth
Contributor
2378 Points
542 Posts
Re: GuestBook page in Personal website
Jul 28, 2006 07:18 PM|LINK
Create a table named Guestbook and add the following columns:
GuestbookID int (make it a primary key and set it as an Identity column)
Name nvarchar(50)
Email nvarchar(50) Allow Nulls
Message nvarchar(50)
DateCreated datetime Allow Nulls (add this (getdate()) into the "Default Value or Binding" property)
Then create a stored procedure named GetGuestbook with no parameters, and add the following:
SELECT
* FROM [Guestbook] ORDER BY [Guestbook].[DateCreated] RETURN Then create a stored procedure named AddGuestbookEntry with 3 parameters, and add the following:@Name
nvarchar(50),@Email
nvarchar(50),@Message
nvarchar(MAX)AS
INSERT INTO [Guestbook] ([Name],
[Email],
[Message] )
VALUES (@Name,
@Email,
@Message )
RETURN
You will also need a storeed procedure for deleting a guestbook entry in case someone post an indecent entry.Extended Personal Site Starter kit
gridview
Participant
949 Points
400 Posts
Re: GuestBook page in Personal website
Jul 28, 2006 09:07 PM|LINK
Thanks Jeremy for BOTH posts, i am working as mentioned in post,
if faced error will post.