using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class EmployeeList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            setEmployees();
        }
    }
    SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings.Get("con"));

    private void setEmployees()
    {
        if (con.State == ConnectionState.Closed) con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from emp", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "emp");
        DataGrid1.DataSource = ds.Tables[0];
        DataGrid1.DataBind();
    }

    protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
    {
        DataGrid1.EditItemIndex = -1;
        setEmployees();
    }
    protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
        if (con.State == ConnectionState.Closed) con.Open();
        try
        {
            SqlCommand cd = new SqlCommand("delete from data where empno='" + e.Item.Cells[0].Text + "'", con);
            cd.ExecuteNonQuery();
            SqlCommand cmd = new SqlCommand("delete from emp where empno='" + e.Item.Cells[0].Text + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
            DataGrid1.EditItemIndex = -1;
            setEmployees();
        }
        catch (Exception ex)
        {
            lbltext.Text = "Conflict occured in the database"+ex;
        }
    }
    protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    {
        DataGrid1.EditItemIndex = e.Item.ItemIndex;
        setEmployees();
    }
    protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        setEmployees();
    }
    protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        if (con.State == ConnectionState.Closed) con.Open();
        String t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11;
        t1 = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
        t2 = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
        t3 = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
        t4 = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
        t5 = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
        t6 = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
        t7 = ((TextBox)e.Item.Cells[7].Controls[0]).Text;
        t8 = e.Item.Cells[8].Text;
        t9 = e.Item.Cells[9].Text;
        t10 = ((TextBox)e.Item.Cells[10].Controls[0]).Text;
        t11 = ((TextBox)e.Item.Cells[11].Controls[0]).Text;
        SqlCommand cmd = new SqlCommand("update emp set ename='" + t1 + "',pwd='" + t2 + "',age='" + t3 + "',gender='" + t4 + "',address='" + t5 + "',phno='" + t6 + "',emailid='" + t7 + "',branchno='" + t8 + "',deptno='" + t9 + "',role='" + t10 + "',dateReg='" + t11 + "' where empno='" + e.Item.Cells[0].Text + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        lbltext.Text = "One Record Modified";
        DataGrid1.EditItemIndex = -1;
        setEmployees();
    }
}
