Hello everyone:
I've working in XBAP application (Code Behind in C#).
When i want to call a store procedure, Visual Studio gives me this kind of error:TypeInitializationException, and the problem, i think, is in this static class, called Global (Global.cs).
This is the code where i call the store procedure:
public DataTable ValidarUsuario(string NombreUsuario, string PasswordUsuario) { try { DataTable TablaResultado = new DataTable(); MessageBox.Show("Estoy acá ... "); var Comando = new SqlCommand("ValidarUsuario", Global.Global_Conectar_BaseMaestra); MessageBox.Show("Ya me conecté a la Base Maestra ... "); Comando.CommandType = System.Data.CommandType.StoredProcedure; Comando.Parameters.AddWithValue("@NombreUsuario", NombreUsuario); Comando.Parameters.AddWithValue("@PasswordUsuario", PasswordUsuario); SqlDataAdapter RegistrosObtenidos = new SqlDataAdapter(Comando); RegistrosObtenidos.Fill(TablaResultado); MessageBox.Show("Ya hice la Consulta ... "); Global.CerrarConexion_BaseMaestra(); return TablaResultado; } catch (SystemException Excepcion) { MessageBox.Show("No ha sido posible validar los datos ingresados (Nombre de Usuario y Password de Usuario): " + Excepcion.Message); Global.CerrarConexion_BaseMaestra(); return null; } }
As you can see; i put MessageBoxs to find the line where the problem is. The application shows just the first MessageBox. So, the problem starts here:
var Comando = new SqlCommand("ValidarUsuario", Global.Global_Conectar_BaseMaestra);
This is a part of the Global class, where there are the methods to connect with the database:
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Windows; namespace WpfBrowserApplication1 { static class Global { private static string RutaBaseDeDatosMaestra = "Data Source=SERGIO\\SQLEXPRESS;Initial Catalog=a000_sysgesNC;Integrated Security=True"; public static SqlConnection Conectar_BaseMaestra = null; // Tiene su método GET y SET más abajo. public static SqlConnection Global_Conectar_BaseMaestra { get { MessageBox.Show("Conectándose a la Base Maestra ... "); if (AbrirConexion_BaseMaestra()) { MessageBox.Show("Conexión lista ... "); return Conectar_BaseMaestra; } else { MessageBox.Show("No me he podido conectar ... falló algo ... "); return null; } } } private static bool AbrirConexion_BaseMaestra() { try { MessageBox.Show("Ruta Base Maestra: " + RutaBaseDeDatosMaestra); Conectar_BaseMaestra = new SqlConnection(); Conectar_BaseMaestra.ConnectionString = RutaBaseDeDatosMaestra; Conectar_BaseMaestra.Open(); return true; } catch (SystemException Excepcion) { MessageBox.Show("Ha habido un error en la conexión a la base de datos maestra del Sistema: " + Excepcion.Message); return false; } } public static bool CerrarConexion_BaseMaestra() { try { Conectar_BaseMaestra.Close(); return true; } catch (SystemException Excepcion) { MessageBox.Show("Ha habido un error en el ciere de la conexión a la base de datos maestra del Sistema: " + Excepcion.Message); return false; } }
That is.
Thank you for your time and thank you for your replies.