Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Working with datalist control
|
edit,delete,update,cancel in datalist control
using System.Data; 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; using System.Data.SqlClient; using System.Drawing;
public partial class DataListControl : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) { LoadData(); DataList1.DeleteCommand += new DataListCommandEventHandler( this.DataList1_DeleteCommand); }
private void LoadData() { SqlConnection cn = new SqlConnection("user id=sa;password=sa;database=master;data source=netsvr"); cn.Open(); SqlCommand cmd = new SqlCommand("select EmployeeCode,EmployeeName,Salary,Department from Employee", cn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, "Employee"); //bind data to the repeater control DataList1.DataSource = ds; DataList1.DataBind();
} //item command for selecting a row protected void Employee_Select(object source, DataListCommandEventArgs e) { if (e.CommandName == "Cancel") { DataList1.EditItemIndex = -1; LoadData(); } } protected void DataList1_EditCommand(object source, DataListCommandEventArgs e) { DataList1.EditItemIndex = e.Item.ItemIndex; LoadData(); } protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e) { try {
int id = e.Item.ItemIndex; SqlConnection cn = new SqlConnection("user id=sa;password=sa;database=master;data source=netsvr"); cn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = "delete from Employee where EmployeeCode=" +id; cmd.ExecuteNonQuery(); DataList1.EditItemIndex = -1; //Reset the index LoadData(); } catch (Exception ex) { throw ex; }
} protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e) { Label lbl; TextBox tbox; int empcode; string empname; int salary; string dept; // ---retrieves the key for the row--- lbl = ((Label)(e.Item.FindControl("EmpCodeLabel"))); empcode = Convert.ToInt32(lbl.Text); //empcode = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
// ---find the textbox control containing the EmpName tbox = ((TextBox)(e.Item.FindControl("EmpNameTextBox"))); empname = tbox.Text;
// ---find the textbox control containing the salary tbox = ((TextBox)(e.Item.FindControl("SalaryTextBox"))); salary = Convert.ToInt32(tbox.Text);
//----find the textbox control containing the department tbox = ((TextBox)(e.Item.FindControl("DepartmentTextBox"))); dept = tbox.Text;
// ---updates the database--- string sql = "UPDATE Employee SET EmployeeName='" + empname + "' , Salary=" +salary + ", Department='" + dept + "' WHERE EmployeeCode=" + empcode ; SqlConnection conn = new SqlConnection(("user id=sa;password=sa;database=master;data source=netsvr")); SqlCommand comm = new SqlCommand(sql, conn); conn.Open(); comm.ExecuteNonQuery(); conn.Close(); DataList1.EditItemIndex = -1; LoadData();
} }
Attachmentssource code (19355-11016-source code.docx)
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|