Step 1
To Create the comment box, we need to add some tables and stored procedures.
Create a UserTable:
- CREATE TABLE [dbo].[UserTable](
- [UserName] [varchar](50) NOT NULL,
- [Password] [varchar](50) NULL,
- CONSTRAINT [PK_UserTable] PRIMARY KEY CLUSTERED
- (
- [UserName] 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
Create a ParentComment Table:
- CREATE TABLE [dbo].[ParentComment](
- [CommentID] [int] IDENTITY(1,1) NOT NULL,
- [UserName] [varchar](50) NULL,
- [CommentMessage] [varchar](300) NULL,
- [CommentDate] [date] NULL,
- CONSTRAINT [PK_ParentComment] 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
-
- ALTER TABLE [dbo].[ParentComment] WITH CHECK ADD CONSTRAINT [FK_ParentComment_UserTable] FOREIGN KEY([UserName])
- REFERENCES [dbo].[UserTable] ([UserName])
- GO
-
- ALTER TABLE [dbo].[ParentComment] CHECK CONSTRAINT [FK_ParentComment_UserTable]
- GO
Create a ChildComment Table:
- CREATE TABLE [dbo].[ChildComment](
- [CommentID] [int] IDENTITY(1,1) NOT NULL,
- [UserName] [varchar](50) NULL,
- [CommentMessage] [varchar](300) NULL,
- [CommentDate] [date] NULL,
- [ParebCommentID] [int] NULL,
- CONSTRAINT [PK_ChildComment] 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
-
- ALTER TABLE [dbo].[ChildComment] WITH CHECK ADD CONSTRAINT [FK_ChildComment_ParentComment] FOREIGN KEY([UserName])
- REFERENCES [dbo].[UserTable] ([UserName])
- GO
-
- ALTER TABLE [dbo].[ChildComment] CHECK CONSTRAINT [FK_ChildComment_ParentComment]
- GO
-
- ALTER TABLE [dbo].[ChildComment] WITH CHECK ADD CONSTRAINT [FK_ChildCommentID_ChildCommentID] FOREIGN KEY([ParebCommentID])
- REFERENCES [dbo].[ParentComment] ([CommentID])
- GO
-
- ALTER TABLE [dbo].[ChildComment] CHECK CONSTRAINT [FK_ChildCommentID_ChildCommentID]
- GO
Step 2
Create a stored procedure for comments session so as to check credentials of the User.
- create proc spCheckUserCredentials
- (@UserName varchar(25))
- as
- Begin
- Select *from UserTable where UserName=@UserName
- End
Insert comment for ParentComment.
- create proc spCommentInsert
- (@UserName varchar(25),
- @CommanetMessage varchar(300))
- as
- Begin
- Insert into ParentComment(UserName,CommentMessage,CommentDate)values(@UserName,@CommanetMessage,GETDATE())
- End
Insert reply comment for ChildComment.
- create proc spCommentReplyInsert(@UserName varchar(25),
- @CommentMessage varchar(300),
- @ParentCommentID int)
- as
- Begin
- Insert into ChildComment(UserName,CommentMessage,CommentDate,ParebCommentID)
- values(@UserName,@CommentMessage,GETDATE(),@ParentCommentID)
- End
Get Data Comment from database.
- create Proc spGetParetComment
- as
- Begin
- Select *from ParentComment
- End
- create proc spGetParentCommentByParentCommnetID(@ParentCommnetID int)
- as
- Begin
- select *from ChildComment where ParebCommentID=@ParentCommnetID
- End
Step 3
Create a webform for UserLogin.
- <body>
- <form id="form1" runat="server">
- <div>
-
- <table class="auto-style1">
- <tr>
- <td class="auto-style2">Login Page</td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2">UserName</td>
- <td>
- <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td class="auto-style2">Password</td>
- <td>
- <asp:TextBox ID="TextBox1" TextMode="Password" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td class="auto-style2">
- <asp:Button ID="Button1" runat="server" Height="31px" Text="LOGIN" Width="115px" OnClick="Button1_Click" />
- </td>
- <td> </td>
- </tr>
- </table>
- <asp:Label ID="Label1" runat="server" ></asp:Label>
- <asp:LinkButton runat="server" ID="lnkRegistor" Text="Registration" PostBackUrl="~/Default3.aspx"></asp:LinkButton>
- </div>
- </form>
- </body>
Codebehind File
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
-
-
- using System.Data;
- using System.Data.SqlClient;
-
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection("Data Source=DEEPAK-PC;Initial Catalog=comment;Integrated Security=True");
- using (SqlCommand cmd = new SqlCommand("spCheckUserCredentials", con))
- {
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@UserName", TextBox2.Text);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- if (ds.Tables[0].Rows.Count > 0)
- {
- if (ds.Tables[0].Rows[0]["Password"].ToString() == TextBox1.Text)
- {
- Response.Redirect("Default2.aspx?User_name=" + TextBox2.Text);
- }
- else
- {
- Label1.Text = "Invailid password and Username";
- }
- }
- else
- {
- Label1.Text = "Invailid password and Username";
- }
- }
-
- }
- }
Step 4
Create a comment session webform.
Head section
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Body section
Code Behind File
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
-
- public partial class Default2 : System.Web.UI.Page
- {
- SqlConnection con = new SqlConnection("Data Source=DEEPAK-PC;Initial Catalog=comment;Integrated Security=True");
- protected void Page_Load(object sender, EventArgs e)
- {
- GridView1.DataSource = ParentCommentIDAcess.GetAllDepartmentsandEmployee();
- GridView1.DataBind();
- }
- protected void btnCommentPublish_Click(object sender, EventArgs e)
- {
- SqlCommand cmd = new SqlCommand("spCommentInsert", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@UserName", Request.QueryString["User_name"].ToString());
- cmd.Parameters.AddWithValue("@CommanetMessage", textComment.Text);
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- GridView1.DataSource = ParentCommentIDAcess.GetAllDepartmentsandEmployee();
- GridView1.DataBind();
- }
- protected void btnReplyParent_Click(object sender, EventArgs e)
- {
- GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
- Label lblchildCommentid = (Label)row.FindControl("lb1COmmenId");
- TextBox txtCommentParent = (TextBox)row.FindControl("textCommentReplyParent");
- SqlCommand cmd = new SqlCommand("spCommentReplyInsert", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@UserName", Request.QueryString["User_name"].ToString());
- cmd.Parameters.AddWithValue("@CommentMessage", txtCommentParent.Text);
- cmd.Parameters.AddWithValue("@ParentCommentID", lblchildCommentid.Text);
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
-
- GridView1.DataSource = ParentCommentIDAcess.GetAllDepartmentsandEmployee();
- GridView1.DataBind();
-
- }
- }
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.