Add,Update,Delete,paging with DetailsView in asp.net c#
Introduction: In this article i will show you how to bind DetailsView in asp.net c# . I have implemented all operation like paging ,add ,edit and delete in DetailsView in asp.net c# . In following i have written the Css class to format the DetaildView.
Html Code for DetailsView in asp.net :
Introduction: In this article i will show you how to bind DetailsView in asp.net c# . I have implemented all operation like paging ,add ,edit and delete in DetailsView in asp.net c# . In following i have written the Css class to format the DetaildView.
Html Code for DetailsView in asp.net :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DetailView.aspx.cs" Inherits="HamidSite.DetailView" %> <!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 id="Head1" runat="server"> <title></title> <style type="text/css"> .DetailsViewClass { font-family: verdana,arial,sans-serif; font-size: 11px; color: #333333; border-width: 1px; border-color: #999999; border-collapse: collapse; border-style: solid; } .Header { background: #b5cfd2; border-width: 1px; padding: 8px; border-style: solid; border-color: #999999; } .Foter { background: #dcddc0; border-width: 1px; padding: 8px; border-style: solid; border-color: #999999; } .btn { background: #ffffff; border-width: 1px; padding: 2px; border-style: solid; border-color: #999999; font-family: verdana,arial,sans-serif; font-size: 11px; } .textbox { border-width: 1px; padding: 1px; border-style: solid; border-color: #999999; font-family: verdana,arial,sans-serif; font-size: 11px; width: 100px; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView ID="DetailsViewExample" AllowPaging="true" AutoGenerateRows="false" runat="server" Height="50px" CssClass="DetailsViewClass" CellPadding="4" OnPageIndexChanging="DetailsViewExample_PageIndexChanging" OnItemCommand="DetailsViewExample_ItemCommand1" OnItemUpdating="DetailsViewExample_ItemUpdating" OnModeChanging="DetailsViewExample_ModeChanging" OnItemInserting="DetailsViewExample_ItemInserting" Width="270px" OnItemDeleting="DetailsViewExample_ItemDeleting"> <Fields> <asp:TemplateField HeaderText="First Name"> <ItemTemplate> <asp:Label ID="lblID" Text='<%# Eval("ID") %>' Visible="false" runat="server"></asp:Label> <asp:Label ID="lblFirstName" Text='<%# Eval("FirstName") %>' runat="server"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lblIDEdit" Text='<%# Eval("ID") %>' Visible="false" runat="server"></asp:Label> <asp:TextBox ID="txtFirstname" Text='<%# Eval("FirstName") %>' runat="server" CssClass="textbox"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Last Name"> <ItemTemplate> <asp:Label ID="lblLastName" Text='<%# Eval("LastName") %>' runat="server"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtLastName" Text='<%# Eval("LastName") %>' runat="server" CssClass="textbox"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="City"> <ItemTemplate> <asp:Label ID="lbldob" Text='<%# Eval("City") %>' runat="server"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtCity" Text='<%# Eval("City") %>' runat="server" CssClass="textbox"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address"> <ItemTemplate> <asp:Label ID="lblAddress" Text='<%# Eval("Address") %>' runat="server"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtAddress" Text='<%# Eval("Address") %>' runat="server" CssClass="textbox"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Pin No"> <ItemTemplate> <asp:Label ID="lblPinNo" Text='<%# Eval("PinNo") %>' runat="server"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtPinNo" Text='<%# Eval("PinNo") %>' runat="server" CssClass="textbox"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Mobile No"> <ItemTemplate> <asp:Label ID="lblMobileNo" Text='<%# Eval("MobileNo") %>' runat="server"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtMobileNo" Text='<%# Eval("MobileNo") %>' runat="server" CssClass="textbox"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:CommandField Visible="true" ShowInsertButton="true" ShowCancelButton="true" ShowDeleteButton="true" ShowEditButton="true" /> </Fields> <PagerStyle CssClass="Foter" /> <FieldHeaderStyle Width="80px" CssClass="Header" /> </asp:DetailsView> </div> </form> </body> </html>C# Code for DetailsView in asp.net c# :
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace HamidSite { public partial class DetailView : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindDetailtView(); } } private void bindDetailtView() { try { DataSet Ds = GetDataSet("Select * from Employee"); DetailsViewExample.DataSource = Ds; DetailsViewExample.DataBind(); } catch (Exception ex) { throw ex; } } private DataSet GetDataSet(string Query) { DataSet Ds = new DataSet(); try { string strCon = @"Data Source=ServerName;Initial Catalog=Test;Integrated Security=True"; SqlConnection Con = new SqlConnection(strCon); SqlDataAdapter Da = new SqlDataAdapter(Query, Con); Da.Fill(Ds); } catch (Exception) { } return Ds; } private void ExecuteQuery(string Query) { try { string strCon = @"Data Source=ServerName;Initial Catalog=Test;Integrated Security=True"; SqlConnection Con = new SqlConnection(strCon); Con.Open(); SqlCommand cmd = new SqlCommand(Query, Con); cmd.ExecuteNonQuery(); Con.Close(); } catch (Exception) { } } protected void DetailsViewExample_PageIndexChanging(object sender, DetailsViewPageEventArgs e) { DetailsViewExample.PageIndex = e.NewPageIndex; bindDetailtView(); } protected void DetailsViewExample_ItemCommand1(object sender, DetailsViewCommandEventArgs e) { switch (e.CommandName.ToString()) { case "Edit": DetailsViewExample.ChangeMode(DetailsViewMode.Edit); bindDetailtView(); break; case "Cancel": DetailsViewExample.ChangeMode(DetailsViewMode.ReadOnly); bindDetailtView(); break; case "New": DetailsViewExample.ChangeMode(DetailsViewMode.Insert); bindDetailtView(); break; } } protected void DetailsViewExample_ModeChanging(object sender, DetailsViewModeEventArgs e) { } protected void DetailsViewExample_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { TextBox txtFirstname = (TextBox)DetailsViewExample.FindControl("txtFirstname"); TextBox txtLastName = (TextBox)DetailsViewExample.FindControl("txtLastName"); TextBox txtCity = (TextBox)DetailsViewExample.FindControl("txtCity"); TextBox txtAddress = (TextBox)DetailsViewExample.FindControl("txtAddress"); TextBox txtPinNo = (TextBox)DetailsViewExample.FindControl("txtPinNo"); TextBox txtMobileNo = (TextBox)DetailsViewExample.FindControl("txtMobileNo"); Label lblIDEdit = (Label)DetailsViewExample.FindControl("lblIDEdit"); string Query = "Update Employee Set FirstName='" + txtFirstname.Text + "' ,LastName ='" + txtLastName.Text + "' ,City ='" + txtCity.Text + "',Address='" + txtAddress.Text + "',PinNo='" + txtPinNo.Text + "',MobileNo='" + txtMobileNo.Text + "' where ID =" + lblIDEdit.Text; ExecuteQuery(Query); DetailsViewExample.ChangeMode(DetailsViewMode.ReadOnly); bindDetailtView(); } protected void DetailsViewExample_ItemInserting(object sender, DetailsViewInsertEventArgs e) { TextBox txtFirstname = (TextBox)DetailsViewExample.FindControl("txtFirstname"); TextBox txtLastName = (TextBox)DetailsViewExample.FindControl("txtLastName"); TextBox txtCity = (TextBox)DetailsViewExample.FindControl("txtCity"); TextBox txtAddress = (TextBox)DetailsViewExample.FindControl("txtAddress"); TextBox txtPinNo = (TextBox)DetailsViewExample.FindControl("txtPinNo"); TextBox txtMobileNo = (TextBox)DetailsViewExample.FindControl("txtMobileNo"); string Query = "Insert into Employee ([FirstName] ,[LastName] ,[City] ,[Address] ,[PinNo] ,[MobileNo]) values ('" + txtFirstname.Text + "' ,'" + txtLastName.Text + "' ,'" + txtCity.Text + "','" + txtAddress.Text + "','" + txtPinNo.Text + "','" + txtMobileNo.Text + "')"; ExecuteQuery(Query); DetailsViewExample.ChangeMode(DetailsViewMode.ReadOnly); bindDetailtView(); } protected void DetailsViewExample_ItemDeleting(object sender, DetailsViewDeleteEventArgs e) { Label lblID = (Label)DetailsViewExample.FindControl("lblID"); string Query = "Delete from Employee where ID =" + lblID.Text; ExecuteQuery(Query); bindDetailtView(); } } }
I like to make friends with you,haha.
ReplyDelete----------------------------------------------------------------------------------------------------------------------------------------
Rc Helicopter|Mini Rc Helicopter|Rc Helicopters
I believe that is among the most vital info for me.
ReplyDeleteAnd i'm satisfied reading your article. But wanna observation on few normal issues, The web site style is perfect, the articles is actually nice : D. Just right job, cheers
My page ... bd web searh
this error 'ASP.userprofile_aspx' does not contain a definition for 'DetailsView1_ModeChanging' and no extension method 'DetailsView1_ModeChanging' accepting a first argument of type 'ASP.userprofile_aspx' could be found (are you missing a using directive or an assembly reference?)
ReplyDeletethax gr8
ReplyDeleteNice. Thanq:-)
ReplyDeleteI googled many sites and blogs... But I didn't get answer any where .... Finally got in this Blog... Thanq For Posting... Nice Explanation....
ReplyDeleteVery good!! Thx.
ReplyDeleteVery interesting and good Explanation on asp .net ..
ReplyDeleteASP.NET Training
ASP NET Training
ASP.NET Online Training
C# Training
C# Online Training
C-Sharp Training
Dot Net Training in Chennai
.Net Online Training
ASP.NET Training
Very interesting and good Explanation on asp .net ..
ReplyDeleteASP.NET Training
ASP NET Training
ASP.NET Online Training
C# Training
C# Online Training
C-Sharp Training
Dot Net Training in Chennai
.Net Online Training
ASP.NET Training
ReplyDeleteIt is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. Dot Net Training in chennai | dot net training in chennai velachery
Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training and placement in chennai | dot net training institute in velachery
ReplyDeleteSomebody necessarily help to make severely posts I might state. This is the first time I frequented your website page and to this point? I surprised with the research you made to create this particular post extraordinary. Well done admin..
ReplyDeleteDot Net Training in Chennai
t is very good blog and useful for students and developer ,Thanks for sharing
ReplyDelete.Net Online Training
Dot Net Online Training Bangalore
.Net Online Course