From 43aa4006138c253c8ca3071994a797f1b03a355c Mon Sep 17 00:00:00 2001 From: frauseo Date: Tue, 26 May 2020 21:18:56 +0200 Subject: [PATCH] Fix SqlNullValueException --- .../LoggingClient/LoggingClient.csproj | 2 +- .../LoggingClient/ViewModel/LogViewModel.cs | 38 +++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/LoggingClient/LoggingClient/LoggingClient.csproj b/LoggingClient/LoggingClient/LoggingClient.csproj index a928bb3..64bde42 100644 --- a/LoggingClient/LoggingClient/LoggingClient.csproj +++ b/LoggingClient/LoggingClient/LoggingClient.csproj @@ -5,7 +5,7 @@ Debug AnyCPU {2622C2FC-3522-4D6F-B021-F63A243E77F1} - Library + WinExe LoggingClient LoggingClient v4.6.1 diff --git a/LoggingClient/LoggingClient/ViewModel/LogViewModel.cs b/LoggingClient/LoggingClient/ViewModel/LogViewModel.cs index fefa987..67dac29 100644 --- a/LoggingClient/LoggingClient/ViewModel/LogViewModel.cs +++ b/LoggingClient/LoggingClient/ViewModel/LogViewModel.cs @@ -10,7 +10,7 @@ using LoggingClient.ViewModel.Commands; namespace LoggingClient.ViewModel { - public class LogViewModel : INotifyPropertyChanged + public class LogViewModel : INotifyPropertyChanged { private string _txtConnectionString; private string _enterPod; @@ -142,28 +142,36 @@ namespace LoggingClient.ViewModel conn.Open(); using (var cmd = new MySqlCommand("SELECT id, pod, location, hostname, severity, timestamp, message FROM v_logentries ORDER BY timestamp", conn)) { - var reader = cmd.ExecuteReader(); - while (reader.Read()) - { - Logs.Add(new LogModel( - reader.GetInt32("id"), - reader.GetString("pod"), - reader.GetString("location"), - reader.GetString("hostname"), - reader.GetInt32("severity"), - reader.GetDateTime("timestamp"), - reader.GetString("message") - )); + var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + // ISDBNull Check because location can be NULL in Database + var location = ""; + if (!reader.IsDBNull(reader.GetOrdinal("location"))) + { + location = reader.GetString("location"); + } + Logs.Add(new LogModel( + reader.GetInt32("id"), + reader.GetString("pod"), + location, + reader.GetString("hostname"), + reader.GetInt32("severity"), + reader.GetDateTime("timestamp"), + reader.GetString("message") + )); } } + conn.Close(); } } catch (Exception ex) { - //MessageBox.Show("Error occurred: " + ex.Message); + MessageBox.Show("Error occurred: " + ex.Message); } - } + } + private void BtnLogClear_Click() { if (MySelectedItem == null) return;