Exemple #1
0
    public static string SaveChanges(string rows)
    {
        Dictionary <string, string> result = new Dictionary <string, string>()
        {
            { "saved", "" }, { "ids", "" }, { "error", "" }
        };
        bool   exists = false, saved = false;
        string ids = string.Empty, errorMsg = string.Empty, tempMsg = string.Empty;

        try
        {
            DataTable dtjson = (DataTable)JsonConvert.DeserializeObject(rows, (typeof(DataTable)));
            if (dtjson.Rows.Count == 0)
            {
                errorMsg = "Unable to save. An invalid list of changes was provided.";
                saved    = false;
            }

            int    id = 0, priorty = 0, dailyMeetings = 0, archive = 0;
            string allocationGroup = string.Empty, description = string.Empty, notes = string.Empty;

            HttpServerUtility server = HttpContext.Current.Server;
            //save
            foreach (DataRow dr in dtjson.Rows)
            {
                id = 0;
                allocationGroup = string.Empty;
                description     = string.Empty;
                notes           = string.Empty;
                priorty         = 0;
                dailyMeetings   = 0;
                archive         = 0;

                tempMsg = string.Empty;
                int.TryParse(dr["ALLOCATIONGROUPID"].ToString(), out id);
                allocationGroup = Uri.UnescapeDataString(dr["ALLOCATIONGROUP"].ToString());
                description     = Uri.UnescapeDataString(dr["DESCRIPTION"].ToString());
                notes           = Uri.UnescapeDataString(dr["NOTES"].ToString());
                int.TryParse(dr["PRIORTY"].ToString(), out priorty);
                int.TryParse(dr["DAILYMEETINGS"].ToString(), out dailyMeetings);
                int.TryParse(dr["ARCHIVE"].ToString(), out archive);

                if (string.IsNullOrWhiteSpace(allocationGroup))
                {
                    tempMsg = "You must specify a value for allocation group.";
                    saved   = false;
                }
                else
                {
                    if (id == 0)
                    {
                        exists = false;
                        saved  = MasterData.AllocationGroup_Add(allocationGroup, description, notes, priorty, dailyMeetings == 1, archive == 1, out exists, out id, out tempMsg);
                        if (exists)
                        {
                            saved   = false;
                            tempMsg = string.Format("{0}{1}{2}", tempMsg, tempMsg.Length > 0 ? Environment.NewLine : "", "Cannot add duplicate Allocation Group record [" + allocationGroup + "].");
                        }
                    }
                    else
                    {
                        saved = MasterData.AllocationGroup_Update(id, allocationGroup, description, notes, priorty, dailyMeetings == 1, archive == 1, out tempMsg);
                    }
                }

                if (saved)
                {
                    ids += string.Format("{0}{1}", ids.Length > 0 ? "," : "", id.ToString());
                }

                if (tempMsg.Length > 0)
                {
                    errorMsg = string.Format("{0}{1}{2}", errorMsg, errorMsg.Length > 0 ? Environment.NewLine : "", tempMsg);
                }
            }
        }
        catch (Exception ex)
        {
            LogUtility.LogException(ex);
            saved    = false;
            errorMsg = ex.Message;
        }

        result["saved"] = saved.ToString();
        result["error"] = errorMsg;

        return(JsonConvert.SerializeObject(result, Formatting.None));
    }