Search Asp.Net Here

Display,Save, Update,Delete in DetailsView Control in asp.net





Save,Update,Delete in DetailsView Control in asp.net

DetailsView is a DataBound control of asp.net used to Displays the single record from a DataSource in the form of a table, where each row represents a field of the record. The DetailsView control allows you to insert,edit and delete records.

Here I am giving an example of how we can insert,edit and delete record in Datasource using DetailsView control

I have used following table (tbemp) in this example


Code for .aspx page


<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="353px" 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="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<PagerStyle BackColor="#2461BF" ForeColor="White" 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>
</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="#507CD1" 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="#D1DDF1" Font-Bold="True" />
<RowStyle BackColor="#EFF3FB" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
</asp:DetailsView>


Write following code in your page_load event

if (Page.IsPostBack == false)
{
det_bind();
}

Now copy paste following code below page_load event


private void det_bind()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tbemp", "server=tiger;database=database1;uid=sa");
DataSet ds = new DataSet();
adp.Fill(ds);
DetailsView1.DataSource = ds;
DetailsView1.DataBind();
}
//Inserting Record
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
Int32 eno, es;
String en, ed;

eno = Convert.ToInt32(((TextBox)(DetailsView1.Rows[0].FindControl("TextBox1"))).Text);
en = ((TextBox)(DetailsView1.Rows[1].FindControl("TextBox3"))).Text;
ed = ((TextBox)(DetailsView1.Rows[2].FindControl("TextBox5"))).Text;
es = Convert.ToInt32(((TextBox)(DetailsView1.Rows[3].FindControl("TextBox7"))).Text);

SqlConnection con = new SqlConnection();
con.ConnectionString = "server=tiger;database=database1;uid=sa";
con.Open();

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into tbemp values(@eno,@en,@ed,@es)";
cmd.Connection = con;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = eno;
cmd.Parameters.Add("@en", SqlDbType.VarChar, 50).Value = en;
cmd.Parameters.Add("@ed", SqlDbType.VarChar, 50).Value = ed;
cmd.Parameters.Add("@es", SqlDbType.Int).Value = es;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
det_bind();
}

//DetailsView’s Event to Update Record

protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
//int eno1 = Convert.ToInt32(DetailsView1.DataKey[0]);
//Response.Write(eno1);
Int32 eno, es;
String en, ed;

eno = Convert.ToInt32(((Label)(DetailsView1.Rows[0].FindControl("Label2"))).Text);
en = ((TextBox)(DetailsView1.Rows[1].FindControl("TextBox2"))).Text;
ed = ((TextBox)(DetailsView1.Rows[2].FindControl("TextBox4"))).Text;
es = Convert.ToInt32(((TextBox)(DetailsView1.Rows[3].FindControl("TextBox6"))).Text);

SqlConnection con = new SqlConnection();
con.ConnectionString = "server=tiger;database=database1;uid=sa";
con.Open();

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "update tbemp set ename=@en,eadd=@ed,esal=@es where eno=@eno";
cmd.Connection = con;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = eno;
cmd.Parameters.Add("@en", SqlDbType.VarChar, 50).Value = en;
cmd.Parameters.Add("@ed", SqlDbType.VarChar, 50).Value = ed;
cmd.Parameters.Add("@es", SqlDbType.Int).Value = es;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
det_bind();
}

//DetailsView’s Event to Delete Record

protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
Int32 eno;


eno = Convert.ToInt32(((Label)(DetailsView1.Rows[0].FindControl("Label1"))).Text);

SqlConnection con = new SqlConnection();
con.ConnectionString = "server=tiger;database=database1;uid=sa";
con.Open();

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "delete from tbemp where eno=@eno";
cmd.Connection = con;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = eno;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
det_bind();

}

//DetailsView’s Event to Change the Index of page (used for paging functionality in Detailsview


protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
det_bind();
}

//DetailsView’s Event to change the mode of DetailsView i.e. edit,read only,insert


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);
}
det_bind();
}



Comments :

1
Anonymous said...
on 

Thank you very much
Its really worth

Post a Comment