2
Oops good night,
Personal to with a doubt that it is something silly, is that I am not accustomed to use an external connection class in the development, had this bad-habit but now I went to use and to find a Unreachable code if someone can help me/ explain why I appreciate :) Follows class and method error ta no con.disconnect();:
public class Conexao:IDisposable
{
    private static string stringConexao = ConfigurationManager.ConnectionStrings["stringConexao"].ConnectionString;
    private SqlConnection con = new SqlConnection(stringConexao);
    public void conectar()
    {
        if (con.State == System.Data.ConnectionState.Closed)
        {
            con.Open();
        }
    }
    public void desconectar()
    {
        if (con.State == System.Data.ConnectionState.Open)
        {
            con.Close();
        }
    }
    public SqlConnection getCon()
    {
        return con;
    }
}
public int cadastrarAssociado (Associado assoc)
    {
        using(Conexao con = new Conexao())
        {
            using(SqlCommand comando = new SqlCommand(procCadastrar, con.getCon()))
            {
                comando.CommandType = System.Data.CommandType.StoredProcedure;
                comando.Parameters.AddWithValue("@cpf", assoc.Cpf);
                comando.Parameters.AddWithValue("@nome", assoc.Nome);
                comando.Parameters.AddWithValue("@rg", assoc.Rg);
                comando.Parameters.AddWithValue("@data_nasc", assoc.Nascimento.Date);
                comando.Parameters.AddWithValue("@endereco", assoc.Endereco);
                comando.Parameters.AddWithValue("@apto", assoc.Apto);
                comando.Parameters.AddWithValue("@cidade", assoc.Cidade);
                comando.Parameters.AddWithValue("@estado", assoc.Estado);
                comando.Parameters.AddWithValue("@email", assoc.Email);
                comando.Parameters.AddWithValue("@usuario", assoc.Login);
                comando.Parameters.AddWithValue("@senha", assoc.Senha);
                try
                {
                    con.conectar();
                    return Convert.ToInt32(comando.ExecuteScalar());
                    con.desconectar();
                }
                catch (SqlException)
                {
                    throw;
                }
            }
        }
    }
Just remove the line! and its connection class has not been implemented Idisposable?
– user46523
But if I remove the line the connection will not close, It was the Idisposable only for when you finish using the using take from memory
– Filipe
If you implement
IDisposableand put the disconnect on it yes! because it is usingusing;– user46523
Really now I touched myself, I didn’t say it was something silly
– Filipe
put in the answer!
– user46523