开发者

System.TypeInitializationException was unhandled by 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 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
  开发者_如何学Python  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();

    }
}

}

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;
    }
}


I am quite sure that the TypeInitializationException that is thrown has another exception assigned to its InnerException property. If you examine that exception, I think you will find the real cause of your problem.


Sounds like you have specified an invalid setting for DbProviderName so internal checking code reports this exception. You'd better review the connection string settings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜