How to Insert and Retrieve the data to Repeater in Asp.net.
Here is the Example for inserting and retrieving the data for the Comments Part. The posts given by the user are saved and viewed in the Repeater List.To implement this concept you need to follow the below steps
Step1 :
First you need to design a table in Sql Database to to save the records in database.
Step2:
Create a new Asp.net website in Visual Studio and write the following html code in the design part of the Default.aspx page.
Design Code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Enter Name: </td>
<td><asp:TextBox ID="txtName" runat="server"/></td>
</tr><tr>
<td>Enter Subject: </td>
<td><asp:TextBox ID="txtSubject" runat="server"/></td>
</tr><tr>
<td valign="top">Enter Comments:</td>
<td><asp:TextBox ID="txtComment" runat="server" Rows="5" Columns="20" TextMode="MultiLine"/></td>
</tr><tr>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /></td>
</tr>
</table>
</div>
<div>
<asp:Repeater ID="RepDetails" runat="server">
<HeaderTemplate>
<table style=" border:1px solid Blue; width:500px" cellpadding="0">
<tr style="background-color:Blue; color:White">
<td colspan="2">
<b>Comments</b>
</td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted Blue; width:500px" >
<tr><td>
Subject:
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Subject") %>' Font-Bold="true"/>
</td>/tr>
</table>
</td></tr>
<tr><td>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment") %>'/>
</td></tr>
<tr><td>
<table style="background-color:#EBEFF0;border-top:1px dotted Blue;border-bottom:1px solid Blue; width:500px" >
<tr>
<td>Post By: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("UserName") %>'/></td>
<td>Created Date:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("PostedDate") %>'/></td>
</tr>
</table>
</td></tr>
<tr>
<td colspan="2"> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Step3:
Now open the Default.aspx.cs page and write the following source code
Default.aspx.cs Page :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default3 :
System.Web.UI.Page
{
SqlConnection con = new
SqlConnection("Data
Source=sridhar;Initial Catalog=Register;User ID=sa;Password=123");
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeaterData();
}
}
protected void
btnSubmit_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new
SqlCommand("insert
into Repeater_Table (UserName,Subject,Comment,PostedDate)
values(@userName,@subject,@comment,@postedDate)", con);
cmd.Parameters.AddWithValue("@userName",
txtName.Text);
cmd.Parameters.AddWithValue("@subject",
txtSubject.Text);
cmd.Parameters.AddWithValue("@comment",
txtComment.Text);
cmd.Parameters.AddWithValue("@postedDate",
DateTime.Now);
cmd.ExecuteNonQuery();
con.Close();
txtName.Text = string.Empty;
txtSubject.Text = string.Empty;
txtComment.Text = string.Empty;
BindRepeaterData();
}
protected void
BindRepeaterData()
{
con.Open();
SqlCommand cmd = new
SqlCommand("select
* from Repeater_Table Order By PostedDate desc", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new
SqlDataAdapter(cmd);
da.Fill(ds);
RepDetails.DataSource = ds;
RepDetails.DataBind();
con.Close();
}
}Step4:
Now build the Solution and Debug it for the output.
Output Page :
No comments:
Post a Comment