示例#1
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var needsDbAction = false;

            if (_record == null)
            {
                _record = new Record {
                    Attributes = new Dictionary <string, string>(), Active = true
                }
            }
            ;

            foreach (var ctrl in _txtControls)
            {
                var attrId  = ctrl.Tag.ToString();
                var attrVal = ctrl.Text;

                if (attrVal.Length == 0)
                {
                    attrVal = null;
                }

                if (!_record.Attributes.ContainsKey(attrId))
                {
                    _record.Attributes.Add(attrId, null);
                }

                var currAttrVal = _record.Attributes[attrId];

                if (attrVal != currAttrVal)
                {
                    _record.Attributes[attrId] = attrVal;
                    needsDbAction = true;
                }
            }

            if (needsDbAction)
            {
                using (var oH = new ObjectHelper(_dbSettings, _user))
                {
                    StatusObject dbActionStatus;
                    if (_record.Id == 0)
                    {
                        dbActionStatus = oH.AddRecord(_recordType, _record.Attributes, true);
                        if (dbActionStatus.Success)
                        {
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                        else
                        {
                            NotificationHelper.ShowNotification(this, NotificationHelper.NotificationType.Error,
                                                                "Failed to add this new record. Please try again.");
                        }
                    }
                    else
                    {
                        dbActionStatus = oH.UpdateRecord(_record);
                        if (dbActionStatus.Success)
                        {
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                        else
                        {
                            NotificationHelper.ShowNotification(this, NotificationHelper.NotificationType.Error,
                                                                "Failed to update this record. Please try again.");
                        }
                    }
                }
            }
            else
            {
                DialogResult = DialogResult.Cancel;
                Close();
            }
        }