using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace IntranetApp
{
	/// <summary>
	/// Summary description for EmployeeList.
	/// </summary>
	public partial class EmployeeList : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			Label3.Text="";
			if (!IsPostBack)
			{
				setEmployees();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
			this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
			this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
			this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);

		}
		#endregion

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

		private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			DataGrid1.CurrentPageIndex=e.NewPageIndex;
			setEmployees();
		}

		private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex=-1;
			setEmployees();
		}

		private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.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)
			{
				Label4.Text="Conflict occured in the database"; 
			}
		}

		private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex=e.Item.ItemIndex;
			setEmployees();
		}

		private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.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();
			Label3.Text="One Record Modified";
			DataGrid1.EditItemIndex=-1;
			setEmployees();
		}
	}
}
