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_admins_pg_CourseAdd : System.Web.UI.Page
{
    //*******************************************
    SqlConnection con;
    SqlTransaction tr;
    //*******************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "ایجاد دوره جدید";
        }
        catch
        {
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        string returnUrl = Request.QueryString["returnUrl"].ToString();
        Response.Redirect(returnUrl);
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            System.Threading.Thread.Sleep(2000);
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            SqlCommand cmd = con.CreateCommand();
            try
            {
                string courseName = tbCourseName.Text;
                string beginDay = (cmbBeginDay.SelectedIndex > 0 ? cmbBeginDay.SelectedItem.ToString() : "");
                string beginMonth = (cmbBeginMonth.SelectedIndex > 0 ? cmbBeginMonth.SelectedItem.ToString() : "");
                string beginYear = (cmbBeginYear.SelectedIndex > 0 ? cmbBeginYear.SelectedItem.ToString() : "");
                string endDay = (cmbEndDay.SelectedIndex > 0 ? cmbEndDay.SelectedItem.ToString() : "");
                string endMonth = (cmbEndMonth.SelectedIndex > 0 ? cmbEndMonth.SelectedItem.ToString() : "");
                string endYear = (cmbEndYear.SelectedIndex > 0 ? cmbEndYear.SelectedItem.ToString() : "");
                string beginDate = beginDay + " " + beginMonth + " " + beginYear;
                string endDate = endDay + " " + endMonth + " " + endYear;
                string duration = tbDuration.Text;
                int amount = Int32.Parse(tbAmount.Text);
                bool showInList = (checkShowInList.Checked ? true : false);
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandText = t_CoursesInsert(courseName, beginDate, duration, endDate, amount, showInList);
                cmd.ExecuteNonQuery();
                tr.Commit();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ذخیره شد.";
            }
            catch(Exception exc)
            {
                try
                {
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Text = exc.Message;//"عملیات ذخیره سازی با شکست مواجه شد.";
                    tr.Rollback();
                }
                catch
                {
                }
            }
            finally
            {
                if (con.State != ConnectionState.Closed)
                    con.Close();
            }
        }
        catch(Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }

    private string t_CoursesInsert(string CourseName, string BeginDate, string Duration, string EndDate, int Amount, bool ShowInList)
    {
        string comm = "insert into t_Courses (courseName, beginDate, duration, endDate, amount, showInList) VALUES (N'" +
            CourseName + "' , N'" +
                BeginDate + "' , N'" +
                Duration + "' , N'" +
                EndDate + "' , " +
                Amount + " , " +
                ( ShowInList ? 1 : 0) + " )";
        return comm;
    }
}
