I am trying to understand why it does not 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 invokes the ExecuteConsulta method is internal and Static methods as I put 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 that shows me is:
An exception occurred in the type initializer of 'co.InfraestructuraData.Conexion'
I have well-referenced dll to mysql, the query is fine (I tested)
but when debug return Conexion.EjecutarConsulta(ConsultaEncabezado, CommandType.Text);
in this line, the exception occurs in the type initializer.
Could you explain to me why you do not enter the class call Connection run 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
As much as possible explain with code Thank you! ;);)
Update
this is the code of the internal Conexion 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];
}
}