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.IO;
using AjaxControlToolkit;
using System.Drawing;
using System.Data.SqlClient;

public partial class pg_RegisterSite : System.Web.UI.Page
{
    /////////////////////////////////

    //*****************************
    MembershipUser newUser;
    static string sendFrom = "";
    SqlTransaction tr;
    SqlConnection con;
    protected static Table adminTable = new Table();
    protected static Table studentTable = new Table();
    protected static Table teacherTable = new Table();
    protected static Table userTable = new Table();
    //*****************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                C_myClass.MenueItemsCreate(User, panelMenues);
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "عضویت کاربر";
                /*adminTable = panelItems("admin", adminTable);
                studentTable = panelItems("student", studentTable);
                teacherTable = panelItems("teacher", teacherTable);
                userTable = panelItems("user", userTable);
                lblErrorList.Text = adminTable.Rows.Count.ToString();*/
            }
        }
        catch
        {
        }
    }
    protected void rbAdmin_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            if (sender == rbAdmin)
            {
                if (rbAdmin.Checked)
                {
                    sendFrom = "admin";
                    lblMoreAttributes.Text = "اطلاعات بیشتر را می توانید در قسمت پروفایل کارمندان وارد کنید";
                    divUserType.Visible = true;
                    btnOk.Visible = true;
                }
            }
            else if (sender == rbTeacher)
            {
                if (rbTeacher.Checked)
                {
                    sendFrom = "teacher";
                    lblMoreAttributes.Text = "اطلاعات بیشتر را می توانید در قسمت پروفایل مدرسان وارد کنید";
                    divUserType.Visible = true;
                    btnOk.Visible = true;
                }
            }
            else if (sender == rbStudent)
            {
                if (rbStudent.Checked)
                {
                    sendFrom = "student";
                    lblMoreAttributes.Text = "اطلاعات بیشتر را می توانید در قسمت پروفایل دانشجویان وارد کنید";
                    divUserType.Visible = true;
                    btnOk.Visible = true;
                }
            }
            else if (sender == rbUser)
            {
                if (rbUser.Checked)
                {
                    sendFrom = "user";
                    lblMoreAttributes.Text = "اطلاعات بیشتر را می توانید در قسمت پروفایل کاربران وارد کنید";
                    divUserType.Visible = true;
                    btnOk.Visible = true;
                }
            }
        }
        catch
        {
        }
    }

    protected Table panelItems(string sendFrom, Table table)
    {
        if (sendFrom == "admin")
        {
            //init rows :
            TableRow tr1 = new TableRow();
            TableRow tr2 = new TableRow();
            TableRow tr3 = new TableRow();
            TableRow tr4 = new TableRow();
            //init cells :
            TableCell tdlbl1 = new TableCell();
            TableCell tdlbl2 = new TableCell();
            TableCell tdlbl3 = new TableCell();
            TableCell tdlbl4 = new TableCell();
            TableCell tdContent1 = new TableCell();
            TableCell tdContent2 = new TableCell();
            TableCell tdContent3 = new TableCell();
            TableCell tdContent4 = new TableCell();
            //init controls :
            Label lblFullName = new Label();
            Label lblGender = new Label();
            Label lblPost = new Label();
            Label lblPhoto = new Label();
            TextBox tbPost = new TextBox();
            TextBox tbAdminName = new TextBox();
            RadioButton rbAdminMale = new RadioButton();
            RadioButton rbAdminFemale = new RadioButton();
            FileUpload fuAdminPhoto = new FileUpload();
            RequiredFieldValidator validTbAdminName = new RequiredFieldValidator();
            // attributes :
            //tabel :
            table.BorderStyle = BorderStyle.None;
            table.CellPadding = 0;
            table.CellSpacing = 5;
            //cells :
            tdlbl1.VerticalAlign = VerticalAlign.Top;
            tdlbl1.HorizontalAlign = HorizontalAlign.Left;
            tdlbl2.VerticalAlign = VerticalAlign.Top;
            tdlbl2.HorizontalAlign = HorizontalAlign.Left;
            tdlbl3.VerticalAlign = VerticalAlign.Top;
            tdlbl3.HorizontalAlign = HorizontalAlign.Left;
            tdlbl4.VerticalAlign = VerticalAlign.Top;
            tdlbl4.HorizontalAlign = HorizontalAlign.Left;
            tdContent1.VerticalAlign = VerticalAlign.Top;
            tdContent2.VerticalAlign = VerticalAlign.Top;
            tdContent3.VerticalAlign = VerticalAlign.Top;
            tdContent4.VerticalAlign = VerticalAlign.Top;
            tdContent1.HorizontalAlign = HorizontalAlign.Right;
            tdContent2.HorizontalAlign = HorizontalAlign.Right;
            tdContent3.HorizontalAlign = HorizontalAlign.Right;
            tdContent4.HorizontalAlign = HorizontalAlign.Right;
            //controls :
            lblFullName.Text = "نام و نام خانوادگی :";
            lblFullName.ID = "lblFullNameIden";
            lblGender.Text = "جنسیت :";
            lblGender.ID = "lblGenderIden";
            lblPost.Text = "مسئولیت در آموزشگاه :";
            lblPost.ID = "lblPost.Iden";
            lblPhoto.Text = "عکس پرسنلی :";
            lblPhoto.ID = "lblPhotoIden";
            tbAdminName.ID = "tbAdminNameIden";
            tbAdminName.Width = 200;
            tbAdminName.Font.Name = "Tahoma";
            tbAdminName.Font.Size = 9;
            tbPost.ID = "tbPostIden";
            tbPost.Width = 200;
            tbPost.Font.Name = "Tahoma";
            tbPost.Font.Size = 9;
            rbAdminMale.ID = "rbAdminMaleIden";
            rbAdminMale.GroupName = "adminGender";
            rbAdminFemale.ID = "rbAdminFemaleIden";
            rbAdminFemale.GroupName = "adminGender";
            rbAdminMale.Text = "مرد";
            rbAdminMale.Checked = true;
            rbAdminFemale.Text = "زن";
            fuAdminPhoto.ID = "fuAdminPhotoIden";
            fuAdminPhoto.Width = 200;
            fuAdminPhoto.Font.Name = "Tahoma";
            fuAdminPhoto.Font.Size = 9;
            //tbAdminNameExtender.ID = "adminEx";
            //tbAdminNameExtender.TargetControlID = tbAdminName.ID;
            //tbAdminNameExtender.WatermarkText = "اجباری";
            //tbAdminNameExtender.WatermarkCssClass = "WaterMarkClass";
            //tbAdminNameExtender.Enabled = true;
            //tbPostExtender.ID = "postEx";
            //tbPostExtender.TargetControlID = tbPost.ID;
            //tbPostExtender.WatermarkText = "اختیاری";
            //tbPostExtender.WatermarkCssClass = "WaterMarkClass";
            //tbPostExtender.Enabled = true;
            validTbAdminName.ID = "validTbAdminNameIden";
            validTbAdminName.ControlToValidate = tbAdminName.ID;
            validTbAdminName.ErrorMessage = "*";
            //add controls to td :
            tdlbl1.Controls.Add(lblFullName);
            tdlbl2.Controls.Add(lblGender);
            tdlbl3.Controls.Add(lblPost);
            tdlbl4.Controls.Add(lblPhoto);
            tdContent1.Controls.Add(tbAdminName);
            //tdContent1.Controls.Add(tbAdminNameExtender);
            tdContent1.Controls.Add(validTbAdminName);
            tdContent2.Controls.Add(rbAdminMale);
            tdContent2.Controls.Add(rbAdminFemale);
            tdContent3.Controls.Add(tbPost);
            //tdContent3.Controls.Add(tbPostExtender);
            tdContent4.Controls.Add(fuAdminPhoto);
            //add cells to rows :
            tr1.Cells.Add(tdlbl1);
            tr1.Cells.Add(tdContent1);
            tr2.Cells.Add(tdlbl2);
            tr2.Cells.Add(tdContent2);
            tr3.Cells.Add(tdlbl3);
            tr3.Cells.Add(tdContent3);
            tr4.Cells.Add(tdlbl4);
            tr4.Cells.Add(tdContent4);
            // ad rows to table :
            table.Rows.Add(tr1);
            table.Rows.Add(tr2);
            table.Rows.Add(tr3);
            table.Rows.Add(tr4);
            // add table to div(page) :
        }
        else if (sendFrom == "teacher")
        {
            TableRow tr1 = new TableRow();
            TableRow tr2 = new TableRow();
            TableRow tr3 = new TableRow();
            TableRow tr4 = new TableRow();
            TableCell tdlbl1 = new TableCell();
            TableCell tdlbl2 = new TableCell();
            TableCell tdlbl3 = new TableCell();
            TableCell tdlbl4 = new TableCell();
            TableCell tdContent1 = new TableCell();
            TableCell tdContent2 = new TableCell();
            TableCell tdContent3 = new TableCell();
            TableCell tdContent4 = new TableCell();
            Label lblteacherName = new Label();
            lblteacherName.Text = "نام و نام خانوادگی مدرس :";
            Label lblGender = new Label();
            lblGender.Text = "جنسیت :";
            Label lblHistory = new Label();
            lblHistory.Text = "رزومه :";
            Label lblPhoto = new Label();
            lblPhoto.Text = "عکس پرسنلی :";
            TextBox tbTeacherName = new TextBox();
            tbTeacherName.ID = "tbTeacherName";
            TextBox tbHistory = new TextBox();
            tbHistory.ID = "HistoryId";
            FileUpload fuPhoto = new FileUpload();
            RadioButton rbMale = new RadioButton(); 
            RadioButton rbFemale = new RadioButton();
            TextBoxWatermarkExtender tbTeachernameEx = new TextBoxWatermarkExtender();
            tbTeachernameEx.WatermarkText = "اجباری";
            tbTeachernameEx.WatermarkCssClass = "WaterMarkClass";
            tbTeachernameEx.Enabled = true;
            tbTeachernameEx.TargetControlID = "tbTeacherName";
            RequiredFieldValidator validtbTeacherName = new RequiredFieldValidator();
            validtbTeacherName.ID = "validtbTeacherNameId";
            validtbTeacherName.ControlToValidate = tbTeacherName.ID;
            validtbTeacherName.ErrorMessage = "*";
            ///////////////////////////////////// attributes :
            table.CellPadding = 0;
            table.CellSpacing = 5;
            table.BorderStyle = BorderStyle.None;
            tdlbl1.VerticalAlign = VerticalAlign.Top;
            tdlbl1.HorizontalAlign = HorizontalAlign.Left;
            tdlbl2.VerticalAlign = VerticalAlign.Top;
            tdlbl2.HorizontalAlign = HorizontalAlign.Left;
            tdlbl3.VerticalAlign = VerticalAlign.Top;
            tdlbl3.HorizontalAlign = HorizontalAlign.Left;
            tdlbl4.VerticalAlign = VerticalAlign.Top;
            tdlbl4.HorizontalAlign = HorizontalAlign.Left;
            tdContent1.VerticalAlign = VerticalAlign.Top;
            tdContent1.HorizontalAlign = HorizontalAlign.Right;
            tdContent2.VerticalAlign = VerticalAlign.Top;
            tdContent2.HorizontalAlign = HorizontalAlign.Right;
            tdContent3.VerticalAlign = VerticalAlign.Top;
            tdContent3.HorizontalAlign = HorizontalAlign.Right;
            tdContent4.VerticalAlign = VerticalAlign.Top;
            tdContent4.HorizontalAlign = HorizontalAlign.Right;
            tbTeacherName.Width = 200;
            tbTeacherName.Font.Name = "Tahoma";
            tbTeacherName.Font.Size = 9;
            tbHistory.Width = 200;
            tbHistory.Font.Name = "Tahoma";
            tbHistory.Font.Size = 9;
            tbHistory.TextMode = TextBoxMode.MultiLine;
            tbHistory.Height = 100;
            fuPhoto.Width = 200;
            fuPhoto.Font.Name = "Tahoma";
            fuPhoto.Font.Size = 9;
            rbMale.Text = "مرد";
            rbFemale.Text = "زن";
            rbMale.GroupName = "gender";
            rbFemale.GroupName = "gender";
            rbFemale.CssClass = "femaleMargin";
            rbMale.Checked = true;
            /////////////////////////////////////////////// add to cell :
            tdlbl1.Controls.Add(lblteacherName);
            tdContent1.Controls.Add(tbTeacherName);
            tdContent1.Controls.Add(tbTeachernameEx);
            tdContent1.Controls.Add(validtbTeacherName);
            tdlbl2.Controls.Add(lblGender);
            tdContent2.Controls.Add(rbMale);
            tdContent2.Controls.Add(rbFemale);
            tdlbl3.Controls.Add(lblHistory);
            tdContent3.Controls.Add(tbHistory);
            tdlbl4.Controls.Add(lblPhoto);
            tdContent4.Controls.Add(fuPhoto);
            /////////////////////////////////////////////// add to row : 
            tr1.Cells.Add(tdlbl1);
            tr1.Cells.Add(tdContent1);
            tr2.Cells.Add(tdlbl2);
            tr2.Cells.Add(tdContent2);
            tr3.Cells.Add(tdlbl3);
            tr3.Cells.Add(tdContent3);
            tr4.Cells.Add(tdlbl4);
            tr4.Cells.Add(tdContent4);
            //////////////////////////////////////////////// add to table :
            table.Rows.Add(tr1);
            table.Rows.Add(tr2);
            table.Rows.Add(tr3);
            table.Rows.Add(tr4);
            ///////////////////////////////////////////////// add to div : 
        }
        else if (sendFrom == "student")
        {
            //init rows :
            TableRow tr1 = new TableRow();
            TableRow tr2 = new TableRow();
            TableRow tr3 = new TableRow();
            TableRow tr4 = new TableRow();
            TableRow tr5 = new TableRow();
            TableRow tr6 = new TableRow();
            //init cells :
            TableCell tdlbl1 = new TableCell();
            TableCell tdlbl2 = new TableCell();
            TableCell tdlbl3 = new TableCell();
            TableCell tdlbl4 = new TableCell();
            TableCell tdlbl5 = new TableCell();
            TableCell tdlbl6 = new TableCell();
            TableCell tdContent1 = new TableCell();
            TableCell tdContent2 = new TableCell();
            TableCell tdContent3 = new TableCell();
            TableCell tdContent4 = new TableCell();
            TableCell tdContent5 = new TableCell();
            TableCell tdContent6 = new TableCell();
            //init controls :
            Label lblstuName = new Label();
            Label lblstuBirthDate = new Label();
            Label lblstuMellicode = new Label();
            Label lblstuGender = new Label();
            Label lblstuDegree = new Label();
            Label lblstuPhoto = new Label();
            TextBox tbstuName = new TextBox();
            TextBox tbstuBirthDate = new TextBox();
            TextBox tbstuMelliCode = new TextBox();
            RadioButton rbstuMale = new RadioButton();
            RadioButton rbstuFemale = new RadioButton();
            DropDownList cmbstuDegree = new DropDownList();
            FileUpload fustuPhoto = new FileUpload();
            TextBoxWatermarkExtender tbstuNameExtender = new TextBoxWatermarkExtender();
            RequiredFieldValidator validtbstuName = new RequiredFieldValidator();
            ///////////////////////set attributes:
            //tabel :
            table.BorderStyle = BorderStyle.None;
            table.CellPadding = 0;
            table.CellSpacing = 5;
            //cells :
            tdlbl1.VerticalAlign = VerticalAlign.Top;
            tdlbl1.HorizontalAlign = HorizontalAlign.Left;
            tdlbl2.VerticalAlign = VerticalAlign.Top;
            tdlbl2.HorizontalAlign = HorizontalAlign.Left;
            tdlbl3.VerticalAlign = VerticalAlign.Top;
            tdlbl3.HorizontalAlign = HorizontalAlign.Left;
            tdlbl4.VerticalAlign = VerticalAlign.Top;
            tdlbl4.HorizontalAlign = HorizontalAlign.Left;
            tdlbl5.VerticalAlign = VerticalAlign.Top;
            tdlbl5.HorizontalAlign = HorizontalAlign.Left;
            tdlbl6.VerticalAlign = VerticalAlign.Top;
            tdlbl6.HorizontalAlign = HorizontalAlign.Left;
            tdContent1.VerticalAlign = VerticalAlign.Top;
            tdContent1.HorizontalAlign = HorizontalAlign.Right;
            tdContent2.VerticalAlign = VerticalAlign.Top;
            tdContent2.HorizontalAlign = HorizontalAlign.Right;
            tdContent3.VerticalAlign = VerticalAlign.Top;
            tdContent3.HorizontalAlign = HorizontalAlign.Right;
            tdContent4.VerticalAlign = VerticalAlign.Top;
            tdContent4.HorizontalAlign = HorizontalAlign.Right;
            tdContent5.VerticalAlign = VerticalAlign.Top;
            tdContent5.HorizontalAlign = HorizontalAlign.Right;
            tdContent6.VerticalAlign = VerticalAlign.Top;
            tdContent6.HorizontalAlign = HorizontalAlign.Right;
            //labels :
            lblstuName.Text = "نام و نام خانوادگی :";
            lblstuBirthDate.Text = "تاریخ تولد :";
            lblstuMellicode.Text = "شماره ملی :";
            lblstuGender.Text = "جنسیت :";
            lblstuDegree.Text = "میزان تحصیلات :";
            lblstuPhoto.Text = "عکس پرسنلی :";
            //textboxes :
            tbstuName.ID = "studentName";
            tbstuName.Width = 200;
            tbstuName.Font.Size = 9;
            tbstuName.Font.Name = "Tahoma";
            tbstuBirthDate.Width = 200;
            tbstuBirthDate.Font.Size = 9;
            tbstuBirthDate.Font.Name = "Tahoma";
            tbstuMelliCode.Width = 200;
            tbstuMelliCode.Font.Size = 9;
            tbstuMelliCode.Font.Name = "Tahoma";
            //extender :
            tbstuNameExtender.TargetControlID = tbstuName.ID;
            tbstuNameExtender.Enabled = true;
            tbstuNameExtender.WatermarkCssClass = "WaterMarkClass";
            tbstuNameExtender.WatermarkText = "اجباری";
            //validator :
            validtbstuName.ID = "validtbstuNameId";
            validtbstuName.ControlToValidate = tbstuName.ID;
            validtbstuName.ErrorMessage = "*";
            //radio :
            rbstuMale.Text = "مرد";
            rbstuMale.Checked = true;
            rbstuFemale.Text = "زن";
            rbstuMale.GroupName = "stuGender";
            rbstuFemale.GroupName = "stuGender";
            //cmbdegree
            cmbstuDegree.Width = 200;
            cmbstuDegree.Font.Size = 9;
            cmbstuDegree.Font.Name = "Tahoma";
            cmbstuDegree.Items.Add("زیر دیپلم");
            cmbstuDegree.Items.Add("دیپلم");
            cmbstuDegree.Items.Add("پیش دانشگاهی");
            cmbstuDegree.Items.Add("دانشجو");
            cmbstuDegree.Items.Add("فوق دیپلم");
            cmbstuDegree.Items.Add("لیسانس");
            cmbstuDegree.Items.Add("بالاتر");
            //fileupload :
            fustuPhoto.Width = 200;
            fustuPhoto.Font.Size = 9;
            fustuPhoto.Font.Name = "Tahoma";
            //add controls to cells :
            tdlbl1.Controls.Add(lblstuName);
            tdlbl2.Controls.Add(lblstuBirthDate);
            tdlbl3.Controls.Add(lblstuMellicode);
            tdlbl4.Controls.Add(lblstuGender);
            tdlbl5.Controls.Add(lblstuDegree);
            tdlbl6.Controls.Add(lblstuPhoto);
            tdContent1.Controls.Add(tbstuName);
            tdContent1.Controls.Add(tbstuNameExtender);
            tdContent1.Controls.Add(validtbstuName);
            tdContent2.Controls.Add(tbstuBirthDate);
            tdContent3.Controls.Add(tbstuMelliCode);
            tdContent4.Controls.Add(rbstuMale);
            tdContent4.Controls.Add(rbstuFemale);
            tdContent5.Controls.Add(cmbstuDegree);
            tdContent6.Controls.Add(fustuPhoto);
            //add td to rows :
            tr1.Cells.Add(tdlbl1);
            tr1.Cells.Add(tdContent1);
            tr2.Cells.Add(tdlbl2);
            tr2.Cells.Add(tdContent2);
            tr3.Cells.Add(tdlbl3);
            tr3.Cells.Add(tdContent3);
            tr4.Cells.Add(tdlbl4);
            tr4.Cells.Add(tdContent4);
            tr5.Cells.Add(tdlbl5);
            tr5.Cells.Add(tdContent5);
            tr6.Cells.Add(tdlbl6);
            tr6.Cells.Add(tdContent6);
            // add rows to tabel :
            table.Rows.Add(tr1);
            table.Rows.Add(tr2);
            table.Rows.Add(tr3);
            table.Rows.Add(tr4);
            table.Rows.Add(tr5);
            table.Rows.Add(tr6);
            //add table to page :
        }
        else if (sendFrom == "user")
        {
            //init rows :
            TableRow tr1 = new TableRow();
            TableRow tr2 = new TableRow();
            TableRow tr3 = new TableRow();
            //init cells :
            TableCell tdlbl1 = new TableCell();
            TableCell tdlbl2 = new TableCell();
            TableCell tdlbl3 = new TableCell();
            TableCell tdContent1 = new TableCell();
            TableCell tdContent2 = new TableCell();
            TableCell tdContent3 = new TableCell();
            // init controls :
            Label lbluserFullname = new Label();
            Label lbluserGender = new Label();
            Label lbluserPhoto = new Label();
            TextBox tbUserFullname = new TextBox();
            RadioButton rbUserMale = new RadioButton();
            RadioButton rbUserFemale = new RadioButton();
            FileUpload fuUserPhoto = new FileUpload();
            TextBoxWatermarkExtender tbUserFullnameExtender = new TextBoxWatermarkExtender();
            RequiredFieldValidator validtbUserFullname = new RequiredFieldValidator();
            //set attributes :
            tdlbl1.VerticalAlign = VerticalAlign.Top;
            tdlbl1.HorizontalAlign = HorizontalAlign.Left;
            tdlbl2.VerticalAlign = VerticalAlign.Top;
            tdlbl2.HorizontalAlign = HorizontalAlign.Left;
            tdlbl3.VerticalAlign = VerticalAlign.Top;
            tdlbl3.HorizontalAlign = HorizontalAlign.Left;
            tdContent1.VerticalAlign = VerticalAlign.Top;
            tdContent1.HorizontalAlign = HorizontalAlign.Right;
            tdContent2.VerticalAlign = VerticalAlign.Top;
            tdContent2.HorizontalAlign = HorizontalAlign.Right;
            tdContent3.VerticalAlign = VerticalAlign.Top;
            tdContent3.HorizontalAlign = HorizontalAlign.Right;
            lbluserFullname.Text = "نام و نام خانوادگی کاربر :";
            lbluserGender.Text = "جنسیت :";
            lbluserPhoto.Text = "عکس پرسنلی :";
            tbUserFullname.ID = "userfullname";
            tbUserFullname.Width = 200;
            tbUserFullname.Font.Size = 9;
            tbUserFullname.Font.Name = "Tahoma";
            rbUserMale.Text = "مرد";
            rbUserMale.Checked = true;
            rbUserFemale.Text = "زن";
            rbUserMale.GroupName = "usergender";
            rbUserFemale.GroupName = "usergender";
            fuUserPhoto.Width = 200;
            fuUserPhoto.Font.Size = 9;
            fuUserPhoto.Font.Name = "Tahoma";
            tbUserFullnameExtender.ID = "tbfullnameExtender";
            tbUserFullnameExtender.TargetControlID = tbUserFullname.ID;
            tbUserFullnameExtender.Enabled = true;
            tbUserFullnameExtender.WatermarkText = "اجباری";
            tbUserFullnameExtender.WatermarkCssClass = "WaterMarkClass";
            validtbUserFullname.ID = "validtbUserFullnameId";
            validtbUserFullname.ControlToValidate = tbUserFullname.ID;
            validtbUserFullname.ErrorMessage = "*";
            //add controls to td :
            tdlbl1.Controls.Add(lbluserFullname);
            tdContent1.Controls.Add(tbUserFullname);
            tdContent1.Controls.Add(tbUserFullnameExtender);
            tdContent1.Controls.Add(validtbUserFullname);
            tdlbl2.Controls.Add(lbluserGender);
            tdContent2.Controls.Add(rbUserMale);
            tdContent2.Controls.Add(rbUserFemale);
            tdlbl3.Controls.Add(lbluserPhoto);
            tdContent3.Controls.Add(fuUserPhoto);
            //add cells to rows :
            tr1.Cells.Add(tdlbl1);
            tr1.Cells.Add(tdContent1);
            tr2.Cells.Add(tdlbl2);
            tr2.Cells.Add(tdContent2);
            tr3.Cells.Add(tdlbl3);
            tr3.Cells.Add(tdContent3);
            // add rows to table :
            table.Rows.Add(tr1);
            table.Rows.Add(tr2);
            table.Rows.Add(tr3);
            //add table to page :
        }
        return table;
    }
    protected void btnCheck_Click(object sender, EventArgs e)
    {
        try
        {
            bool passSize = false;
            bool usernameExists = false;
            System.Threading.Thread.Sleep(1000);
            string userName = tbUserName.Text;
            string pass = tbUserPass1.Text;
            if (pass.Length < 5)
            {
                passSize = false;
            }
            else
            {
                passSize = true;
            }
            MembershipUserCollection muColl = Membership.GetAllUsers();
            MembershipUser[] muArray = new MembershipUser[muColl.Count];
            muColl.CopyTo(muArray, 0);
            for (int i = 0; i < muArray.Length; i++)
            {
                if (userName == muArray[i].UserName)
                {
                    usernameExists = true;
                }
            }
            if (usernameExists)
            {
                imgCheckUsername.ImageUrl = "~/images/error.gif";
                imgCheckUsername.Visible = true;
            }
            else
            {
                imgCheckUsername.ImageUrl = "~/images/ok.gif";
                imgCheckUsername.Visible = true;
            }
            if (passSize)
            {
                imgCheckPass1.ImageUrl = "~/images/ok.gif";
                imgCheckPass2.ImageUrl = "~/images/ok.gif";
                imgCheckPass1.Visible = true;
                imgCheckPass2.Visible = true;
            }
            else
            {
                imgCheckPass1.ImageUrl = "~/images/error.gif";
                imgCheckPass2.ImageUrl = "~/images/error.gif";
                imgCheckPass1.Visible = true;
                imgCheckPass2.Visible = true;
            }
            if ((!usernameExists) && (passSize))
            {
                if (Roles.IsUserInRole("admin"))
                {
                    rbAdmin.Enabled = false;
                    rbStudent.Enabled = true;
                    rbTeacher.Enabled = true;
                    rbUser.Enabled = true;
                }
                else if (Roles.IsUserInRole("manager"))
                {
                    rbAdmin.Enabled = true;
                    rbStudent.Enabled = true;
                    rbTeacher.Enabled = true;
                    rbUser.Enabled = true;
                }
            }
            else
            {
                rbAdmin.Enabled = false;
                rbStudent.Enabled = false;
                rbTeacher.Enabled = false;
                rbUser.Enabled = false;
            }
        }
        catch(Exception ex)
        {
            lblErrorList.Text = ex.Message;
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        if (sendFrom == "admin")
        {
            try
            {
                ////////////////////////
                string conString = RamanConnectionString.MyConnectionString();
                con = new SqlConnection(conString);
                SqlCommand cmd = con.CreateCommand();
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                ////////////////////////
                string currentUser = HttpContext.Current.User.Identity.Name;
                string userName = tbUserName.Text;
                string userPass = tbUserPass1.Text;
                string EmailAddress = tbEmailAddress.Text;
                string rolename = sendFrom;
                string fullname = tbFullname.Text;
                string gender = (rbMale.Checked ? "مرد" : (rbFemale.Checked ? "زن" : ""));
                string tahsilat = (cmbTahsilat.SelectedIndex > 0 ? cmbTahsilat.SelectedItem.ToString() : "");
                MembershipCreateStatus mcs = new MembershipCreateStatus();
                newUser = Membership.CreateUser(userName, userPass, EmailAddress, "noquestion", "noanswer", true, out mcs);
                if (newUser == null)
                {
                    lblErrorList.Text = C_myClass.GetErrorMessageCreateUser(mcs);
                }
                else
                {
                    Roles.AddUserToRole(newUser.UserName, "admin");
                    cmd.CommandText = t_AdminsInsertCommand(fullname, gender, tahsilat, userName, currentUser, "", "");
                    cmd.ExecuteNonQuery();
                    tr.Commit();
                    SuccessfullCreatedMessage();
                }
            }
            catch (Exception exc)
            {
                try
                {
                    if (newUser != null)
                        Membership.DeleteUser(newUser.UserName);
                    lblErrorList.Text = exc.Message;
                    tr.Rollback();
                }
                catch
                {
                }
            }
            finally
            {
                con.Close();
            }
        }
        else if (sendFrom == "teacher")
        {
            try
            {
                ////////////////////////
                string conString = RamanConnectionString.MyConnectionString();
                con = new SqlConnection(conString);
                SqlCommand cmd = con.CreateCommand();
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                ////////////////////////
                string currentUser = HttpContext.Current.User.Identity.Name;
                string userName = tbUserName.Text;
                string userPass = tbUserPass1.Text;
                string EmailAddress = tbEmailAddress.Text;
                string rolename = sendFrom;
                string fullname = tbFullname.Text;
                string gender = (rbMale.Checked ? "مرد" : (rbFemale.Checked ? "زن" : ""));
                string tahsilat = (cmbTahsilat.SelectedIndex > 0 ? cmbTahsilat.SelectedItem.ToString() : "");
                MembershipCreateStatus mcs = new MembershipCreateStatus();
                newUser = Membership.CreateUser(userName, userPass, EmailAddress, "noquestion", "noanswer", true, out mcs);
                if (newUser == null)
                {
                    lblErrorList.Text = C_myClass.GetErrorMessageCreateUser(mcs);
                }
                else
                {
                    Roles.AddUserToRole(newUser.UserName, "teacher");
                    cmd.CommandText = t_TeachersInsertCommand(fullname, gender, tahsilat, userName, currentUser, "", "");
                    cmd.ExecuteNonQuery();
                    tr.Commit();
                    SuccessfullCreatedMessage();
                }
            }
            catch(Exception exc)
            {
                try
                {
                    if (newUser != null)
                        Membership.DeleteUser(newUser.UserName);
                    lblErrorList.Text = exc.Message;
                    tr.Rollback();
                }
                catch
                {
                }
            }
            finally
            {
                con.Close();
            }
        }
        else if (sendFrom == "student")
        {
            try
            {
                ////////////////////////
                string conString = RamanConnectionString.MyConnectionString();
                con = new SqlConnection(conString);
                SqlCommand cmd = con.CreateCommand();
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                ////////////////////////
                string currentUser = HttpContext.Current.User.Identity.Name;
                string userName = tbUserName.Text;
                string userPass = tbUserPass1.Text;
                string EmailAddress = tbEmailAddress.Text;
                string rolename = sendFrom;
                string fullname = tbFullname.Text;
                string gender = (rbMale.Checked ? "مرد" : (rbFemale.Checked ? "زن" : ""));
                string tahsilat = (cmbTahsilat.SelectedIndex > 0 ? cmbTahsilat.SelectedItem.ToString() : "");
                MembershipCreateStatus mcs = new MembershipCreateStatus();
                newUser = Membership.CreateUser(userName, userPass, EmailAddress, "noquestion", "noanswer", true, out mcs);
                if (newUser == null)
                {
                    lblErrorList.Text = C_myClass.GetErrorMessageCreateUser(mcs);
                }
                else
                {
                    Roles.AddUserToRole(newUser.UserName, "student");
                    cmd.CommandText = t_StudentsInsertCommand(fullname, gender, tahsilat, userName, currentUser, "", "", "");
                    cmd.ExecuteNonQuery();
                    tr.Commit();
                    SuccessfullCreatedMessage();
                }
            }
            catch (Exception exc)
            {
                try
                {
                    if (newUser != null)
                        Membership.DeleteUser(newUser.UserName);
                    lblErrorList.Text = exc.Message;
                    tr.Rollback();
                }
                catch
                {
                }
            }
            finally
            {
                con.Close();
            }
        }
        else if (sendFrom == "user")
        {
            try
            {
                ////////////////////////
                string conString = RamanConnectionString.MyConnectionString();
                con = new SqlConnection(conString);
                SqlCommand cmd = con.CreateCommand();
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                ////////////////////////
                string currentUser = HttpContext.Current.User.Identity.Name;
                string userName = tbUserName.Text;
                string userPass = tbUserPass1.Text;
                string EmailAddress = tbEmailAddress.Text;
                string rolename = sendFrom;
                string fullname = tbFullname.Text;
                string gender = (rbMale.Checked ? "مرد" : (rbFemale.Checked ? "زن" : ""));
                string tahsilat = (cmbTahsilat.SelectedIndex > 0 ? cmbTahsilat.SelectedItem.ToString() : "");
                MembershipCreateStatus mcs = new MembershipCreateStatus();
                newUser = Membership.CreateUser(userName, userPass, EmailAddress, "noquestion", "noanswer", true, out mcs);
                if (newUser == null)
                {
                    lblErrorList.Text = C_myClass.GetErrorMessageCreateUser(mcs);
                }
                else
                {
                    Roles.AddUserToRole(newUser.UserName, "user");
                    cmd.CommandText = t_UsersInsertCommand(fullname, gender, tahsilat, userName, currentUser, "");
                    cmd.ExecuteNonQuery();
                    tr.Commit();
                    SuccessfullCreatedMessage();
                }
            }
            catch (Exception exc)
            {
                try
                {
                    if (newUser != null)
                        Membership.DeleteUser(newUser.UserName);
                    lblErrorList.Text = exc.Message;
                    tr.Rollback();
                }
                catch
                {
                }
            }
            finally
            {
                con.Close();
            }
        }
    }

    private void SuccessfullCreatedMessage()
    {
        lblErrorList.ForeColor = Color.Green;
        string msg = "کاربر جدید با موفقیت ایجاد شد.";
        lblErrorList.Text = msg;
    }

    private string t_AdminsInsertCommand(string FullName, string Gender, string Tahsilat, string UserName, string UserCreater, string AdminPost, string PhotoUrl)
    {
        string comm = "insert into t_Admins (fullName, gender, tahsilat, userName, userCreater, adminPost, adminPhotoUrl) values (N'" +
            FullName + "', N'" +
            Gender + "', N'" +
            Tahsilat + "', N'" +
            UserName + "', N'" +
            UserCreater + "', N'" +
            AdminPost + "', N'" +
            PhotoUrl + "')";
        return comm;
    }

    private string t_UsersInsertCommand(string FullName, string Gender, string Tahsilat, string UserName, string UserCreater, string PhotoUrl)
    {
        string comm = "insert into t_Users (fullName, gender, tahsilat, userName, userCreater, userPhotoUrl) values (N'" +
            FullName + "', N'" +
            Gender + "', N'" +
            Tahsilat + "', N'" +
            UserName + "', N'" +
            UserCreater + "', N'" +
            PhotoUrl + "')";
        return comm;
    }

    private string t_StudentsInsertCommand(string FullName, string Gender, string Tahsilat, string UserName, string UserCreater, string DateOfBirth, string MelliCode, string PhotoUrl)
    {
        string comm = "insert into t_Students (fullName, gender, tahsilat, userName, userCreater, dateOfBirth, melliCode, studentPhotoUrl) values (N'" +
            FullName + "', N'" +
            Gender + "', N'" +
            Tahsilat + "', N'" +
            UserName + "', N'" +
            UserCreater + "', N'" +
            DateOfBirth + "' , N'" +
            MelliCode + "' , N'" +
            PhotoUrl + "')";
        return comm;
    }

    private string t_TeachersInsertCommand(string FullName, string Gender, string Tahsilat, string UserName, string UserCreater, string History, string PhotoUrl)
    {
        string comm = "insert into t_Teachers (fullname, gender, tahsilat, userName, userCreater, history, teacherPhotoUrl) values (N'" +
            FullName + "', N'" +
            Gender + "', N'" +
            Tahsilat + "', N'" +
            UserName + "', N'" +
            UserCreater + "', N'" +
            History + "', N'" +
            PhotoUrl + "')";
        return comm;
    }
}
