我有以下代码:
public void addCustomer()
{
bool sessionBegun = false;
bool connectionOpen = false;
QBSessionManager sessionManager = null;
try
{
//Create sessions manager object
sessionManager = new QBSessionManager();
//Create the message set request object to hold our request
IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 8, 0);
requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;
//Connect to quickbooks and begin a session
sessionManager.OpenConnection("", "CSW QB Interface");
connectionOpen = true;
sessionManager.BeginSession(@"C:\Users\Public\Documents\Intuit\QuickBooks\Company Files\Super Legit Industries.qbw", ENOpenMode.omDontCare);
sessionBegun = true;
ICustomerAdd customerAddRq = requestMsgSet.AppendCustomerAddRq();
customerAddRq.Name.SetValue("Test Customer 1");
//Send the request and get the response from quickbooks
IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet);
IResponse response = responseMsgSet.ResponseList.GetAt(0);
ICustomerRet customerRet = (ICustomerRet)response.Detail;
//Console.WriteLine(response.Detail.ToString());
custID = customerRet.ListID.GetValue();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//End the session and close the connection to quickbooks
if(sessionBegun)
{
sessionManager.EndSession();
}
if(connectionOpen)
{
sessionManager.CloseConnection();
}
}
}
我的问题是,可能存在两个人的名字相同并且需要输入快速手册的情况,但是如果我多次运行此代码(尝试多次添加“测试客户 1”),第二次我在线上抛出一个错误:
custID = customerRet.ListID.GetValue();
说:
Object reference not set to an instance of an object
有没有办法使用 QBFC 将重复的客户添加到 QB 中而不会引发异常?