Saturday, 11 April 2015

Comment System OR Form and Display In GridView Using C# In Asp.Net

4/11/2015 - By Pranav Singh 4

This article will show you how you can create a comment post form in asp.net using c#.net and display the posted comment in the gridview control.

Here is the sql table to save the posted comment.



So for this article first we will create a new asp.net application and add the below code.\


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Comment System OR Comment Form and Display In GridView Using C# In Asp.Net</title>
    <style type="text/css">
        body
        {
            font-family:Tahoma;
            font-size:11pt;
        }
        .style1 {
            color: #FF0000;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%">
        <tr>
            <td align="right"> </td>
            <td align="left">
                <asp:Label ID="lblmessage" runat="server" style="color: #3366FF" Text=""></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="right">Name
            </td>
            <td align="left">
                <asp:TextBox ID="txtName" runat="server" Width="300px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                    ControlToValidate="txtName" ErrorMessage="Please enter name"
                    style="color: #FF0000"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="right">Email Id
            </td>
            <td align="left">
                <asp:TextBox ID="txtEmailId" runat="server" Width="300px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                    ControlToValidate="txtEmailId" ErrorMessage="Please enter email id"
                    style="color: #FF0000"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
                    ControlToValidate="txtEmailId" CssClass="style1"
                    ErrorMessage="Please enter a valid email id"
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td align="right">Comment
            </td>
            <td align="left">
                <asp:TextBox ID="txtComment" runat="server" Height="87px" TextMode="MultiLine"
                    Width="300px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                    ControlToValidate="txtComment" ErrorMessage="Please enter some comment"
                    style="color: #FF0000"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="center" colspan="2">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit"
                    onclick="btnSubmit_Click" />
            </td>
        </tr>
        <tr>
            <td align="left" colspan="2">
                <asp:GridView ID="gdvUserComment" runat="server" AutoGenerateColumns="False"
                    ShowHeader="False" Width="100%">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <table width="100%">
                                <tr>
                                <td align="left" style="width:20%"><b>Name :</b> <%#Eval("Name")%></td>
                                  <td align="left"><b>Posted Date :</b> <%#Eval("CommentDate")%></td>
                                </tr>
                                <tr>
                                <td>
                                    <hr />
                                </td>
                                </tr>
                                <tr>
                                <td colspan="2"><%#Eval("Comment")%></td>
                                </tr>
                                </table>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>
    <br />
    </form>
</body>
</html>

In above code I have created a form and after saving the record I have displayed the posted comment in the grid view.

Now we will make the auto generated column false as shown below.



After this make the header of gridview visible false. This we are doing for just displaying the header.



Now we will check the code to make the page dynamic.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindComment();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string connection = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString();
                SqlConnection con = new SqlConnection(connection);
                SqlDataAdapter da = new SqlDataAdapter("Insert into CommentSystem(Name,EmailId,Comment) values('" + txtName.Text + "','" + txtEmailId.Text + "','" + txtComment.Text + "')", con);
                con.Open();
                da.SelectCommand.ExecuteNonQuery();
                con.Close();
                lblmessage.Text = "Comment posted successfully.";
                BindComment();
            }
            catch
            {
                lblmessage.Text = "sorry Error while posting comment.";
            }
        }
        ///

        /// To bind posted comment
      ///
         private void BindComment()
        {
            string connection = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString();
            SqlConnection con = new SqlConnection(connection);
            SqlDataAdapter da = new SqlDataAdapter("Select * from CommentSystem", con);

            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                gdvUserComment.DataSource = dt;
                gdvUserComment.DataBind();
            }
        }
    }
}

       

In above code I have created a function which will fetch the record from DB and the button click function event is used for saving the record.

Now check he web.config setting to define the connection string.

  <connectionStrings>
     <add name="con" connectionString="Data Source=DELL-PC;Initial Catalog=Demo;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

Now we have done run the application and check the output

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

4 comments:

  1. Error in output sorry while posting comment

    ReplyDelete
    Replies
    1. Hi Shital what the error you are getting.

      Delete
    2. how to create dynamic divs for comment

      Delete
  2. Hello every one here I never believed in a spell casting but After 6 years of dating my ex lover, I still imagine how Dr oduma brought my ex lover back to me in just 24 hour. No one could have ever made me believe that there is a real spell caster that really work. am Sophia by name,I want to quickly tell the world that there is a real an online spell caster that is powerful and genuine, His name is Dr  oduma  He helped me recently to reunite my relationship with my ex lover who left me, When i contacted Dr oduma Whats App +27638438737 he cast a love spell for me and my ex lover who said he doesn't have anything to do with me again called me and started begging me. he is back now with so much love and caring. today i am glad to let you all know that this spell caster have the powers of bring lovers back. because i am now happy with my lover,and the most surprise,is that our love is very strong,every day is happiness and joy. and there is nothing like been with the man you love.i am so happy my love is back to me with the help of Dr  oduma  – Supernatural Power if you have similar problem here's his contact WhatsApp +27638438737 and Email: pt066709@gmail.com 

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top