protected void ExpertAddButtonClick(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            if (button != null)
            {
                var appIdTmp = Session["ApplicationID"];
                if (appIdTmp == null)
                {
                    Response.Redirect("ApllicationExpertEdit.aspx");
                }
                int applicationId = Convert.ToInt32(appIdTmp);

                CompetitionDataContext CompetitionsDataBase = new CompetitionDataContext();
                zExpertsAndApplicationMappingTable expertAndApplicationConnection = (from a in CompetitionsDataBase.zExpertsAndApplicationMappingTable
                                                                                     where a.FK_UsersTable == Convert.ToInt32(button.CommandArgument)
                                                                                     && a.FK_ApplicationsTable == applicationId
                                                                                     select a).FirstOrDefault();
                if (expertAndApplicationConnection != null)
                {
                    expertAndApplicationConnection.Active = true;
                    CompetitionsDataBase.SubmitChanges();
                }
                else
                {
                    expertAndApplicationConnection = new zExpertsAndApplicationMappingTable();
                    expertAndApplicationConnection.Active = true;
                    expertAndApplicationConnection.FK_ApplicationsTable = applicationId;
                    expertAndApplicationConnection.FK_UsersTable = Convert.ToInt32(button.CommandArgument);
                    CompetitionsDataBase.zExpertsAndApplicationMappingTable.InsertOnSubmit(expertAndApplicationConnection);
                    CompetitionsDataBase.SubmitChanges();
                }
            }
            Response.Redirect("ApllicationExpertEdit.aspx");
        }
        protected void DeleteExpertButtonClick(object sender, EventArgs e)
        {
            var         sessionParam1 = Session["ApplicationID"];
            ImageButton button        = (ImageButton)sender;

            if (sessionParam1 == null && button != null)
            {
                //error
                Response.Redirect("~/Default.aspx");
            }
            int applicationId = Convert.ToInt32(sessionParam1);
            int userId        = Convert.ToInt32(button.CommandArgument);
            CompetitionDataContext competitionDataBase = new CompetitionDataContext();
            zApplicationTable      currentApplication  = (from a in competitionDataBase.zApplicationTable
                                                          where a.Active == true && a.Accept == false &&
                                                          a.ID == applicationId &&
                                                          a.Sended == true
                                                          select a).FirstOrDefault();

            if (currentApplication != null)
            {
                zExpertsAndApplicationMappingTable expertlink =
                    (from a in competitionDataBase.zExpertsAndApplicationMappingTable
                     where a.Active == true && a.FK_ApplicationsTable == applicationId &&
                     a.FK_UsersTable == userId
                     select a).FirstOrDefault();
                if (expertlink != null)
                {
                    expertlink.Active = false;
                    competitionDataBase.SubmitChanges();
                }
            }
            Response.Redirect("ApplicationExpertsAndExpertsGroupEdit.aspx");
        }
        protected void AddExpertGroupClick(object sender, EventArgs e)
        {
            var         sessionParam1 = Session["ApplicationID"];
            ImageButton button        = (ImageButton)sender;

            if (sessionParam1 == null && button != null)
            {
                //error
                Response.Redirect("~/Default.aspx");
            }
            int applicationId = Convert.ToInt32(sessionParam1);
            int groupId       = Convert.ToInt32(button.CommandArgument);
            CompetitionDataContext competitionDataBase = new CompetitionDataContext();
            zApplicationTable      currentApplication  = (from a in competitionDataBase.zApplicationTable
                                                          where a.Active == true && a.Accept == false &&
                                                          a.ID == applicationId &&
                                                          a.Sended == true
                                                          select a).FirstOrDefault();

            if (currentApplication != null)
            {
                List <zExpertAndExpertGroupMappingTable> currentgroup =
                    (from a in competitionDataBase.zExpertAndExpertGroupMappingTable
                     where a.Active == true && a.FK_ExpertGroupTable == groupId
                     select a).ToList();
                if (currentgroup != null)
                {
                    foreach (var n in currentgroup)
                    {
                        zExpertsAndApplicationMappingTable expertlink =
                            (from a in competitionDataBase.zExpertsAndApplicationMappingTable
                             where a.FK_ApplicationsTable == applicationId &&
                             a.FK_UsersTable == n.FK_UsersTable
                             select a).FirstOrDefault();
                        if (expertlink == null)
                        {
                            zExpertsAndApplicationMappingTable newexpertlink = new zExpertsAndApplicationMappingTable();
                            newexpertlink.Active = true;
                            newexpertlink.FK_ApplicationsTable = currentApplication.ID;
                            newexpertlink.FK_UsersTable        = n.FK_UsersTable;
                            competitionDataBase.zExpertsAndApplicationMappingTable.InsertOnSubmit(newexpertlink);
                            competitionDataBase.SubmitChanges();
                        }
                        else
                        {
                            if (expertlink.Active == false)
                            {
                                expertlink.Active = true;
                                competitionDataBase.SubmitChanges();
                            }
                        }
                    }
                }
            }
            Response.Redirect("ApplicationExpertsAndExpertsGroupEdit.aspx");
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            var sessionParam1 = Session["ApplicationID"];

            if (sessionParam1 == null)
            {
                //error
                Response.Redirect("~/Default.aspx");
            }
            int         applicationId = Convert.ToInt32(sessionParam1);
            ImageButton addexpert     = (ImageButton)e.Row.FindControl("AddExpertButton");
            ImageButton deletexpert   = (ImageButton)e.Row.FindControl("DeleteExpertButton");
            ImageButton addgroup      = (ImageButton)e.Row.FindControl("AddExpertGroup");
            ImageButton deletegroup   = (ImageButton)e.Row.FindControl("DeleteGroupButton");
            Label       status        = (Label)e.Row.FindControl("StatusLabel");
            Label       groupstatus   = (Label)e.Row.FindControl("GroupStatusLabel");

            if (addexpert != null && deletexpert != null && addgroup != null)
            {
                if (Convert.ToInt32(addgroup.CommandArgument) == 0 && Convert.ToInt32(deletegroup.CommandArgument) == 0)
                {
                    addgroup.Visible    = false;
                    deletegroup.Visible = false;
                }
                CompetitionDataContext CompetitionsDataBase = new CompetitionDataContext();

                zExpertsAndApplicationMappingTable buttonstate =
                    (from a in CompetitionsDataBase.zExpertsAndApplicationMappingTable
                     where a.FK_UsersTable == Convert.ToInt32(addexpert.CommandArgument) &&
                     a.FK_ApplicationsTable == applicationId
                     select a).FirstOrDefault();
                if (buttonstate == null)
                {
                    deletexpert.Visible = false;
                }
                else
                {
                    if (buttonstate.Active == true)
                    {
                        addexpert.Visible = false;
                        status.Visible    = true;
                    }
                    else
                    {
                        deletexpert.Visible = false;
                    }
                }
                List <zExpertAndExpertGroupMappingTable> currentgroup =
                    (from a in CompetitionsDataBase.zExpertAndExpertGroupMappingTable
                     where a.Active == true && a.FK_ExpertGroupTable == Convert.ToInt32(addgroup.CommandArgument)
                     select a).ToList();
                if (currentgroup != null)
                {
                    foreach (var n in currentgroup)
                    {
                        zExpertsAndApplicationMappingTable expertlink =
                            (from a in CompetitionsDataBase.zExpertsAndApplicationMappingTable
                             where a.FK_ApplicationsTable == applicationId &&
                             a.FK_UsersTable == n.FK_UsersTable
                             select a).FirstOrDefault();
                        if (expertlink != null)
                        {
                            if (expertlink.Active == true)
                            {
                                addgroup.Visible = false;
                            }
                            else
                            {
                                deletegroup.Visible = false;
                            }
                        }
                    }
                }
            }
        }