Asp.Net Data-Bound Controls Example

For more information visit my site: New AspDotNet Concepts

Hai Friend Today I developed task using Repeater Control ,where user comment on the webpage the data is displayed below with the time.

                           contect us

Aspx code

  1. <body style="background-image: url('webdesign_1338484318_44.jpg')">  
  2.     <form id="form1" runat="server">  
  3.     <div align="center">  
  4.     <asp:Label ID="lblresults" runat="server" Text="" ForeColor="Red"></asp:Label>  
  5.       
  6.     <h2 >Contact US</h2>  
  7.     <table>  
  8.     <tr>  
  9.     <td><b class="h1">Enter Name</b></td>  
  10.     <td>:</td>  
  11.     <td><asp:TextBox ID="txtname" runat="server" CssClass="h1" ToolTip="Enter Your Name"></asp:TextBox></td>  
  12.     </tr>  
  13.      <tr>  
  14.     <td><b class="h1">Enter Subject</b></td>  
  15.     <td>:</td>  
  16.     <td><asp:TextBox ID="txtsubject" runat="server" CssClass="h1" ToolTip="Enter Your Name"></asp:TextBox></td>  
  17.     </tr>  
  18.      <tr>  
  19.     <td><b class="h1">Enter Comments</b></td>  
  20.     <td>:</td>  
  21.     <td><asp:TextBox CssClass="h1" ID="txtcomments" runat="server" TextMode="MultiLine"   
  22.             ToolTip="Enter Your Name" Height="80px" style="margin-left: 0px"   
  23.             Width="212px"></asp:TextBox></td>  
  24.     </tr>  
  25.     <tr>  
  26.     <td>  
  27.       
  28.     </td>  
  29.     <td></td>  
  30.     <td>  
  31.         <br />  
  32.     <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" />  
  33.     </td>  
  34.     </tr>  
  35.     </table >  
  36.     </div>  
  37.     <div align="center">  
  38.     <asp:Repeater ID="repeaterdata" runat="server" >  
  39.       
  40.       
  41.     <HeaderTemplate>  
  42.     <table  align="center">  
  43.     <tr>  
  44.     <td> <h3    class="h1">Comments</h3>  
  45. </td>  
  46.     </tr>  
  47.     </table>  
  48.     </HeaderTemplate>  
  49.     <ItemTemplate>  
  50.     <table style="border=1px" >  
  51.     <tr>  
  52.     <td>  
  53.     <table border="1px" style="background-color:Orange">  
  54.     <table style="background-color:Orange" >  
  55.     <tr>  
  56.     <td>  
  57.     <asp:Label BackColor="Orange" ID="Label1" runat="server" Text='<%#Bind("sno") %>'></asp:Label>  )  
  58.     <b>Subject</b>   <asp:Label BackColor="Orange"ID="lblsubject"runat="server"Text='<%#Bind("subject") %>'></asp:Label>    </td>  
  59.     </tr>  
  60.     </table>  
  61.   
  62.     </td>  
  63.     </tr>  
  64.     <tr>  
  65.     <td>  
  66.     <asp:Label ID="lblcomments" runat="server" Text='<%#Bind("comments") %>'></asp:Label>  
  67.     </td>  
  68.     </tr>  
  69.     <tr>  
  70.     <td>  
  71.       
  72.     <table border="1px" style="background-color:Green">  
  73.       
  74.     <tr>  
  75.     <td>  
  76.     Post By    <asp:Label ID="lblpost"runat="server"Text='<%#Bind("username") %>'></asp:Label>  
  77.     </td>  
  78.       
  79.     <td>       
  80.     Create Date   :   <asp:Label ID="lbldate" runat="server" Text='<%#Bind("postedDate") %>'></asp:Label>  
  81.     </td>  
  82.     </tr>  
  83.     </table>  
  84.     </td>  
  85.     </tr>  
  86.     </table>  
  87.       
  88.     </ItemTemplate>  
  89.     </asp:Repeater>  
  90.     </div>  
  91.     </form>  
  92. </body>  
Aspx code file
  1. using System.Data;  
  2. using System.Data.SqlClient;  
  3. using System.Configuration;  
  4.   
  5. public partial class WebBasedComments: System.Web.UI.Page  
  6. {  
  7.     string connentionconfig = ConfigurationManager.ConnectionStrings["mydataconfig"].ToString();  
  8.     SqlConnection sqlcon = null;  
  9.     SqlCommand sqlcmd = null;  
  10.     DataSet ds = null;  
  11.     SqlDataAdapter sqldap = null;  
  12.     protected voidPage_Load(object sender, EventArgs e)  
  13.     {  
  14.         if(!Page.IsPostBack)  
  15.         {  
  16.             datarepeater();  
  17.       
  18.         }}  

  19.     public voiddatarepeater()  
  20.     {  
  21.         sqlcon = new SqlConnection(connentionconfig);  
  22.         sqlcmd = new SqlCommand();  
  23.           
  24.         sqlcmd.Connection = sqlcon;  
  25.         sqlcmd.CommandType = CommandType.StoredProcedure;  
  26.         sqlcmd.CommandText = "myinsertmahesh";  
  27.         sqldap = new SqlDataAdapter(sqlcmd);  
  28.         ds = new DataSet();  
  29.         sqldap.Fill(ds);  
  30.         repeaterdata.DataSource=ds;  
  31.         repeaterdata.DataBind();  
  32.     }        
  33.   
  34.     protected voidbtnsubmit_Click(object sender, EventArgs e)  
  35.     {  
  36.         submiddata(txtname.Text, txtsubject.Text, txtcomments.Text);  
  37.         datarepeater();  
  38.   
  39.         lblresults.Text = "data Inserted Sucessfully";  
  40.     }  
  41.   
  42.     public voidsubmiddata(string username, string subject, stringcomments)  
  43.     {  
  44.         sqlcon = new SqlConnection(connentionconfig);  
  45.         sqlcmd = new SqlCommand();  
  46.         sqlcon.Open();  
  47.         sqlcmd.Connection = sqlcon;  
  48.         sqlcmd.CommandType = CommandType.StoredProcedure;  
  49.         sqlcmd.CommandText = "mydatainsert";  
  50.         sqlcmd.Parameters.AddWithValue("@spusername",username);  
  51.           
  52.         sqlcmd.Parameters.AddWithValue("@spsubject",subject);  
  53.   
  54.         sqlcmd.Parameters.AddWithValue("@spcomments",comments);  
  55.         sqlcmd.Parameters.AddWithValue("@spdatetime",DateTime.Now);  
  56.         sqlcmd.ExecuteNonQuery();  
  57.         sqlcon.Close();    
  58.   
  59.     }  
  60.   
  61. }  
DataBase Queries 
  1. create database skyworld  
  2. use skyworld  
  3.   
  4. create table repeater(sno int identity(1,1),username varchar(30),subject varchar(max),comments varchar(30),postedDate datetime)  
  5.   
  6. select * from repeater  
  7. create procedure mydatainsert  
  8. (@spusername varchar(30),@spsubject varchar(max),@spcomments varchar(30),@spdatetime varchar(30))  
  9. as  
  10. begin  
  11. insert into repeater values(@spusername,@spsubject,@spcomments,@spdatetime)  
  12. end  
  13. go  
  14.   
  15. create procedure myinsertmahesh  
  16. as  
  17. begin  
  18. select * from repeater order by sno desc  
  19. end  
  20. go