Step 1

To Create the comment box, we need to add some tables and stored procedures.

Create a UserTable:
  1. CREATE TABLE [dbo].[UserTable](
  2. [UserName] [varchar](50) NOT NULL,
  3. [Password] [varchar](50) NULL,
  4. CONSTRAINT [PK_UserTable] PRIMARY KEY CLUSTERED
  5. (
  6. [UserName] ASC
  7. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  8. ) ON [PRIMARY]
  9. GO

Create a ParentComment Table:

  1. CREATE TABLE [dbo].[ParentComment](
  2. [CommentID] [int] IDENTITY(1,1) NOT NULL,
  3. [UserName] [varchar](50) NULL,
  4. [CommentMessage] [varchar](300) NULL,
  5. [CommentDate] [date] NULL,
  6. CONSTRAINT [PK_ParentComment] PRIMARY KEY CLUSTERED
  7. (
  8. [CommentID] ASC
  9. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  10. ) ON [PRIMARY]
  11. GO
  12. SET ANSI_PADDING OFF
  13. GO
  14. ALTER TABLE [dbo].[ParentComment] WITH CHECK ADD CONSTRAINT [FK_ParentComment_UserTable] FOREIGN KEY([UserName])
  15. REFERENCES [dbo].[UserTable] ([UserName])
  16. GO
  17. ALTER TABLE [dbo].[ParentComment] CHECK CONSTRAINT [FK_ParentComment_UserTable]
  18. GO
Create a ChildComment Table:
  1. CREATE TABLE [dbo].[ChildComment](
  2. [CommentID] [int] IDENTITY(1,1) NOT NULL,
  3. [UserName] [varchar](50) NULL,
  4. [CommentMessage] [varchar](300) NULL,
  5. [CommentDate] [date] NULL,
  6. [ParebCommentID] [int] NULL,
  7. CONSTRAINT [PK_ChildComment] PRIMARY KEY CLUSTERED
  8. (
  9. [CommentID] ASC
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  11. ) ON [PRIMARY]
  12. GO
  13. SET ANSI_PADDING OFF
  14. GO
  15. ALTER TABLE [dbo].[ChildComment] WITH CHECK ADD CONSTRAINT [FK_ChildComment_ParentComment] FOREIGN KEY([UserName])
  16. REFERENCES [dbo].[UserTable] ([UserName])
  17. GO
  18. ALTER TABLE [dbo].[ChildComment] CHECK CONSTRAINT [FK_ChildComment_ParentComment]
  19. GO
  20. ALTER TABLE [dbo].[ChildComment] WITH CHECK ADD CONSTRAINT [FK_ChildCommentID_ChildCommentID] FOREIGN KEY([ParebCommentID])
  21. REFERENCES [dbo].[ParentComment] ([CommentID])
  22. GO
  23. ALTER TABLE [dbo].[ChildComment] CHECK CONSTRAINT [FK_ChildCommentID_ChildCommentID]
  24. GO
Step 2

Create a stored procedure for comments session so as to check credentials of the User.
  1. create proc spCheckUserCredentials
  2. (@UserName varchar(25))
  3. as
  4. Begin
  5. Select *from UserTable where UserName=@UserName
  6. End

Insert comment for ParentComment.

  1. create proc spCommentInsert
  2. (@UserName varchar(25),
  3. @CommanetMessage varchar(300))
  4. as
  5. Begin
  6. Insert into ParentComment(UserName,CommentMessage,CommentDate)values(@UserName,@CommanetMessage,GETDATE())
  7. End

Insert reply comment for ChildComment.

  1. create proc spCommentReplyInsert(@UserName varchar(25),
  2. @CommentMessage varchar(300),
  3. @ParentCommentID int)
  4. as
  5. Begin
  6. Insert into ChildComment(UserName,CommentMessage,CommentDate,ParebCommentID)
  7. values(@UserName,@CommentMessage,GETDATE(),@ParentCommentID)
  8. End

Get Data Comment from database.

  1. create Proc spGetParetComment
  2. as
  3. Begin
  4. Select *from ParentComment
  5. End
  6. create proc spGetParentCommentByParentCommnetID(@ParentCommnetID int)
  7. as
  8. Begin
  9. select *from ChildComment where ParebCommentID=@ParentCommnetID
  10. End
Step 3

Create a webform for UserLogin.
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <table class="auto-style1">
  5. <tr>
  6. <td class="auto-style2">Login Page</td>
  7. <td> </td>
  8. </tr>
  9. <tr>
  10. <td class="auto-style2">UserName</td>
  11. <td>
  12. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  13. </td>
  14. </tr>
  15. <tr>
  16. <td class="auto-style2">Password</td>
  17. <td>
  18. <asp:TextBox ID="TextBox1" TextMode="Password" runat="server"></asp:TextBox>
  19. </td>
  20. </tr>
  21. <tr>
  22. <td class="auto-style2">
  23. <asp:Button ID="Button1" runat="server" Height="31px" Text="LOGIN" Width="115px" OnClick="Button1_Click" />
  24. </td>
  25. <td> </td>
  26. </tr>
  27. </table>
  28. <asp:Label ID="Label1" runat="server" ></asp:Label>
  29. <asp:LinkButton runat="server" ID="lnkRegistor" Text="Registration" PostBackUrl="~/Default3.aspx"></asp:LinkButton>
  30. </div>
  31. </form>
  32. </body>

Codebehind File

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void Button1_Click(object sender, EventArgs e)
  15. {
  16. SqlConnection con = new SqlConnection("Data Source=DEEPAK-PC;Initial Catalog=comment;Integrated Security=True");
  17. using (SqlCommand cmd = new SqlCommand("spCheckUserCredentials", con))
  18. {
  19. cmd.CommandType = CommandType.StoredProcedure;
  20. cmd.Parameters.AddWithValue("@UserName", TextBox2.Text);
  21. SqlDataAdapter da = new SqlDataAdapter(cmd);
  22. DataSet ds = new DataSet();
  23. da.Fill(ds);
  24. if (ds.Tables[0].Rows.Count > 0)
  25. {
  26. if (ds.Tables[0].Rows[0]["Password"].ToString() == TextBox1.Text)
  27. {
  28. Response.Redirect("Default2.aspx?User_name=" + TextBox2.Text);
  29. }
  30. else
  31. {
  32. Label1.Text = "Invailid password and Username";
  33. }
  34. }
  35. else
  36. {
  37. Label1.Text = "Invailid password and Username";
  38. }
  39. }
  40. }
  41. }

Step 4

Create a comment session webform.
Head section
  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Body section

  1. <body>
  2. <form id="form1" runat="server">
  3. <div >
  4. <div class="container" style=" padding:15px 20px; width:auto; ">
  5. <div class="row">
  6. <div class="col-md-6">
  7. <asp:TextBox ID="textComment" runat="server" CssClass="input-group" TextMode="MultiLine" Width="300px" Height="60px" Rows="15" ></asp:TextBox>
  8. <%-- <asp:Image ID="Image1" runat="server" ImageUrl="~/Image/h.jpg" Width="30px" Height="30px" style="border:1px solid #4800ff;" />--%>
  9. <br/> <asp:Button ID="btnCommentpublilsh" CssClass="btn-sm btn-default" Text="Comment" runat="server" OnClick="btnCommentPublish_Click" />
  10. </div>
  11. <div class="col-md-6"></div>
  12. </div>
  13. </div>
  14. <%--Comment Session--%>
  15. <asp:GridView ID="GridView1" BorderStyle="None" CssClass="table-responsive" Width="100%" GridLines="None" runat="server" AutoGenerateColumns="False" ShowHeader="False">
  16. <Columns>
  17. <asp:BoundField DataField="ParentCommentID" Visible="false" HeaderText="ParentCommentID" />
  18. <asp:TemplateField HeaderText="ParentMessage">
  19. <ItemTemplate>
  20. <div class="container">
  21. <div class="row">
  22. <div class="col-lg-12">
  23. <table>
  24. <tr>
  25. <td style="width:55px;vertical-align:top;padding-top:10px">
  26. <asp:Label ID="lblParentDate" runat="server" Text='<%#Bind("ParanetCommentDate") %>'></asp:Label>
  27. <br />
  28. <asp:Image ID="ImageParent" runat="server" style="width:25px;height:25px;" ImageUrl="~/Image/student-512.png" />
  29. <asp:Label ID="Label4" Font-Bold="true" ForeColor="#cc0066" runat="server" Text='<%# Bind("ParentUserName") %>'></asp:Label>
  30. </td>
  31. </tr>
  32. <tr>
  33. <td><asp:Label ID="Label1" runat="server" Text='<%# Bind("ParentCommentMessage") %>'></asp:Label></td>
  34. </tr>
  35. <tr >
  36. <td><asp:Label ID="lb1COmmenId" runat="server" Visible="false" Text='<%#Eval("ParentCommentID") %>'></asp:Label>
  37. <a class="link" id='lnkReplyParent<%#Eval("ParentCommentID") %>' href="javascript:void(0)" onclick="showReply(<%#Eval("ParentCommentID") %>);return false;">Reply</a>
  38. <a class="link" id="lnkCancle" href="javascript:void(0)" onclick="closeReply(<%#Eval("ParentCommentID") %>);return false;">Cancle</a>
  39. <div id='divReply<%#Eval("ParentCommentID") %>' style="display:none;margin-top:5px;">
  40. <asp:TextBox ID="textCommentReplyParent" CssClass="input-group" runat="server" Width="300px" TextMode="MultiLine" ></asp:TextBox>
  41. <br />
  42. <asp:Button ID="btnReplyParent" runat="server" Text="Reply" CssClass="input-group btn" OnClick="btnReplyParent_Click" /></div>
  43. </td>
  44. </tr>
  45. <tr >
  46. <td style="padding-left:100px;border-bottom:1px solid #4cff00; ">
  47. <br />
  48. <asp:GridView ID="GridView2" BorderStyle="None" GridLines="None" runat="server" AutoGenerateColumns="False" DataSource='<%# Bind("Empl") %>' ShowHeader="False">
  49. <Columns>
  50. <asp:TemplateField HeaderText="UserName">
  51. <ItemTemplate>
  52. <asp:Label ID="lblChildDate" runat="server" Text='<%#Bind("ChilCommentDate") %>'></asp:Label>
  53. <br />
  54. <asp:Image ID="ImageParent" runat="server" style="width:25px;height:25px;" ImageUrl="~/Image/student-512.png" />
  55. <asp:Label ID="Label2" runat="server" Font-Bold="true" ForeColor="#ff0066" Text='<%#Bind("UserName") %>'></asp:Label>
  56. <br />
  57. <asp:Label ID="Label3" runat="server" Text='<%# Bind("ChildcommentMessage") %>'></asp:Label>
  58. <hr />
  59. </ItemTemplate>
  60. </asp:TemplateField>
  61. </Columns>
  62. </asp:GridView>
  63. <br />
  64. </td>
  65. </tr>
  66. </table>
  67. </div>
  68. </div>
  69. </div>
  70. </ItemTemplate>
  71. </asp:TemplateField>
  72. </Columns>
  73. </asp:GridView>
  74. <br />
  75. <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllDepartmentsandEmployee" TypeName="ParentCommentIDAcess"></asp:ObjectDataSource>
  76. <br />
  77. </div>
  78. <script type="text/javascript">
  79. //GridView Comment
  80. function showReply(n) {
  81. $("#divReply" + n).show();
  82. return false;
  83. }
  84. function closeReply(n) {
  85. $("#divReply" + n).hide();
  86. return false;
  87. }
  88. </script>
  89. </form>
  90. </body>

Code Behind File

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10. public partial class Default2 : System.Web.UI.Page
  11. {
  12. SqlConnection con = new SqlConnection("Data Source=DEEPAK-PC;Initial Catalog=comment;Integrated Security=True");
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. GridView1.DataSource = ParentCommentIDAcess.GetAllDepartmentsandEmployee();
  16. GridView1.DataBind();
  17. }
  18. protected void btnCommentPublish_Click(object sender, EventArgs e)
  19. {
  20. SqlCommand cmd = new SqlCommand("spCommentInsert", con);
  21. cmd.CommandType = CommandType.StoredProcedure;
  22. cmd.Parameters.AddWithValue("@UserName", Request.QueryString["User_name"].ToString());
  23. cmd.Parameters.AddWithValue("@CommanetMessage", textComment.Text);
  24. con.Open();
  25. cmd.ExecuteNonQuery();
  26. con.Close();
  27. GridView1.DataSource = ParentCommentIDAcess.GetAllDepartmentsandEmployee();
  28. GridView1.DataBind();
  29. }
  30. protected void btnReplyParent_Click(object sender, EventArgs e)
  31. {
  32. GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
  33. Label lblchildCommentid = (Label)row.FindControl("lb1COmmenId");
  34. TextBox txtCommentParent = (TextBox)row.FindControl("textCommentReplyParent");
  35. SqlCommand cmd = new SqlCommand("spCommentReplyInsert", con);
  36. cmd.CommandType = CommandType.StoredProcedure;
  37. cmd.Parameters.AddWithValue("@UserName", Request.QueryString["User_name"].ToString());
  38. cmd.Parameters.AddWithValue("@CommentMessage", txtCommentParent.Text);
  39. cmd.Parameters.AddWithValue("@ParentCommentID", lblchildCommentid.Text);
  40. con.Open();
  41. cmd.ExecuteNonQuery();
  42. con.Close();
  43. GridView1.DataSource = ParentCommentIDAcess.GetAllDepartmentsandEmployee();
  44. GridView1.DataBind();
  45. }
  46. }

Parent Comment is a message in the comment box

Reply to a parent comment with a child comment.


Show Parent and Child Comment:
Thank you for you reading this blog.