<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="334px"
AllowPaging="True" AutoGenerateRows="False" CellPadding="4" ForeColor="#333333"
GridLines="None" OnItemInserting="DetailsView1_ItemInserting"
OnModeChanging="DetailsView1_ModeChanging"
OnPageIndexChanging="DetailsView1_PageIndexChanging"
OnItemDeleting="DetailsView1_ItemDeleting"
OnItemUpdating="DetailsView1_ItemUpdating" DataKeyNames="eno">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<Fields>
<asp:TemplateField HeaderText="Eno">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("eno") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:TextBox ID="T1" runat="server" ></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Get Ur ID" />
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("eno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ename">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("ename") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("ename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Eadd">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("eadd") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("eadd") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Esal">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("esal") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("esal") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Editing">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton6" runat="server" CommandName="update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton7" runat="server" CommandName="cancel">Cacel</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="insert">Save</asp:LinkButton>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="cancel">Cancel</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete">Delete</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="new">New</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<EmptyDataTemplate>
NO DATA
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="new">Add Data</asp:LinkButton>
</EmptyDataTemplate>
<CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
</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.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddetailsView();
}
}
public void binddetailsView()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=DB_ Name;uid=sa;password=hai";
con.Open();
SqlCommand mySqlCommand = new SqlCommand("SELECT * FROM emp", con);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
DetailsView1.DataSource = myDataSet;
DetailsView1.DataBind();
con.Close();
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
binddetailsView();
}
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=DB_ Name;uid=sa;password=hi";
////Update the values.
int ro = int.Parse(datatable().Rows[e.RowIndex][0].ToString());
string delete = "Delete from fees1 where Roll='" + ro + "';";
SqlCommand command = new SqlCommand(delete, con);
con.Open();
command.ExecuteNonQuery();
con.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
binddetailsView();
}
public DataTable datatable()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=datagrid;uid=sa;password=1soft";
SqlCommand command = new SqlCommand("SELECT * FROM fees1", con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = command;
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=DB_ Name;uid=sa;password=hi";
string ename = ((TextBox)(DetailsView1.Rows[1].FindControl("TextBox3"))).Text;
string eadd = ((TextBox)(DetailsView1.Rows[2].FindControl("TextBox5"))).Text;
string esal = ((TextBox)(DetailsView1.Rows[3].FindControl("TextBox7"))).Text;
string insert = "insert into emp(Ename,Eadd,Esal) values ('" + ename + "','" + eadd + "','" + esal + "')";
SqlCommand command = new SqlCommand(insert, con);
con.Open();
command.ExecuteNonQuery();
con.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
binddetailsView();
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=DB_ Name;uid=sa;password=hi";
string eno = ((Label)(DetailsView1.Rows[0].FindControl("Label2"))).Text;
string ename = ((TextBox)(DetailsView1.Rows[1].FindControl("TextBox2"))).Text;
string eadd = ((TextBox)(DetailsView1.Rows[2].FindControl ("TextBox4"))).Text;
string esal = ((TextBox)(DetailsView1.Rows[3].FindControl("TextBox6"))).Text;
string update = "UPDATE emp SET Ename = '" + ename + "', Eadd = '" + eadd + "',Esal='" + esal + "' WHERE Eno = '" + eno + "';";
SqlCommand command = new SqlCommand(update, con);
con.Open();
command.ExecuteNonQuery();
con.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
binddetailsView();
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (e.NewMode == DetailsViewMode.Edit)
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
}
else if (e.NewMode == DetailsViewMode.Insert)
{
DetailsView1.ChangeMode(DetailsViewMode.Insert);
}
else
{
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
binddetailsView();
}
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
if (((DetailsView)sender).CurrentMode == DetailsViewMode.Edit)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=DB_ Name;uid=sa;password=hi";
SqlCommand com = new SqlCommand("select MAX(Eno)+1 from emp", con);
con.Open();
Int32 count = (Int32) com.ExecuteScalar();
string no = count.ToString();
TextBox T1 = new TextBox();
T1.Visible = true;
T1.Text = no;
((TextBox)(DetailsView1.Rows[0].FindControl("T1"))).Text = no;
con.Close();
}
}
No comments:
Post a Comment