-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!