Thursday, May 15, 2008

Edit,Update and delete in datalist

<!-- 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(); 

}}

7 comments :

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

    Happy programming.

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

    ReplyDelete
  3. U dint Mentioned Database

    ReplyDelete
  4. excellent.... its easy to understand....

    ReplyDelete
  5. cmd.Parameters.Add("@teamName" , SqlDbType.VarChar, 50).Value = ((TextBox)e.Item.FindControl("teamName")).Text;
    error-System.NullReferenceException: Object reference not set to an instance of an object.
    please help me..what is solution of this error?

    ReplyDelete
  6. the fields are not updating..

    ReplyDelete

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath