I'm trying to understand why it doesn't execute a method from a non-static public class ( public class DatosHandler
), even a static method:
public class DatosHandler
{
public static DataTable ConsultaFacturasAll()
{
//*** cadena de texto de consulta de las facturas detalle***
string ConsultaEncabezado = "SELECT " +
"facturaNumero, " +
"DATE_FORMAT(facturaFecha, '%Y-%m-%d') as FacturaFecha, " +
"DATE_FORMAT(facturaFechaVencimiento, '%Y-%m-%d') as FacturaFechaVencimiento, " +
"facturaOrdenCompra, " +
"clienteNombre, " +
"clienteIdentificacion, " +
"clienteTelefono, " +
"clienteEmail, " +
"FORMAT(facturaSubtotal, 2) as facturaSubtotal, " +
"FORMAT(facturaValorTotal, 2) as facturaValorTotal FROM Encabezados";
return Conexion.EjecutarConsulta(ConsultaEncabezado, CommandType.Text); // ACA SALE EL ERROR
}
The Class that is invoked by the ExecuteQuery method is internal and Static methods as I place it in the box:
public static DataTable EjecutarConsulta(string cadenaComando, CommandType tipocomando)
{
MySqlDataAdapter adaptador = new MySqlDataAdapter();// aca he colocado punto de interrupcion pero no llega el debuger en este punto
adaptador.SelectCommand = new MySqlCommand(cadenaComando, conexion);
adaptador.SelectCommand.CommandType = tipocomando;
DataSet resultado = new DataSet();
adaptador.Fill(resultado);
return resultado.Tables[0];
}
The error it shows me is:
An exception occurred in the initializer of type 'co.InfrastructureData.Connection'
I have well referenced the dll to mysql, the query is fine (I did test)
but debugging return Conexion.EjecutarConsulta(ConsultaEncabezado, CommandType.Text);
on this line throws the exception in the type initializer.
Could you explain to me why it does not enter the call of the Connection class execute query??? It is defined as static
!
public static DataTable EjecutarConsulta
Both the class Conexión
and DatosHandler
are in the same project or namespace namespace co.InfraestructuraData
If possible explain with code Thanks! ;) ;)
Update
this is the code of the internal Connection class, and they are in the same namespace namespace co.InfraestructuraData
internal class Conexion
{
private static string cadenaConexion= ConfigurationManager.ConnectionStrings["sqlConexionApp"].ConnectionString;
private static MySqlConnection conexion = new MySqlConnection(cadenaConexion);
public static bool Conectar()
{
try
{
conexion.Open();
return true;
}
catch (Exception)
{
return false;
}
}
public static void Desconectar()
{
conexion.Close();
}
public static void EjecutarOperacion(string cadenaComando, List<MySqlParameter> listaparametros, CommandType tipoComando)
{
if (Conectar())
{
MySqlCommand comando = new MySqlCommand();
comando.CommandText = cadenaComando;
comando.CommandType = tipoComando;
comando.Connection = conexion;
foreach (MySqlParameter parametro in listaparametros)
{
comando.Parameters.Add(parametro);
}
comando.ExecuteNonQuery();
Desconectar();
}
else
{
throw new Exception("No se pudo establecer conexion");
}
}
public static DataTable EjecutarConsulta(string sentencia, List<MySqlParameter> listaparametro, CommandType tipocomando)
{
MySqlDataAdapter adaptador = new MySqlDataAdapter();
adaptador.SelectCommand = new MySqlCommand(sentencia, conexion);
adaptador.SelectCommand.CommandType = tipocomando;
foreach (MySqlParameter parametro in listaparametro)
{
adaptador.SelectCommand.Parameters.Add(parametro);
}
DataSet resultado = new DataSet();
adaptador.Fill(resultado);
return resultado.Tables[0];
}
public static DataTable EjecutarConsulta(string cadenaComando, CommandType tipocomando)
{
MySqlDataAdapter adaptador = new MySqlDataAdapter();
adaptador.SelectCommand = new MySqlCommand(cadenaComando, conexion);
adaptador.SelectCommand.CommandType = tipocomando;
DataSet resultado = new DataSet();
adaptador.Fill(resultado);
return resultado.Tables[0];
}
}
When calling a static method of a class, keep in mind that all static variables of the class are initialized.
In your case, you have two static variables in
Conexion
:cadenaConexion
andconexion
.The error you describe is being thrown by the initialization of one of those variables. Either the key
sqlConexionApp
does not exist in your configuration file (web.config
orapp.config
), or the connection string is not correct and the error is occurring inconexion = new MySqlConnection(cadenaConexion);
You should check both, and also in these cases the
InnerException
should usually give you more information about what the problem is.You must ensure that the version of your library is the same as the mysql database you use, the versions are 5.1 and 5.2, etc; Even if you already have your library installed, you should check that they are the same version.
I show you part of the connection class: to use the method just create an instance of the class
You can add the methods you want something like:
I leave you the code: