I have the following code:
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();
}
}
}
My issue is that there could be a case where two people have the same name and need to be entered into quickbooks, but if I run this code more than once (trying to add "Test Customer 1" more than once), the second time I throws an error on the line:
custID = customerRet.ListID.GetValue();
Saying:
Object reference not set to an instance of an object
Is there any way to add a duplicate customer into QB using QBFC without it throwing an exception?