using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class DataListEx : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(“server=ADMIN-PC\\SQLEXPRESS;database=tutorial;integrated security=SSPI”);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
filldatalist();
}
}
public void filldatalist()
{
SqlDataAdapter da = new SqlDataAdapter(“sp_GetData”, con);
DataSet ds = new DataSet();
con.Open();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
con.Close();
}
protected void edit(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
filldatalist();
}
protected void update(object source, DataListCommandEventArgs e)
{
string id = ((Label)DataList1.Items[e.Item.ItemIndex].FindControl(“Label5”)).Text;
string name = ((TextBox)DataList1.Items[e.Item.ItemIndex].FindControl(“TextBox1”)).Text;
string Fathername = ((TextBox)DataList1.Items[e.Item.ItemIndex].FindControl(“TextBox2”)).Text;
string Salary = ((TextBox)DataList1.Items[e.Item.ItemIndex].FindControl(“TextBox3”)).Text;
SqlCommand cmd = new SqlCommand(“sp_Update”, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter(“id”, id));
cmd.Parameters.Add(new SqlParameter(“name”, name));
cmd.Parameters.Add(new SqlParameter(“fathername”,Fathername));
cmd.Parameters.Add(new SqlParameter(“salary”, Salary));
con.Open();
int result= cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
{
DataList1.EditItemIndex = -1;
filldatalist();
}
}
protected void cancel(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
filldatalist();
}
}