Wednesday, 15 January 2014

Insert, Update, Delete in DetailsView in ASP.NET





DetailsView control in ASP.NET is used to display the Details of the master record in a table format or single record at a time. You can easily insert, update, delete and paging to data items in DetailsView. In this example we explain that how to perform Insert,Update,Delete Operation in Detailview in Asp.Net.

The idea behind using the DetailsView control is nothing but displaying the Details of the master. By default, the DetailsView control displays each field of a record on its own line. The DetailsView control displays only a single data record at a time, even if its data source contain more than one records.

The DetailsView control is typically used for display the detail of master record in a master/detail scenario where the selected record of the master control determines the record to display in the DetailsView control. The DetailsView control does not support sorting but it supports paging.

there are three modes of the Detailsview are as follows:



DetailsViewMode.Edit :-the Detailview is in Editmode and allows user to edit and update record.
DetailsViewMode.Insert :- the Detailview is in Insertmode and allows user to insert the new record.
DetailsViewMode.ReadOnly : the Detailview is in readonly mode and is the normal display mode of Detailview


to insert,update,delete in Repeator control click here Insert,Update,Delete in Repeator control in Asp.Net

Ajax Password Strength Example Password checker like good,medium,loose etc

Javascript validation for enter only number in textbox Disable all keys except number using javascript


detailview.aspx:-



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="detailview.aspx.cs" Inherits="detailview" %>


<!DOCTYPE html>

<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="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="id">
<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("id") %>'></asp:Label>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ename">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Eadd">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("Salary") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Salary") %>'></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>
    </div>
    </form>
</body>
</html>

 

detailview.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 detailview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            det_bind();
        }
    }
    private void det_bind()
    {
        SqlDataAdapter adp = new SqlDataAdapter("select * from poll",  "Data Source=SQLDB;User ID=Demoh;Password=Demo1@");
        DataSet ds = new DataSet();
        adp.Fill(ds);
        DetailsView1.DataSource = ds;
        DetailsView1.DataBind();
    }
    //Inserting Record
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        Int32  es;
        String en;

    
        en = ((TextBox)(DetailsView1.Rows[1].FindControl("TextBox3"))).Text;
     
        es = Convert.ToInt32(((TextBox)(DetailsView1.Rows[3].FindControl("TextBox5"))).Text);

        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=SQLDB;User ID=Demoh;Password=Demo1@";
        con.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "insert into poll values(@en,@es)";
        cmd.Connection = con;
     
        cmd.Parameters.Add("@en", SqlDbType.VarChar, 50).Value = en;
        cmd.Parameters.Add("@es", SqlDbType.VarChar, 50).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;
        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;
      

        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=SQLDB;User ID=Demoh;Password=Demo1@";
        con.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "update poll set Name=@en,Salary=@ed where id=@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.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 = "Data Source=SQLDB;User ID=Demoh;Password=Demo1@";
        con.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "delete from poll where id=@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();
    }
}
 

7 comments:

  1. i want to save this record on a gridview by clicking a button on another page ,

    how to do this please help me.

    ReplyDelete
  2. Simply take gridview instead of detail view. And pass data through query string in another page.thank you .

    ReplyDelete
  3. details view image how to insert update process

    ReplyDelete


  4. 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 course fees

    ReplyDelete
    Replies
    1. thanks. also keep visiting updated post on blog.

      Delete
  5. thanks. also keep visiting updated post on blog.

    ReplyDelete
  6. thanks. also keep visiting updated post on blog.

    ReplyDelete