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 DocumentsList.
	/// </summary>
	public partial class DocumentsList : 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)
			{
				display();
			}
		}

		#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.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);

		}
		#endregion

		SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings.Get("con"));

		private void display()
		{
			if (con.State==ConnectionState.Closed) con.Open();
			SqlDataAdapter da=new SqlDataAdapter("select docno from data  where (status='yes'  or empno=" + Session["eid"].ToString() + ")",con);
			DataSet ds=new DataSet();
			da.Fill(ds,"data");
			DataGrid1.DataSource=ds.Tables[0];
			DataGrid1.DataBind();
			con.Close();
		}

		private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if (con.State==ConnectionState.Closed) con.Open();
			SqlCommand cmd=new SqlCommand("select subject,empno from data where docno=" + e.Item.Cells[0].Text ,con);
			SqlDataReader dr;
			dr=cmd.ExecuteReader();
			if (dr.Read())
			{
				TextBox1.Text=dr.GetValue(0).ToString();
				Session["check"]=dr.GetValue(1).ToString();
			}
			dr.Close();
			con.Close();
			Panel1.Visible=true;
			ViewState["id"]=e.Item.Cells[0].Text;

			if ((Session["eid"].ToString())!=(Session["check"].ToString()))
			{
				Button1.Enabled=false;
				Button2.Enabled=false;
			}
		}

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

		protected void Button1_Click(object sender, System.EventArgs e)
		{
			if (con.State==ConnectionState.Closed) con.Open();
			SqlCommand cmd=new SqlCommand("delete from data where docno=" + ViewState["id"].ToString(),con);
			cmd.ExecuteNonQuery();
			con.Close();
			TextBox1.Text="";
			display();
		}

		protected void Button2_Click(object sender, System.EventArgs e)
		{
			if (con.State==ConnectionState.Closed) con.Open();
			SqlCommand cmd=new SqlCommand("update data set subject='" + TextBox1.Text + "' where docno=" + ViewState["id"].ToString(),con);
			cmd.ExecuteNonQuery();
			con.Close();
		}

		protected void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}

	}
}
