gpt4 book ai didi

c#-2.0 - 用户代码未处理 System.TypeInitializationException 错误请帮助

转载 作者:行者123 更新时间:2023-12-05 00:42:56 25 4
gpt4 key购买 nike

我正忙于使用 Visual Studio 2005 和 IIS 7 的电子商务 Web 应用程序

我收到了这个错误

System.TypeInitializationException 未被用户代码处理

Message="'ShopConfiguration' 的类型初始化程序引发了异常。"

来源="App_Code.r-ihwy-d"

类型名称="商店配置"

堆栈跟踪:

   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)

如果我查看它所指的代码,我看不出有什么问题?这是代码,如果有人可以提供帮助,那就太好了!

GenericDataAccess.cs:

公共(public)静态类 GenericDataAccess
{
//静态构造函数
静态通用数据访问()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
//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;
}

目录访问.cs

公共(public)静态类 CatalogAccess
{
静态目录访问()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
//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);


}

}

部门列表.ascx.cs

公共(public)部分类 UserControls_DepartmentsList : System.Web.UI.UserControl
{
//将部门详细信息加载到 DataList
protected void Page_Load(object sender, EventArgs e)
{
//在回发期间不重新加载数据
   {
// 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();

}
}

}

ShopConfiguration 类

{
//缓存连接字符串
私有(private)只读静态字符串 dbConnectionString;
//Caches the data provider name
private readonly

静态字符串 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;
}
}

最佳答案

我很确定 TypeInitializationException被抛出的还有另一个异常分配给它的InnerException。属性(property)。如果您检查该异常,我认为您会找到问题的真正原因。

关于c#-2.0 - 用户代码未处理 System.TypeInitializationException 错误请帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1648651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com