Edit,Update and delete in datalist

Posted on May 15, 2008. Filed under: Asp.net |

<!– start html code –>

<asp:DataList ID=rp runat=server DataKeyField=”localityid” RepeatColumns=2 RepeatLayout=Table Width=400px OnCancelCommand=Cancel_Command OnEditCommand=edit OnUpdateCommand=update OnDeleteCommand=delete>

<ItemTemplate>

<asp:Label ID=lbl runat=server Text=’<%#Eval(”localityid”) %>’></asp:Label>

<asp:Label ID=lbl1 runat=server Text=’<%#Eval(”localityname”) %>’></asp:Label>

<asp:Button ID=edit Text=Edit CommandName=edit runat=server />

<asp:Button ID=delete Text=Delete CommandName=delete runat=server />

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox ID=locid runat=server Text=’<%#Eval(”localityid”) %>’></asp:TextBox>

<asp:TextBox ID=locname runat=server Text=’<%#Eval(”localityname”) %>’></asp:TextBox>

<asp:Button ID=update Text=Update CommandName=update runat=server />

<asp:Button ID=”Cancle” Text=”Cancle” CommandName=”cancel” runat=server />

</EditItemTemplate>

 

</asp:DataList> 

<!–End html code –> 

<!–In codebehind –>

using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls; 

public partial class editingindatalist : System.Web.UI.Page

{

SqlConnection con = new SqlConnection(”Server=.;Database=raj;uid=sa;pwd=sa”);

protected void Page_Load(object sender, EventArgs e)

{ if (!Page.IsPostBack)

{this.binddata();// for data bind to datalist Control }

}

protected void Cancel_Command(object source, DataListCommandEventArgs e)

{ rp.EditItemIndex =-1;

this.binddata();

}

protected void delete(object source, DataListCommandEventArgs e)

{

int id = Convert.ToInt32(rp.DataKeys[e.Item.ItemIndex]);

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.Parameters.Add(”@locid”, SqlDbType.Int).Value = id;

cmd.CommandText = “delete from locality where localityid=@locid”;

cmd.Connection.Open();

cmd.ExecuteNonQuery();

cmd.Connection.Close();

binddata(); 

}

protected void edit(object source, DataListCommandEventArgs e)

{ rp.EditItemIndex = e.Item.ItemIndex;

binddata();

}

protected void update(object source, DataListCommandEventArgs e)

{ int id = Convert.ToInt32(rp.DataKeys[e.Item.ItemIndex]);

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.Parameters.Add(”@locname”, SqlDbType.VarChar).Value = ((TextBox)e.Item.FindControl(”locname”)).Text;

cmd.Parameters.Add(”@locid”, SqlDbType.Int).Value = id;

cmd.CommandText = “update locality set localityname=@locname WHERE localityid=@locid”;

cmd.Connection.Open();

cmd.ExecuteNonQuery();

cmd.Connection.Close();

rp.EditItemIndex = -1;

this.binddata();

}

protected void binddata()

{ SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandText = “select * from locality WHERE localityid in(select localityid from localityassign where cityid=3)”;

cmd.Connection.Open();

SqlDataReader rdr = cmd.ExecuteReader();

rp.DataSource = rdr;

rp.DataBind();

cmd.Connection.Close(); 

}}

Make a Comment

Make A Comment: ( 3 so far )

blockquote and a tags work here.

3 Responses to “Edit,Update and delete in datalist”

RSS Feed for Dream Asp.net Comments RSS Feed

Very good article, it is concise and helpful. Thank you for posting.

Happy programming.

T N
June 17, 2008

Thanks a ton for your thoughtfullness..
Pls keep up the good work. God bless!!

Amit
July 11, 2008

U dint Mentioned Database

Reena
July 22, 2008

Where's The Comment Form?

    About

    Hi,I am Suresh Sharma(MCP) from Gorakhpur , UP, India.I have work on .net technology from 2 years. Key skills-Asp.net 2.0,3.5, Sql 2005,2000. Ajax.net 1.1,C#.net

    RSS

    Subscribe Via RSS

    • Subscribe with Bloglines
    • Add your feed to Newsburst from CNET News.com
    • Subscribe in Google Reader
    • Add to My Yahoo!
    • Subscribe in NewsGator Online
    • The latest comments to all posts in RSS
    • Subscribe in Rojo

    Meta

Liked it here?
Why not try sites on the blogroll...