This is an old revision of the document!


QuickBooks Integration with C# .NET

C# Sample Code for QuickBooks Desktop Editions

// Use this if you're just using the regular SDK stuff
//using QBXMLRP2Lib;
//using Interop.QBXMLRP2;

// Use this if you're using QBXMLRP2E.exe
using QBXMLRP2ELib;

RequestProcessor2 rp = null;

string ticket = null;
string response = null;

try
{
	rp = new RequestProcessor2();
	rp.OpenConnection("", "IDN CustomerAdd C# sample");
	ticket = rp.BeginSession("C:\\path\\to\\file.QBW", QBFileModeE.qbFileOpenDoNotCare);
	//ticket = rp.BeginSession("C:\\path\\to\\file.QBW", QBFileMode.qbFileOpenDoNotCare);

	Random random = new Random();
	
	string input = @"<?xml version=""1.0"" encoding=""utf-8""?>
	<?qbxml version=""2.0""?>
	<QBXML>
	<QBXMLMsgsRq onError=""stopOnError"">
	<CustomerAddRq requestID=""15"">
	<CustomerAdd>
	...
	</CustomerAdd>
	</CustomerAddRq>
	</QBXMLMsgsRq>
	</QBXML>";
	
	response = rp.ProcessRequest(ticket, input);
}

catch (System.Runtime.InteropServices.COMException ex)
{
	return;
}
finally
{
	if (ticket != null)
	{
		rp.EndSession(ticket);
	}

	if (rp != null)
	{
		rp.CloseConnection();
	}
};

C# .NET Sample Code for QuickBooks Online Edition

string requestUrl = null;
requestUrl = "https://apps.quickbooks.com/j/AppGateway";


HttpWebRequest WebRequestObject = null;
StreamReader sr = null;
HttpWebResponse WebResponseObject = null;
StreamWriter swr = null;


try
{
    WebRequestObject = (HttpWebRequest)WebRequest.Create(requestUrl);
    WebRequestObject.Method = "POST";
    WebRequestObject.ContentType = "application/x-qbxml";
    WebRequestObject.AllowAutoRedirect = false;

string post = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<?qbxml version=""6.0""?>
<QBXML>
<SignonMsgsRq>
<SignonDesktopRq>
<ClientDateTime>%%CLIENT_DATE_TIME%%</ClientDateTime>
<ApplicationLogin>APPLICATION_LOGIN</ApplicationLogin>
<ConnectionTicket>CONNECTION_TICKET</ConnectionTicket>
<Language>English</Language>
<AppID>APP_ID</AppID>
<AppVer>1</AppVer>
</SignonDesktopRq>
</SignonMsgsRq>
<QBXMLMsgsRq onError=""continueOnError"">
<CustomerQueryRq requestID=""2"" />
</QBXMLMsgsRq>
</QBXML>"; 

post = post.Replace("%%CLIENT_DATE_TIME%%", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"));
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(post);
    post = xmlDoc.InnerXml;
    WebRequestObject.ContentLength = post.Length;
    swr = new StreamWriter(WebRequestObject.GetRequestStream());
    swr.Write(post);
    swr.Close();
    WebResponseObject = (HttpWebResponse)WebRequestObject.GetResponse();
    sr = new StreamReader(WebResponseObject.GetResponseStream());
    string Results = sr.ReadToEnd();
    }
finally
    {
        try
        {
            sr.Close();
        }
        catch
        {
        }


    try
    {
        WebResponseObject.Close();
        WebRequestObject.Abort();
    }
    catch
    {
    }
}

resume writing services

quickbooks_integration_csharp.1309426226.txt.gz · Last modified: 2013/01/21 12:44 (external edit)