Hi
I have created a sample which full fill your requirement you need to modify the code according to your requirement.
SQL
CREATE TABLE UserComments
(
Name VARCHAR(50),
Comment NVARCHAR(250)
)
CREATE PROCEDURE GetUserComments
AS
BEGIN
SELECT Name,Comment FROM UserComments
END
HTML
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Name:-
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td>
Comment:-
</td>
<td>
<asp:TextBox ID="txtComment" TextMode="MultiLine" Rows="5" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSave" Text="Save" OnClick="Save" runat="server" />
</td>
</tr>
</table>
<br />
<asp:DataList ID="dvDetails" runat="server">
<HeaderTemplate>
<tr>
<th>
Name
</th>
<th>
Comment
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblName" Text='<%# Eval("Name") %>' runat="server" />
</td>
<td>
<asp:Label ID="lblComment" Text='<%# Eval("Comment") %>' runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:DataList>
</div>
C#
string Constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
}
protected void Save(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Constring);
string query = "INSERT INTO UserComments VALUES(@Name,@Comment)";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@Comment", txtComment.Text.Replace(Environment.NewLine, "<br/>").Trim());
cmd.ExecuteNonQuery();
con.Close();
BindDataList();
Response.Redirect(Request.Url.AbsoluteUri);
}
private void BindDataList()
{
SqlConnection con = new SqlConnection(Constring);
SqlCommand cmd = new SqlCommand("GetUserComments", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
sda.Fill(dt);
dvDetails.DataSource = dt;
dvDetails.DataBind();
}
VB.Net
Private Constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
BindDataList()
End If
End Sub
Protected Sub Save(sender As Object, e As EventArgs)
Dim con As New SqlConnection(Constring)
Dim query As String = "INSERT INTO UserComments VALUES(@Name,@Comment)"
Dim cmd As New SqlCommand(query, con)
con.Open()
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim())
cmd.Parameters.AddWithValue("@Comment", txtComment.Text.Replace(Environment.NewLine, "<br/>").Trim())
cmd.ExecuteNonQuery()
con.Close()
BindDataList()
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Private Sub BindDataList()
Dim con As New SqlConnection(Constring)
Dim cmd As New SqlCommand("GetUserComments", con)
Dim sda As New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
Dim dt As New DataTable()
sda.Fill(dt)
dvDetails.DataSource = dt
dvDetails.DataBind()
End Sub
Screenshot