Search code examples
c#wpfdebuggingrelease

C# running configuration differ


I have a wpf application and i want to upload some data to a sql server on a click of a button:

private void Submit(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("helo");
    List<string> list = new();
    foreach (var element in StackPanel1.Children)
    {
    list.Add(s.GetResult());
    }

    MySqlConnection connection = new(connectionString);
    connection.Open();
    MySqlCommand cmd = new($"INSERT INTO tablename VALUES (\"{list[0]}\", \"{list[1]}\", \"{list[2]}\", \"{list[3]}\");", connection);
    Debug.WriteLine($"Successful execution, rows affected: {cmd.ExecuteNonQuery()}");
    connection.Close();
}

If i run the program in debug mode everything is working perfectly fine but when i run it in release mode and i click the button nothing happens, not even the Debug.WriteLine("helo"); runs.


Solution

  • According to System.Diagnostics.Debug:

    In Visual Studio C# and Visual Basic projects, by default, the DEBUG conditional compilation symbol is defined for debug builds,...

    Placing cmd.ExecuteNonQuery() inside a Debug.WriteLine statement is the source of your issue. You also should use using statements for anything that has a Dispose method.