none
System.TypeInitializationException was unhandled by user code ERROR please help RRS feed

  • Question

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

    I got this error

    System.TypeInitializationException was unhandled by user code

      Message="The type initializer for 'ShopConfiguration' threw 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 dont see what is wrong? Here is the code if anyone can please assist it would 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 details into the DataList
        protected void Page_Load(object sender, EventArgs e)
        {
            // don't 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();
                
            }
        }
    }

     

    Friday, October 30, 2009 7:27 AM

Answers

  • Hi,
    Could you please debug into the type constructor of ShopConfiguration and its property DbProviderName , to see which line generates the exception, also take care whether dbProviderName is valid.

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Monday, November 2, 2009 8:58 AM

All replies

  • While calling the CreateCommand Method, is your ShopConfiguration object initialized?

    I think thats why this error.
    Ganesh Ranganathan
    [Please mark the post as answer if it answers your question]
    blog.ganeshzone.net
    Friday, October 30, 2009 7:55 AM
  • Yes I do in the ShopConfiguration class


    {
        //Caches the connection string
        private readonly static string 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;
            }
        }


      
    and that gets called here


       
            //Obtain the database connection string
            string connectionString = ShopConfiguration.DbConnectionString;
           
    In the Generic Data Access class
    Friday, October 30, 2009 8:27 AM
  • Hi,
    Could you please debug into the type constructor of ShopConfiguration and its property DbProviderName , to see which line generates the exception, also take care whether dbProviderName is valid.

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Monday, November 2, 2009 8:58 AM
  • Hi,
    Did you get the issue resolved, or still concern anything?

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Monday, November 9, 2009 8:59 AM
  • Most likely you get an exception here:
            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"];

    so your static class constructor fails and it says that can't initialize the type.


    Tech Lead at software development company
    Monday, November 9, 2009 6:48 PM
  • Thanks eryang found the error
    Tuesday, November 17, 2009 9:39 AM
  • Hi,
    Glad to see that you got it resolved, and it will be very beneficial for other community members having the similar questions if  you mark useful replies as answers.

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Wednesday, November 18, 2009 2:51 AM
  • The Compilation could not find any resources appropriate for the specified culture or the neutral culture.  Make sure your forms resources are correctly embedded or linked into The assembly at compile time, or that all the satellite assemblies required are loadable and fully signed.

    I hade the same problem and I found that the problem is on my icon :@

    Monday, December 23, 2013 9:18 PM