I’m facing a problem with Datareader, in the C#programming language, Visual Studio development environment

Asked

Viewed 28 times

-1

Hello, all right?

I am facing problems with Data Reader, what happens -> I have several forms/Forms, and when trying to enter one of them, in this case, the Sets form, the message appears "There is already a Datareader associated with this Command, please close it first!".

Try using . Dispose() in the connection instance of the bank to see if it solved, but nothing!

I also tried to use the Destructor of the class to see if it solved, but I didn’t get so successful!

My friend suggested using(<instantiation>){}, but it turns out I don’t know how to use, I even researched some sites about, but are not specific!

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;


namespace ProjetoCompositor
{
    class ClassConexao
    {

        private static string conexao;
        public static SqlConnection connection;
        
        //Construtor
        public ClassConexao()
        {
            conexao = ProjetoCompositor.Properties.Settings.Default.strConexao;
            connection = new SqlConnection(conexao);
        }

        //Destrutor
        ~ClassConexao()
        {
            Console.WriteLine("Liberando recursos....");
        }

        public void Aberto()
        {
            try
            {
                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }
            }
            catch(Exception mensagem)
            {
                Console.Write(mensagem.Message);
            }
        }

        public void Fechar()
        {
            try
            {
                if (connection.State == System.Data.ConnectionState.Open)
                {
                    connection.Dispose();
                    connection.Close();
                }
            }
            catch(Exception mensagem)
            {
                Console.WriteLine(mensagem.Message);
            }
            finally
            {
                connection.Dispose();
            }
        }
        
    }

    
}

The code is like this currently!

1 answer

0


public void Aberto()
    {
        try
        { // esta lina tem que ser close, vai abrir o que já esta aberto? :)
            if (connection.State != System.Data.ConnectionState.open) // CLOSE

            {
                connection.Open();
            }
        }
        catch(Exception mensagem)
        {
            Console.Write(mensagem.Message);
        }
    }

if (if) this Close (then) Open...

Browser other questions tagged

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