Simple User Comments Form using Database in ASP.Net

Last Reply on Nov 19, 2013 05:12 AM By Azim

Posted on Nov 18, 2013 06:34 AM

Hello everyone,

i was owndering if you happen to have a simple Comments form syntax with CSS, so that  a person can leave his/her name, e-mail, subject and comments. the form that can write under the question when Reply is pressed by the person.

example: 

Question
       Reply

thank you. 

 

Posted on Nov 19, 2013 05:12 AM Modified on on Nov 19, 2013 05:26 AM

This way

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Repeater ID="rptComments" runat="server">
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <b>Name</b>
                    </td>
                    <td style="padding-left: 20px;">
                        <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Email</b>
                    </td>
                    <td style="padding-left: 20px;">
                        <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
                    </td>
                </tr>
                <td>
                    <b>Comment Body</b>
                </td>
                <td style="padding-left: 20px;">
                    <asp:Label ID="lblCommentBody" runat="server" Text='<%# Eval("CommentBody") %>'></asp:Label>
                </td>
            </table>
        </ItemTemplate>
    </asp:Repeater>
    <hr />
    <table>
        <tr>
            <td>
                <b>Name</b>
            </td>
            <td style="padding-left: 20px;">
                <asp:TextBox ID="txtName" runat="server" />
            </td>
            <td>
                <asp:RequiredFieldValidator ID="rfvName" runat="server" Display="Dynamic" ControlToValidate="txtName"
                    ErrorMessage="Required"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                <b>Email</b>
            </td>
            <td style="padding-left: 20px;">
                <asp:TextBox ID="txtEmail" runat="server" />
            </td>
            <td>
                <asp:RequiredFieldValidator ID="rfvEmail" runat="server" Display="Dynamic" ControlToValidate="txtEmail"
                    ErrorMessage="Required"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="revEmail" runat="server" Display="Dynamic" ControlToValidate="txtEmail"
                    ErrorMessage="Invalid Email Id" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td>
                <b>Comment Body</b>
            </td>
            <td style="padding-left: 20px;">
                <asp:TextBox ID="txtCommentBody" TextMode="MultiLine" Height="200" runat="server" />
            </td>
            <td>
                <asp:RequiredFieldValidator ID="rfvCommentBody" runat="server" Display="Dynamic"
                    ControlToValidate="txtCommentBody" ErrorMessage="Required"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td style="padding-left: 20px;">
                <asp:Button ID="btnAdd" runat="server" OnClick="AddComment" Text="Add Comment" />
            </td>
            <td>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

C#:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.PopulateComments();
        }
    }

    protected void AddComment(object sender, EventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string sqlStatment = "INSERT INTO Comments(Name,Email,CommentBody) values(@Name,@Email,@CommentBody)";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@Name", this.txtName.Text.Trim());
                cmd.Parameters.AddWithValue("@Email", this.txtEmail.Text.Trim());
                cmd.Parameters.AddWithValue("@CommentBody", txtCommentBody.Text.Trim());
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        Response.Redirect("~/AddComments.aspx");
    }

    private void PopulateComments()
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string sqlStatment = "SELECT Name,Email,CommentBody FROM Comments";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    this.rptComments.DataSource = ds;
                    this.rptComments.DataBind();
                }
            }
        }
    }

 Here i am refreshing the page by calling it self.

 Response.Redirect("~/AddComments.aspx");

 SQL:

CREATE TABLE [dbo].[Comments](
	[CommentId] [int] IDENTITY(1,1) NOT NULL,
	[CommentBody] [nvarchar](2000) NOT NULL,
	[Name] [varchar](30) NOT NULL,
	[Email] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Comments] PRIMARY KEY CLUSTERED 
(
	[CommentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO