Tuesday, 14 May 2013

DetailsView row Edit,Update and Delete simple example C#,Asp.Net


DetailsView row Edit,Update and Delete simple example C#,Asp.Net




using System;
usingSystem.Configuration;
using System.Data;
using System.Linq;
using System.Web;
usingSystem.Web.Security;
using System.Web.UI;
usingSystem.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection("Data Source=J11;Database=Ryan;Integrated Security=true;");
    protected void Page_Load(object sender, EventArgs e)
    {
        Label4.Text = null;
        if (!IsPostBack)
        {
            DetailsView1.AllowPaging = true;
            SqlCommand cmd = new SqlCommand("SELECT * FROM details", con);
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            dt.Load(reader);
            Session["table"] = dt;
            DetailsView1Bind();
        }
    }
    public void DetailsView1Bind()
    {
        dt = (DataTable)Session["table"];
        DetailsView1.DataSource = dt;
        DetailsView1.DataBind();
        con.Close();
    }
    protected void DetailsView1_PageIndexChanging(object sender,DetailsViewPageEventArgs e)
    {
        DetailsView1.PageIndex = e.NewPageIndex;
        DetailsView1Bind();
    }
    protected void DetailsView1_ModeChanging(object sender,DetailsViewModeEventArgs e)
    {
        if (e.CancelingEdit == true)
        {
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            DetailsView1Bind();
        }
    }
    protected void DetailsView1_ItemUpdating(object sender,DetailsViewUpdateEventArgs e)
    {
        string name = ((Label)DetailsView1.FindControl("TextBox1")).Text.ToString();
        string pass = ((TextBox)DetailsView1.FindControl("TextBox2")).Text.ToString();
        string phone = ((TextBox)DetailsView1.FindControl("TextBox3")).Text.ToString();
        SqlCommand cmd = new SqlCommand("UPDATE details SET pass='"+ pass + "', phone='" + phone + "' WHERE name='" + name + "' ", con);
        con.Open();
        cmd.ExecuteNonQuery();
        cmd = new SqlCommand("SELECT * FROM details", con);
        SqlDataReader dr = cmd.ExecuteReader();
        dt.Load(dr);
        Session["table"] = dt;
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        DetailsView1Bind();
    }
    protected void DetailsView1_ItemInserting(object sender,DetailsViewInsertEventArgs e)
    {
        con.Open();
        string name = ((TextBox)DetailsView1.FindControl("TextBox1")).Text.ToString();
        string pass = ((TextBox)DetailsView1.FindControl("TextBox2")).Text.ToString();
        string phone = ((TextBox)DetailsView1.FindControl("TextBox3")).Text.ToString();
        SqlCommand cmd = new SqlCommand("SELECT name FROM details WHERE name = '" + name + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Label4.Text = "Name already exists";
        }
        else
        {
            dr.Close();
            cmd = new SqlCommand("INSERT INTO details VALUES('" + name + "', '" + pass + "', '" + phone + "')", con);
            cmd.ExecuteNonQuery();
            cmd = new SqlCommand("SELECT * FROM details", con);
            dr = cmd.ExecuteReader();
            dt.Load(dr);
            Session["table"] = dt;
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            DetailsView1Bind();
        }
    }
    protected void DetailsView1_ItemDeleting(object sender,DetailsViewDeleteEventArgs e)
    {

    }
    protected void DetailsView1_ItemCommand(object sender,DetailsViewCommandEventArgs e)
    {
        if (e.CommandName == "New")
        {
            DetailsView1.ChangeMode(DetailsViewMode.Insert);
            DetailsView1Bind();
        }
        if (e.CommandName == "Edit")
        {
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
            DetailsView1Bind();
        }
    }
}

No comments:

Post a Comment