Capture error when click button

Asked

Viewed 82 times

1

I want to make a button that when it is clicked is shown the information of SQL Server and say that it is connected (have to configure the file in XML Configuration File)

See below the code:

public class Functions
{
    public static void conn()
    {
        string connectionString = Conn.tank();
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            bool Open = true;
            if (Open)
            {
                connection.Open();
                Globals.UpdateLogs("Conexão testada:");
                Globals.UpdateLogs("Status da Conexão : " + connection.State);  //info
                Globals.UpdateLogs("User : " + connection.WorkstationId);  //info
                Globals.UpdateLogs("Banco de dados : " + connection.Database);  //info
                Globals.UpdateLogs("Versão SQL : " + connection.ServerVersion); //info
            }
            else
            {
                Globals.UpdateLogs("O programa não está conectado em sua DataBase, verifique as configurações");
            }

        }
    }

In case it works right when you press the button and the data are right it says the most settings when SQL Server data is wrong, it stops working the program.

I took a code already ready I’m just trying to make it better, but I can’t do "If the data is wrong show the message".

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

1

I’m not sure I quite understand what you want and not even if that code makes sense, but that’s kind of what I think you want:

public class Functions {
    public static void conn() {
        string connectionString = Conn.tank();
        var connection = new SqlConnection(connectionString);
        try {
            connection.Open();
            Globals.UpdateLogs("Conexão testada:");
            Globals.UpdateLogs("Status da Conexão : " + connection.State);
            Globals.UpdateLogs("User : " + connection.WorkstationId);
            Globals.UpdateLogs("Banco de dados : " + connection.Database);
            Globals.UpdateLogs("Versão SQL : " + connection.ServerVersion);
        } catch (SystemException ex) {
            if (ex is InvalidOperationException || ex is SqlException || ex is ConfigurationErrorsException) {
                Globals.UpdateLogs("O programa não está conectado em sua DataBase, verifique as configurações");
            }
        } finally {
            if (connection != null) {
                ((IDisposable)connection).Dispose();
            }
        }
    }
}

I put in the Github for future reference.

Surely you have better ways of doing that, but you’d have to think about design as a whole.

For C# 5 or earlier:

public class Functions {
    public static void conn() {
        string connectionString = Conn.tank();
        var connection = new SqlConnection(connectionString);
        try {
            connection.Open();
            Globals.UpdateLogs("Conexão testada:");
            Globals.UpdateLogs("Status da Conexão : " + connection.State);
            Globals.UpdateLogs("User : " + connection.WorkstationId);
            Globals.UpdateLogs("Banco de dados : " + connection.Database);
            Globals.UpdateLogs("Versão SQL : " + connection.ServerVersion);
        } catch (InvalidOperationException ex) {
            Globals.UpdateLogs("O programa não está conectado em sua DataBase, verifique as configurações");
        } catch (SqlException  ex) {
            Globals.UpdateLogs("pode por uma mensagem mais específica aqui");
        } catch (ConfigurationErrorsException ex) {
            Globals.UpdateLogs("pode por uma mensagem mais específica aqui");
        } finally {
            if (connection != null) {
                ((IDisposable)connection).Dispose();
            }
        }
    }
}
  • 1

    In the 4 line error: ; expected and when I put ; of the error and in the "ex is Configurationerrorsexeception of the error that was not found

  • There was a typo. Are you using C# 6? If not, it will get more complicated. As you didn’t make a [mcve], and as I don’t know how you applied, I can’t help much more than that.

  • I’m using Visualstudio 2012 I don’t know if you have C# 6, if you can’t install it?

  • No, I recommend using something new. I put as it is in C# old.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.