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.
Some of my previous articles are as follows: C#
Gridview Articles, Hide
Or Show Gridview Column By Column Index at RunTime In Asp.Net Using C#.Net,
Hide
Or Show Gridview Row By Column Name at RunTime In Asp.Net Using C#.Net, Detect
Header Control and Assign Value of Gridview RowDataBound In Asp.Net Using
C#.Net, Merge
Header or Apply ColumnSpan to Header of GridView In Asp.Net Using C#.Net, Display
Selected Row Value Of GridView In JavaScript Alert Message Asp.Net Using C#.Net.
Here is the sql table to save the posted comment.
<%@ 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
Error in output sorry while posting comment
ReplyDeleteHi Shital what the error you are getting.
Deletehow to create dynamic divs for comment
DeleteHello 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