Please go through the below code you will get the answer...
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;
using Microsoft.Dynamics.GP.eConnect;
using Microsoft.Dynamics.GP.eConnect.Serialization;
namespace eConnectSalesOrder_CSharp_ConsoleApplication
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class test
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using (eConnectMethods eConCall = new eConnectMethods())
{
try
{
// Create the eConnect document and store it in a file
SerializeSalesOrderObject("SalesOrder.xml");
// Load the eConnect XML document from the file into
// and XML document object
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("SalesOrder.xml");
// Create an XML string from the document object
string salesOrderDocument = xmldoc.OuterXml;
// Create a connection string
// Integrated Security is required. Integrated security=SSPI
// Update the data source and initial catalog to use the names of
// data server and company database
string sConnectionString = "data source=localhost;initial catalog=TWO;integrated security=SSPI;persist security info=False;packet size=4096";
// Use the CreateTransactionEntity method to create the sales document in Microsoft Dynamics GP
// The method returns a string that contains the doc ID number of the new sales document
string salesOrder = eConCall.CreateTransactionEntity(sConnectionString, salesOrderDocument);
}
// The eConnectException class will catch any business logic related errors from eConnect_EntryPoint.
catch (eConnectException exp)
{
Console.Write(exp.ToString());
}
// Catch any system error that might occurr. Display the error to the user
catch (Exception ex)
{
Console.Write(ex.ToString());
}
finally
{
// Use the Dipose method to release resources associated with the
// eConnectMethods objects
eConCall.Dispose();
}
}
}
private static void SerializeSalesOrderObject(string filename)
{
// Create a datetime format object
DateTimeFormatInfo dateFormat = new CultureInfo("en-US").DateTimeFormat;
try
{
// Create an array that can hold two taSopLineIvcInsert_ItemsTaSopLineIvcInsert objects
taSopLineIvcInsert_ItemsTaSopLineIvcInsert[] LineItems = new taSopLineIvcInsert_ItemsTaSopLineIvcInsert[2];
taCreateSopPaymentInsertRecord_ItemsTaCreateSopPaymentInsertRecord[] inspym = new taCreateSopPaymentInsertRecord_ItemsTaCreateSopPaymentInsertRecord[1];
// taCreateSopPaymentInsertRecord_Items ins = new taCreateSopPaymentInsertRecord_Items();
GetSopNumber mySopNumber = new GetSopNumber();
// Create a taSopLineIvcInsert_ItemsTaSopLineIvcInsert object and populate its fields
taSopLineIvcInsert_ItemsTaSopLineIvcInsert salesLine = new taSopLineIvcInsert_ItemsTaSopLineIvcInsert();
taCreateSopPaymentInsertRecord_ItemsTaCreateSopPaymentInsertRecord invs = new taCreateSopPaymentInsertRecord_ItemsTaCreateSopPaymentInsertRecord();
salesLine.ADDRESS1 = "2345 Main St.";
salesLine.CUSTNMBR = "CONTOSOL0001";
salesLine.CITY = "Aurora";
salesLine.SOPTYPE = 3;
salesLine.DOCID = "STDINV";
salesLine.QUANTITY = 2;
salesLine.ITEMNMBR = "ACCS-CRD-12WH";
salesLine.ITEMDESC = "Phone Cord - 12' White";
salesLine.UNITPRCE = 10.95m;
salesLine.XTNDPRCE = 21.9m;
salesLine.LOCNCODE = "WAREHOUSE";
salesLine.DOCDATE = System.DateTime.Today.ToString("MM/dd/yyyy", dateFormat);
// Add the SOP line item object to the array
LineItems[0] = salesLine;
// Create a second taSopLineIvcInsert_ItemsTaSopLineIvcInsert object and populate its fields
taSopLineIvcInsert_ItemsTaSopLineIvcInsert salesLine2 = new taSopLineIvcInsert_ItemsTaSopLineIvcInsert();
salesLine2.ADDRESS1 = "2345 Main St.";
salesLine2.CUSTNMBR = "CONTOSOL0001";
salesLine2.CITY = "Aurora";
salesLine2.SOPTYPE = 3;
salesLine2.DOCID = "STDINV";
salesLine2.QUANTITY = 2;
salesLine2.ITEMNMBR = "ACCS-CRD-25BK";
salesLine2.ITEMDESC = "Phone Cord - 25' Black";
salesLine2.UNITPRCE = 15.95m;
salesLine2.XTNDPRCE = 31.9m;
salesLine2.LOCNCODE = "WAREHOUSE";
salesLine2.DOCDATE = System.DateTime.Today.ToString("MM/dd/yyyy", dateFormat);
// Add the SOP line item object to the array
LineItems[1] = salesLine2;
// Create a taSopHdrIvcInsert object and populate its fields
taSopHdrIvcInsert salesHdr = new taSopHdrIvcInsert();
string sConnectionString = "data source=localhost;initial catalog=TWO;integrated security=SSPI;persist security info=False;packet size=4096";
salesHdr.SOPTYPE = 3;
salesHdr.DOCID = "STDINV";
salesHdr.BACHNUMB = "eConnect";
salesHdr.TAXSCHID = "USASTCITY-6*";
salesHdr.FRTSCHID = "USASTCITY-6*";
salesHdr.MSCSCHID = "USASTCITY-6*";
salesHdr.LOCNCODE = "WAREHOUSE";
salesHdr.SOPNUMBE= mySopNumber.GetNextSopNumber(3, "STDINV", sConnectionString);
salesHdr.DOCDATE = System.DateTime.Today.ToString("MM/dd/yyyy", dateFormat);
salesHdr.CUSTNMBR = "CONTOSOL0001";
salesHdr.CUSTNAME = "Contoso, Ltd.";
salesHdr.ShipToName = "WAREHOUSE";
salesHdr.ADDRESS1 = "2345 Main St.";
salesHdr.CNTCPRSN = "Joe Healy";
salesHdr.FAXNUMBR = "13125550150";
salesHdr.CITY = "Aurora";
salesHdr.STATE = "IL";
salesHdr.ZIPCODE = "65700";
salesHdr.COUNTRY = "USA";
salesHdr.SUBTOTAL = 53.8m;
salesHdr.DOCAMNT = 53.8m;
salesHdr.PYMTRCVD = 53.8m;
salesHdr.USINGHEADERLEVELTAXES = 0;
salesHdr.PYMTRMID = "Net 30";
invs.Action = 1;
invs.SOPNUMBE = salesHdr.SOPNUMBE;
invs.SOPTYPE = 3;
invs.PYMTTYPE = 4;
//invs.o
invs.SOPNUMBE = salesHdr.SOPNUMBE;
invs.DOCAMNT = 53.8m;
invs.DOCDATE = System.DateTime.Today.ToString("MM/dd/yyyy", dateFormat);
invs.CUSTNAME = "Contoso, Ltd.";
invs.CUSTNMBR = "CONTOSOL0001";
inspym[0] = invs;
// Create a eConnect SOPTransactionType schema object
SOPTransactionType salesOrder = new SOPTransactionType();
// Populate the schema object with the SOP header and SOP line item objects
salesOrder.taSopLineIvcInsert_Items = LineItems;
salesOrder.taSopHdrIvcInsert = salesHdr;
salesOrder.taCreateSopPaymentInsertRecord_Items = inspym;
//salesOrder.taCreateSopPaymentInsertRecord_Items = inspym;
//salesOrder.taCreateSopPaymentInsertRecord_Items = inspym;
// Create an array that holds SOPTransactionType objects
// Populate the array with the SOPTransactionType schema object
SOPTransactionType[] MySopTransactionType = { salesOrder };
// Create an eConnect XML document object and populate it
// with the SOPTransactionType schema object
eConnectType eConnect = new eConnectType();
eConnect.SOPTransactionType = MySopTransactionType;
// Create a file to hold the serialized eConnect XML document
FileStream fs = new FileStream(filename, FileMode.Create);
XmlTextWriter writer = new XmlTextWriter(fs, new UTF8Encoding());
// Serialize the eConnect document object to the file using the XmlTextWriter.
XmlSerializer serializer = new XmlSerializer(eConnect.GetType());
serializer.Serialize(writer, eConnect);
writer.Close();
}
//If an eConnect exception occurs, notify the user
catch (eConnectException ex)
{
Console.Write(ex.ToString());
}
}
}
}