问题描述
项目成功执行,但是当我单击项目的购买或销售选项时,项目突然崩溃并发生错误。我想将通过保存按钮输入的所有横断面保存到数据库中。 我是C#的新手,请帮助
transectionsDAL tdal = new transectionsDAL();
transectionDetailDAL tdDAL = new transectionDetailDAL();
using(TransactionScope scope=new TransactionScope())
{
int transactionID = -1;
bool w = tdal.Insert_Transection(transection,out transactionID);
for(int i = 0; i < transectiondt.Rows.Count; i++)
{
transectionDetailBLL transactiondetail = new transectionDetailBLL();
string productname = transectiondt.Rows[i][0].ToString();
productsBLL p = pdal.GetproductIDFromName(productname);
transactiondetail.product_id = p.id;
transactiondetail.rate = decimal.Parse(transectiondt.Rows[i][1].ToString());
transactiondetail.qty = decimal.Parse(transectiondt.Rows[i][2].ToString());
transactiondetail.total = Math.Round(decimal.Parse(transectiondt.Rows[i][3].ToString()),2);
transactiondetail.dea_cust_id = dc.id;
transactiondetail.added_date = DateTime.Now;
transactiondetail.dea_cust_id = u.id;
bool y = tdDAL.InsertTransectionDetail(transactiondetail);
success = w && y;
解决方法
首先请检查文档:https://docs.microsoft.com/en-us/dotnet/api/system.typeinitializationexception?view=netcore-3.1
作为包装初始化类引发的异常引发的异常。
这意味着这是交易之一
transectionsDAL tdal = new transectionsDAL();
transectionDetailDAL tdDAL = new transectionDetailDAL();
或者是bll
transectionDetailBLL transactiondetail = new transectionDetailBLL();
它也可能在您的函数之一之内
要解决真正的问题,您需要检查InnerException
字段中的真实原因。
当类初始化程序无法初始化类型时,将创建TypeInitializationException并将其引用传递给该类型的类初始化程序抛出的异常。 TypeInitializationException的InnerException属性包含基础异常。
我建议您获得真正的例外是使用
try {
/// your code here
}
catch (TypeInitializationException e) {
throw e.InnerException;
}
更好的是,逐行调试代码以查看问题所在。