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 DocumentsList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            display();
        }
    }
    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();
    }
    protected void Button1_Click(object sender, 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, 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_DeleteCommand(object source, 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;
        }
    }
    protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        display();
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}
