using System;
using System.Linq;
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;

public partial class AddNewsGroup : System.Web.UI.Page
{
    protected void grdFill()
    {
        LSC_KhateratDataContext db = new LSC_KhateratDataContext();
        var Query = from Row in db.XtblKhaterehGroups 
                    select Row;

        GridView1.DataSource = Query;
        GridView1.DataBind();
    }

    protected void dbFill()
    {
        LSC_KhateratDataContext db = new LSC_KhateratDataContext();
        tblKhaterehGroup tblKh = new tblKhaterehGroup();
        tblKh.NewgGroupDescription = TextBox1.Text.Trim();
        db.tblKhaterehGroups.InsertOnSubmit(tblKh);
        db.SubmitChanges();

        grdFill();
        TextBox1.Text = "";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] != null)
        {
            if (int.Parse(Session["UserTypeID"].ToString()) == 1)
            {
                if (!IsPostBack)
                {
                    grdFill();
                }
            }
            else
            {
                Response.Redirect("Default.aspx?ID=NotHavePermission" + DateTime.Now.Ticks.ToString());
            }
        }
        else
        {
            Response.Redirect("Default.aspx?ID=NotHavePermission" + DateTime.Now.Ticks.ToString());
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        LSC_KhateratDataContext db = new LSC_KhateratDataContext();
        Label3.Visible = false;
        try
        {
            tblKhaterehGroup tbl = db.tblKhaterehGroups.Single(n => n.NewgGroupDescription.Trim() == TextBox1.Text.Trim());
            Label3.Visible = true;
        }
        catch
        {
            dbFill();
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        grdFill();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        LSC_KhateratDataContext db = new LSC_KhateratDataContext();
        int usrDelKey = (int)GridView1.DataKeys[e.RowIndex].Value;
        try
        {
            tblKhaterehGroup tblKh = db.tblKhaterehGroups.Single(n => n.NewsGroupID == usrDelKey);
            db.tblKhaterehGroups.DeleteOnSubmit(tblKh);
            db.SubmitChanges();
        }
        catch
        { }

        
        grdFill();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        grdFill();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        grdFill();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        LSC_KhateratDataContext db = new LSC_KhateratDataContext();
        Label lbl = new Label();
        string txtNewsGroup = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        lbl = ((Label)GridView1.Rows[e.RowIndex].FindControl("Label1"));

        int usrDelKey = int.Parse(lbl.Text.Trim());
        try
        {
            tblKhaterehGroup tblKh = db.tblKhaterehGroups.Single(n => n.NewsGroupID == usrDelKey);
            tblKh.NewgGroupDescription = txtNewsGroup;
            db.SubmitChanges();
        }
        catch
        { }
     
        GridView1.EditIndex = -1;
        grdFill();
    }
}
