コード例 #1
0
ファイル: Viewer.cs プロジェクト: donkeizluv/LO_Inventory
        public virtual void InsertButtonHandler()
        {
            try
            {
                if (!HelperMethods.ShowCSVFileBrowser(this, out string path))
                {
                    return;
                }
                var content = HelperMethods.ReadCSV(path);
                if (MessageBox.Show($"Are you sure to insert: {content.Count} record(s)?", "Confirm",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes)
                {
                    return;
                }
                //start insering
                var affedted = Controller.Insert(content);
                ActionLogger.AddToLog($"Insert {Text}", $"Success inserted: {affedted}");
                //refresh main grid
                ButtonRefresh_Click(this, EventArgs.Empty);
            }
            //catch all the parsing, db, entities exceptions here!
            catch (ArgumentException ex) //who throws this???
            {
                throw;
            }
            catch (EntityParsingException ex) //parsing exception
            {
                var mess = string.Empty;
                if (ex.ErrorIndex != -1)
                {
                    mess = $"{ex.Message} row: [{ex.ErrorIndex + 1}]";
                }
                if (ex.ValueName != string.Empty)
                {
                    mess += $", value name: [{ex.ValueName}]";
                }
                if (ex.RawValue != string.Empty)
                {
                    mess += $", raw value: [{ex.RawValue}]";
                }
                var actionLog = new ActionLog($"Insert {Text}", mess, true);
                ActionLogger.AddToLog(actionLog);
            }
            catch (DbUpdateException ex) //db exception
            {
                var dbErrors   = EntityParser.TryDecodeDbUpdateException(ex);
                var actions    = EntityParser.ValidationResultToActionLog($"Insert {Text}", dbErrors, true);
                var actionLogs = new List <ActionLog>();
                if (actions == null)
                {
                    actionLogs = new List <ActionLog>
                    {
                        new ActionLog($"Insert {Text}", "Unknown error.", true)
                    };
                }
                else
                {
                    actionLogs = actions.ToList();
                }

                ActionLogger.AddToLog(actionLogs);
            }
        }