System.TypeInitializationException was raw user code ERROR, please help

I am busy with an e-commerce web application using visual studio 2005 and IIS 7

I got this error

System.TypeInitializationException failed to execute using user code

Message = "The type initializer for" ShopConfiguration "made an exception."

Source = "App_Code.r-ihwy-d"

TypeName = "ShopConfiguration"

Stacktrace:

at ShopConfiguration.get_DbProviderName() at GenericDataAccess.CreateCommand() in c:\inetpub\wwwroot\Beadafrican\App_Code\GenericDataAccess.cs:line 63 at CatalogAccess.GetDepartments() in c:\inetpub\wwwroot\Beadafrican\App_Code\CatalogAccess.cs:line 28 at UserControls_DepartmentsList.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\Beadafrican\UserControls\DepartmentsList.ascx.cs:line 22 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

If I look at the code it refers to, I don’t see what is wrong? Here is the code, if anyone can help, it will be great!

GenericDataAccess.cs:

public static class GenericDataAccess {// static constructor static GenericDataAccess () {// // TODO: add constructor logic here //}

 //execute a command and returns the result as a DataTable Object public static DataTable ExecuteSelectCommand(DbCommand command) { //The DataTable to be returned DataTable table; //Execute the command making sure the connection gets closed in the end try { //open the data connection command.Connection.Open(); //Execute the command and save the results in a DataTable DbDataReader reader = command.ExecuteReader(); table = new DataTable(); table.Load(reader); //Close the reader reader.Close(); } catch (Exception ex) { Utilities.LogError(ex); throw ex; } finally { //Close the connection command.Connection.Close(); } return table; } //creates and prepares a new DbCommand object on a new connection public static DbCommand CreateCommand() { //Obtain the database provider name string dataProviderName = ShopConfiguration.DbProviderName; //Obtain the database connection string string connectionString = ShopConfiguration.DbConnectionString; //Create a new data provider factory DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName); //Obtain a database specific connection object DbConnection conn = factory.CreateConnection(); //Set the connection string conn.ConnectionString = connectionString; //Create a database specific command object DbCommand comm = conn.CreateCommand(); //Set the command type to stored procedure comm.CommandType = CommandType.StoredProcedure; //Return the initialised command object return comm; } 

CatalogAccess.cs

public static class CatalogAccess {static CatalogAccess () {// // TODO: add constructor logic here //}

 //Retrieve the list of departments public static DataTable GetDepartments() { //get configured DbCommand object DbCommand comm = GenericDataAccess.CreateCommand(); //set the stored procedure name comm.CommandText = "GetDepartments"; //execute the stored procedure and return the results return GenericDataAccess.ExecuteSelectCommand(comm); } 

}

DepartementList.ascx.cs

public partial class UserControls_DepartmentsList: System.Web.UI.UserControl {// Load department data in DataList protected void Page_Load (object sender, EventArgs e) {// do not reload data during postbacks

  { // CatalogAccess.GetDepartments returns a DataTable object containing // department data, which is read in the ItemTemplate of the DataList list.DataSource = CatalogAccess.GetDepartments(); // Needed to bind the data bound controls to the data source list.DataBind(); } } 

}

class ShopConfiguration

{// Caches the connection string private static line readonly dbConnectionString;

 //Caches the data provider name private readonly 

static string dbProviderName;

 //stores the number of products per page private readonly static int productsPerPage; //Stores the product description length for product lits private readonly static int productDescriptionLenght; //Store the name of your shop private readonly static string siteName; //Initialize various proeprties in the constructor static ShopConfiguration() { dbConnectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString; dbProviderName = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ProviderName; productsPerPage = Int32.Parse(ConfigurationManager.AppSettings["ProductsPerPage"]); productDescriptionLenght = Int32.Parse(ConfigurationManager.AppSettings["ProductDescriptionLenght"]); siteName = ConfigurationManager.AppSettings["SiteName"]; } //Returns the connection string for BeadAfrican database public static string DbConnectionString { get { return dbConnectionString; } } //Returns the data provider name public static string DbProviderName { get { return dbProviderName; } } 
+4
source share
2 answers

I am sure that the generated TypeInitializationException has another exception assigned to its InnerException property. If you study this exception, I think you will find the real cause of your problem.

+6
source

It looks like you specified the wrong parameter for DbProviderName, so the internal verification code reports this exception. You better look at the connection string settings.

0
source

All Articles