using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;

public partial class rl_students_pg_Exams : System.Web.UI.Page
{
    //**********************************
    static string lastQuery = "";
    SqlConnection con;
    SqlCommand cmd;
    //**********************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                if (User.IsInRole("teacher"))
                {
                    C_myClass.MenueItemsCreate(User, panelMenues);
                    Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                    lblTitle.Text = "آزمون های کلاسی";
                    int teacherId = Int32.Parse(Session["iden"].ToString());
                    lastQuery = "SELECT t_Classes.classTitle, t_Teachers.fullName, t_Exams.* FROM t_Teachers INNER JOIN t_Classes ON t_Teachers.teacherId = t_Classes.teacherId INNER JOIN t_Exams ON t_Exams.classId = t_Classes.classId WHERE (t_Classes.teacherId = " + teacherId + ")";
                    SqlDataSource1.SelectCommand = lastQuery;
                    GridView1.DataBind();
                }
            }
        }
        catch
        {
        }
    }
    protected void btnAllShow_Click(object sender, EventArgs e)
    {
        try
        {
            int teacherId = Int32.Parse(Session["iden"].ToString());
            lastQuery = "SELECT t_Classes.classTitle, t_Teachers.fullName, t_Exams.* FROM t_Teachers INNER JOIN t_Classes ON t_Teachers.teacherId = t_Classes.teacherId INNER JOIN t_Exams ON t_Exams.classId = t_Classes.classId WHERE (t_Classes.teacherId = " + teacherId + ")";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            int teacherId = Int32.Parse(Session["iden"].ToString());
            lastQuery = "SELECT t_Classes.classTitle, t_Teachers.fullName, t_Exams.* FROM t_Teachers INNER JOIN t_Classes ON t_Teachers.teacherId = t_Classes.teacherId INNER JOIN t_Exams ON t_Exams.classId = t_Classes.classId WHERE (t_Classes.teacherId = " + teacherId + ") AND (t_Classes.status = N'جاری')";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "ResaultPress")
            {
                int examId = Int32.Parse(e.CommandArgument.ToString());
                Response.Redirect("pg_ExamResaultSet.aspx?examId=" + examId + "&returnUrl=pg_Exams.aspx");
            }
            else if (e.CommandName == "EditPress")
            {
                int examId = Int32.Parse(e.CommandArgument.ToString());
                Response.Redirect("pg_ExamEdit.aspx?examId=" + examId + "&returnUrl=pg_Exams.aspx");
            }
            else if (e.CommandName == "DeletePress")
            {
                int examId = Int32.Parse(e.CommandArgument.ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "delete from t_Exams where examId = " + examId;
                    cmd.ExecuteNonQuery();
                    SqlDataSource1.SelectCommand = lastQuery;
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
    }
    protected void btnNewClass_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("pg_ExamAdd.aspx?returnUrl=pg_Exams.aspx");
        }
        catch
        {
        }
    }
}
