这是我见过的最奇怪的事情,但希望别人有,因为我一无所知。我有以下代码:
DataTable dt = (DataTable)dataGridView1.DataSource;
List<InvoiceItem> itemList = new List<InvoiceItem>();
int listSize = 30;
int listIndex = 0;
try
{
itemList = (from DataRow dr in dt.Rows
select new InvoiceItem()
{
CustomerRef = dr["CustomerRef"].ToString(),
Description = dr["Description"].ToString(),
ItemRef = dr["ItemRef"].ToString(),
Rate = Convert.ToDouble(dr["Rate"].ToString()),
Quantity = Convert.ToDouble(dr["Quantity"].ToString()),
PONumber = dr["PONumber"].ToString(),
UnitOfMeasure = dr["UnitOfMeasure"].ToString(),
RefNumber = dr["RefNumber"].ToString(),
Total = Convert.ToDouble(dr["Total"].ToString()),
Address1 = dr["Address1"].ToString(),
Address2 = dr["Address2"].ToString(),
Address3 = dr["Address3"].ToString(),
Address4 = dr["Address4"].ToString(),
City = dr["City"].ToString(),
State = dr["State"].ToString(),
PostalCode = dr["PostalCode"].ToString(),
ServiceDate = string.IsNullOrEmpty(dr["ServiceDate"].ToString()) ? (DateTime?)null : DateTime.Parse(dr["ServiceDate"].ToString()),
TxnDate = string.IsNullOrEmpty(dr["TxnDate"].ToString()) ? DateTime.Now : DateTime.Parse(dr["TxnDate"].ToString()),
Note = dr["Note"].ToString()
}).ToList();
List<string> list = new List<string>();
list = loadItems();
List<InvoiceItem> createNewItemsList = new List<InvoiceItem>();
foreach (var importing in itemList)
{
var matchingvalues = list.Where(l => l.Contains(importing.ItemRef));
//If there is no match in Quickbooks already...
if (matchingvalues.Count() < 1)
{
createNewItemsList.Add(new InvoiceItem
{
ItemRef = importing.ItemRef,
UnitOfMeasure = importing.UnitOfMeasure
});
}
}
以下是loadItems()的代码:
private List<string> loadItems()
{
string request = "ItemQueryRq";
connectToQB();
int count = getCount(request);
IMsgSetResponse responseMsgSet = sessionManager.processRequestFromQB(BuildItemQuery());
string[] itemList = parseItemQueryRs(responseMsgSet, count);
disconnectFromQB();
List<string> list = new List<string>(itemList);
return list;
}
以下是错误视图:
这里显示了列表计数:
当我在桌面上运行此代码时,如果 matchingvalues.Count()= 0 ,它会正确执行代码。但是,当我在服务器上的调试中运行完全相同的代码时,该行代码错误输出&#34;对象引用未设置为对象的实例。&#34;任何人都可以解释为什么会发生这种情况以及是否有任何解决办法?