// Default.aspx //
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title> Detail View </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Details View Example</b><br />
<br />
<asp:Label ID="lblmsg" runat="server"></asp:Label><br /><br />
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="400px" AutoGenerateRows="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnItemUpdating="DetailsView1_ItemUpdating"
OnModeChanging="DetailsView1_ModeChanging"
OnItemCommand="DetailsView1_ItemCommand" AllowPaging="true"
oniteminserting="DetailsView1_ItemInserting"
onitemdeleting="DetailsView1_ItemDeleting"
onpageindexchanging="DetailsView1_PageIndexChanging">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbleno" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteno" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<ItemTemplate>
<asp:Label ID="lblempname" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtempname" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblsal" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Address") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtsal" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Address") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True"></asp:CommandField>
<asp:CommandField ShowInsertButton="True"></asp:CommandField>
<asp:CommandField ShowEditButton="True" CancelText="Cancel" EditText="Edit" UpdateText="Update" />
</Fields>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
</div>
</form>
</body>
</html>
// Default.aspx.cs //
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;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString);
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadDet();
}
}
void LoadDet()
{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from Add_Record", sqlcon);
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
sqlcon.Close();
DetailsView1.DataSource = dt;
DetailsView1.DataBind();
}
//Below code is used to display page wise records
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
LoadDet();
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (e.CancelingEdit == true)
{
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
LoadDet();
}
}
//Below code is used to update record details
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
string eno = ((TextBox)DetailsView1.FindControl("ID")).Text.ToString();
string empname = ((TextBox)DetailsView1.FindControl("Name")).Text.ToString();
string Add = ((TextBox)DetailsView1.FindControl("Address")).Text.ToString();
sqlcon.Open();
sqlcmd = new SqlCommand("UPDATE Add_Record SET Name='" + empname + "', Address='" + Add + "' WHERE ID='" + eno + "' ", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
LoadDet();
}
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "New")
{
DetailsView1.ChangeMode(DetailsViewMode.Insert);
LoadDet();
}
if (e.CommandName == "Edit")
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
LoadDet();
}
}
//Below code is used to insert record into database table
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
sqlcon.Open();
string eno = ((TextBox)DetailsView1.FindControl("ID")).Text.ToString();
string empname = ((TextBox)DetailsView1.FindControl("Name")).Text.ToString();
string sal = ((TextBox)DetailsView1.FindControl("Address")).Text.ToString();
SqlCommand cmd = new SqlCommand("select ID from Add_Record where ID = '" + eno + "'", sqlcon);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
lblmsg.Text = "User No already exists";
}
else
{
dr.Close();
sqlcmd = new SqlCommand("insert into Add_Record values('" + eno + "', '" + empname + "', '" + sal + "')", sqlcon);
sqlcmd.ExecuteNonQuery();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
sqlcon.Close();
LoadDet();
}
//Below code is used to delete record from database
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
string eno = ((Label)DetailsView1.Rows[0].FindControl("lbleno")).Text;
sqlcon.Open();
sqlcmd = new SqlCommand("delete from Add_Record where ID = '" + eno + "'", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
LoadDet();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title> Detail View </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Details View Example</b><br />
<br />
<asp:Label ID="lblmsg" runat="server"></asp:Label><br /><br />
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="400px" AutoGenerateRows="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnItemUpdating="DetailsView1_ItemUpdating"
OnModeChanging="DetailsView1_ModeChanging"
OnItemCommand="DetailsView1_ItemCommand" AllowPaging="true"
oniteminserting="DetailsView1_ItemInserting"
onitemdeleting="DetailsView1_ItemDeleting"
onpageindexchanging="DetailsView1_PageIndexChanging">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbleno" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteno" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<ItemTemplate>
<asp:Label ID="lblempname" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtempname" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblsal" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Address") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtsal" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Address") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True"></asp:CommandField>
<asp:CommandField ShowInsertButton="True"></asp:CommandField>
<asp:CommandField ShowEditButton="True" CancelText="Cancel" EditText="Edit" UpdateText="Update" />
</Fields>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
</div>
</form>
</body>
</html>
// Default.aspx.cs //
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;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString);
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadDet();
}
}
void LoadDet()
{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from Add_Record", sqlcon);
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
sqlcon.Close();
DetailsView1.DataSource = dt;
DetailsView1.DataBind();
}
//Below code is used to display page wise records
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
LoadDet();
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (e.CancelingEdit == true)
{
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
LoadDet();
}
}
//Below code is used to update record details
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
string eno = ((TextBox)DetailsView1.FindControl("ID")).Text.ToString();
string empname = ((TextBox)DetailsView1.FindControl("Name")).Text.ToString();
string Add = ((TextBox)DetailsView1.FindControl("Address")).Text.ToString();
sqlcon.Open();
sqlcmd = new SqlCommand("UPDATE Add_Record SET Name='" + empname + "', Address='" + Add + "' WHERE ID='" + eno + "' ", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
LoadDet();
}
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "New")
{
DetailsView1.ChangeMode(DetailsViewMode.Insert);
LoadDet();
}
if (e.CommandName == "Edit")
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
LoadDet();
}
}
//Below code is used to insert record into database table
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
sqlcon.Open();
string eno = ((TextBox)DetailsView1.FindControl("ID")).Text.ToString();
string empname = ((TextBox)DetailsView1.FindControl("Name")).Text.ToString();
string sal = ((TextBox)DetailsView1.FindControl("Address")).Text.ToString();
SqlCommand cmd = new SqlCommand("select ID from Add_Record where ID = '" + eno + "'", sqlcon);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
lblmsg.Text = "User No already exists";
}
else
{
dr.Close();
sqlcmd = new SqlCommand("insert into Add_Record values('" + eno + "', '" + empname + "', '" + sal + "')", sqlcon);
sqlcmd.ExecuteNonQuery();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
sqlcon.Close();
LoadDet();
}
//Below code is used to delete record from database
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
string eno = ((Label)DetailsView1.Rows[0].FindControl("lbleno")).Text;
sqlcon.Open();
sqlcmd = new SqlCommand("delete from Add_Record where ID = '" + eno + "'", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
LoadDet();
}
}
SQL TABLE
OUTPUT
No comments:
Post a Comment