C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


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


Posted Date: 01 Jul 2008    Resource Type: Articles    Category: Web Applications
Author: joohithaMember Level: Silver    
Rating: Points: 10



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

}
}




Attachments

  • source code (19355-11016-source code.docx)



  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    (No tags found.)

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Working with Tiff Image and Resizing Images
    Previous Resource: Application & Session Variables explained....................
    Return to Discussion Resource Index
    Post New Resource
    Category: Web Applications


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers   BizTalk Adaptors    Web Design


    Contact Us    Privacy Policy    Terms Of Use