Hi nauna,
Please refer below sample.
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>
Namespaces
C#
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
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,@CreateDate)";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Comment", txtComment.Text.Replace(Environment.NewLine, "<br/>"));
cmd.Parameters.AddWithValue("@CreateDate", DateTime.Now);
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(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindDataList()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim con As SqlConnection = New SqlConnection(Constring)
Dim query As String = "INSERT INTO UserComments VALUES(@Name,@Comment,@CreateDate)"
Dim cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Comment", txtComment.Text.Replace(Environment.NewLine, "<br/>"))
cmd.Parameters.AddWithValue("@CreateDate", DateTime.Now)
cmd.ExecuteNonQuery()
con.Close()
BindDataList()
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Private Sub BindDataList()
Dim con As SqlConnection = New SqlConnection(Constring)
Dim cmd As SqlCommand = New SqlCommand("GetUserComments", con)
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
dvDetails.DataSource = dt
dvDetails.DataBind()
End Sub
Screenshot