Skip to content

Commit

Permalink
Exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirdock committed Sep 15, 2021
1 parent 406fc35 commit fb42455
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
3 changes: 1 addition & 2 deletions DataTableConverter/Assisstant/DatabaseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,11 +1599,10 @@ internal Dictionary<string, long> GroupCountOfColumn(string columnName, string t

internal void SplitTableOnRowValue(Dictionary<string, string[]> dict, string column, string tableName)
{
SQLiteConnection connection = GetConnection(tableName);
Dictionary<string, string> aliasColumnMapping = GetAliasColumnMapping(tableName);
string headerString = GetHeaderString(aliasColumnMapping.Keys);
string valueString = GetValueString(aliasColumnMapping.Count);
using (SQLiteCommand command = connection.CreateCommand())
using (SQLiteCommand command = GetConnection(tableName).CreateCommand())
{
CreateIndexOn(tableName, column, null, false);
command.CommandText = $"SELECT {headerString} from [{tableName}] where [{column}] = ? COLLATE CASESENSITIVE";
Expand Down
7 changes: 6 additions & 1 deletion DataTableConverter/Assisstant/ErrorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ internal static void LogMessage(Exception exception, Form mainForm, bool showMes
{
ShowError($"Die Datei {path.FileName} kann nicht gefunden werden", mainForm);
}
else if(exception is FileLoadException ex)
{
ShowError($"Die Datei {ex.FileName} kann nicht geladen werden\nWird sie von einem anderen Prozess verwendet?", mainForm);
LogMessage(exception.ToString(), mainForm, false);
}
else
{
LogMessage(exception.ToString(), mainForm, showMessage);
Expand All @@ -26,7 +31,7 @@ internal static void LogMessage(string text, Form mainForm, bool showMessage = t
{
try
{
File.AppendAllText(path, $"{Environment.NewLine}{DateTime.Today}; Version:{FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion} {Environment.NewLine}{text}{Environment.NewLine}");
File.AppendAllText(path, $"{Environment.NewLine}{DateTime.Now}; Version:{FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion} {Environment.NewLine}{text}{Environment.NewLine}");
if (showMessage)
{
ShowError(ErrorMessage, mainForm);
Expand Down
2 changes: 1 addition & 1 deletion DataTableConverter/Assisstant/ImportHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ private string GetFormattedValue(object value, string formatString, int formatIn
internal void OpenExcel(string path, ProgressBar progressBar, Form mainForm, string tableName, ref string password)
{

using (var stream = File.Open(path, FileMode.Open, FileAccess.Read))
using (var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
// Auto-detect format, supports:
// - Binary Excel files (2.0-2003 format; *.xls)
Expand Down
2 changes: 1 addition & 1 deletion DataTableConverter/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.16.0")]
[assembly: AssemblyFileVersion("1.0.17.0")]
56 changes: 32 additions & 24 deletions DataTableConverter/View/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,34 +661,42 @@ private void LoadData(bool readjustColumnWidth = true, bool preventLoading = fal

void load()
{
if (readjustColumnWidth)
{
ColumnWidths.Clear();
}
else
try
{
SaveWidthOfDataGridViewColumns();
}
MaxPages = Math.Ceiling(DatabaseHelper.GetRowCount(TableName) / Properties.Settings.Default.MaxRows);
SetPage();
CheckAllowToAddRows();
if (readjustColumnWidth)
{
ColumnWidths.Clear();
}
else
{
SaveWidthOfDataGridViewColumns();
}
MaxPages = Math.Ceiling(DatabaseHelper.GetRowCount(TableName) / Properties.Settings.Default.MaxRows);
SetPage();
CheckAllowToAddRows();

int scrollBarHorizontal = dgTable.HorizontalScrollingOffset;
if (!preventLoading)
{
DataTable table = DatabaseHelper.GetData(SortingOrder, OrderType, (int)((Page - 1) * Properties.Settings.Default.MaxRows), TableName);

dgTable.RowsAdded -= dgTable_RowsAdded;
dgTable.DataSource = null; //else readded columns are at the wrong index
dgTable.DataSource = table;
dgTable.Columns[0].Visible = false;
dgTable.RowsAdded += dgTable_RowsAdded;
SetRowCount(DatabaseHelper.GetRowCount(TableName));
}
RestoreDataGridSortMode();
SetWidth();

int scrollBarHorizontal = dgTable.HorizontalScrollingOffset;
if (!preventLoading)
dgTable.HorizontalScrollingOffset = scrollBarHorizontal;
}
catch(OutOfMemoryException)
{
DataTable table = DatabaseHelper.GetData(SortingOrder, OrderType, (int)((Page - 1) * Properties.Settings.Default.MaxRows), TableName);

dgTable.RowsAdded -= dgTable_RowsAdded;
dgTable.DataSource = null; //else readded columns are at the wrong index
dgTable.DataSource = table;
dgTable.Columns[0].Visible = false;
dgTable.RowsAdded += dgTable_RowsAdded;
SetRowCount(DatabaseHelper.GetRowCount(TableName));
ErrorHelper.LogMessage("Es können nicht so viele Zeilen gelesen werden. Bitte reduzieren sie die maximal geladenen Zeilen pro Seite in den Einstellungen", this);
GC.Collect();
}
RestoreDataGridSortMode();
SetWidth();

dgTable.HorizontalScrollingOffset = scrollBarHorizontal;
}
}

Expand Down

0 comments on commit fb42455

Please sign in to comment.