private void Save()
    {
        if (ViewState["mode"].ToString() == "Add")
        {
            ProgrammingContestDataContextHandler pcdch = new ProgrammingContestDataContextHandler();
            Data.User user = new Data.User();
            user.Name = txtName.Text;
            user.IPAddress = txtIPAddress.Text;
            pcdch.ProgrammingContestDatabase.Users.InsertOnSubmit(user);
            pcdch.ProgrammingContestDatabase.SubmitChanges();

            user = pcdch.Users.GetByName(txtName.Text);

            long contestId = Convert.ToInt64(ddlSelectContest.SelectedValue);

            if (CheckBox1.Checked == true)
            {
                if (pcdch.ContestPermission.GetByUserIdAndContestId(user.ID, contestId) == null)
                {
                    ContestPermission cp = new ContestPermission();
                    cp.ContestSetting = pcdch.ContestSetting.GetById(contestId);
                    cp.ContestId = contestId;
                    cp.User = user;
                    cp.UserId = user.ID;
                    pcdch.ProgrammingContestDatabase.ContestPermissions.InsertOnSubmit(cp);
                }
            }
            else
            {
                if (pcdch.ContestPermission.GetByUserIdAndContestId(user.ID, contestId) != null)
                {
                    ContestPermission cp = pcdch.ContestPermission.GetByUserIdAndContestId(user.ID, contestId);
                    pcdch.ProgrammingContestDatabase.ContestPermissions.DeleteOnSubmit(cp);
                }
            }

            pcdch.ProgrammingContestDatabase.SubmitChanges();
            rptProblems.DataSource = pcdch.Users.GetAllWithOutAdmin();
            rptProblems.DataBind();

        }
        else if (ViewState["mode"].ToString() == "Edit")
        {
            ProgrammingContestDataContextHandler pcdch = new ProgrammingContestDataContextHandler();
            Data.User user = pcdch.Users.GetById(Convert.ToInt64(ViewState["ID"].ToString()));
            user.Name = txtName.Text;
            user.IPAddress = txtIPAddress.Text;
            pcdch.ProgrammingContestDatabase.SubmitChanges();

            user = pcdch.Users.GetByName(txtName.Text);

            long contestId = Convert.ToInt64(ddlSelectContest.SelectedValue);

            if (CheckBox1.Checked == true)
            {
                if (pcdch.ContestPermission.GetByUserIdAndContestId(user.ID, contestId) == null)
                {
                    ContestPermission cp = new ContestPermission();
                    cp.ContestSetting = pcdch.ContestSetting.GetById(contestId);
                    cp.ContestId = contestId;
                    cp.User = user;
                    cp.UserId = user.ID;
                    pcdch.ProgrammingContestDatabase.ContestPermissions.InsertOnSubmit(cp);
                }
            }
            else
            {
                if (pcdch.ContestPermission.GetByUserIdAndContestId(user.ID, contestId) != null)
                {
                    ContestPermission cp = pcdch.ContestPermission.GetByUserIdAndContestId(user.ID, contestId);
                    pcdch.ProgrammingContestDatabase.ContestPermissions.DeleteOnSubmit(cp);
                }
            }

            pcdch.ProgrammingContestDatabase.SubmitChanges();
            rptProblems.DataSource = pcdch.Users.GetAllWithOutAdmin();
            rptProblems.DataBind();
        }

        mvProblem.ActiveViewIndex = 0;
    }
		private void detach_ContestPermissions(ContestPermission entity)
		{
			this.SendPropertyChanging();
			entity.ContestSetting = null;
		}
 partial void DeleteContestPermission(ContestPermission instance);
 partial void UpdateContestPermission(ContestPermission instance);
 partial void InsertContestPermission(ContestPermission instance);
		private void attach_ContestPermissions(ContestPermission entity)
		{
			this.SendPropertyChanging();
			entity.User = this;
		}