Site Search:
Sign in | Join | Help

Problem converting eConnect VB sample to C#

Last post 06-15-2008 4:09 PM by alan. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 03-12-2008 4:06 PM

    Problem converting eConnect VB sample to C#

    Hello.  First, I wanted to say great site!

    I'm just getting started with eConnect and am trying to work through the examples in the eConnect Programmer's Guide and Reference.  Specifically, I am working on the eConnectOut Serialization Example.  I am able to get it working if I create a VB project, but cannot get it to work for C# (I only know C#).  I have been able to convert pretty much the entire code to C# except for the 'ReDim Preserve' statement.  Since there isn't an equivalent statement in C#, I have no idea how to go about converting it from VB.  I have googled it and worked through many suggestions on how to convert it without any luck.  It's probably something simple, but I cannot figure it out and was hoping someone here could shed a little insight.  Any help or information you could provide would be extremely helpful.  I will post the entire code below for your reference, but I will highlight the section I am having trouble with.  Thank you so much in advance!

    Imports System
    Imports System.Xml
    Imports System.Xml.Serialization
    Imports System.IO
    Imports System.EnterpriseServices
    Imports System.Text
    Imports Microsoft.Dynamics.GP.eConnect
    Imports Microsoft.Dynamics.GP.eConnect.Serialization
    Module Module1
        Sub Main()
            Dim serializer As New XmlSerializer(GetType(eConnectType))
            Dim eConnect As New eConnectType()
            Dim eConnectouttype As New RQeConnectOutType()
            Dim eConnectOut As New eConnectOut()
            Dim entrypoint As New eConnectMethods()
            Dim requesterDoc As String
            Dim sConnectionString As String
            Try
                'Populate the eConnectOut object to specify the data request
                With eConnectOut
                    .DOCTYPE = "Customer"
                    .OUTPUTTYPE = 1
                    .INDEX1FROM = "AARONFIT0001"
                    .INDEX1TO = "AARONFIT0001"
                    .FORLIST = 1
                End With
                'Create an XML document for the request
                eConnectouttype.eConnectOut = eConnectOut
                ReDim Preserve eConnect.RQeConnectOutType(0)
                eConnect.RQeConnectOutType(0) = eConnectouttype
                'Serialize the eConnect object to a memory stream
                Dim memStream As New MemoryStream()
                serializer.Serialize(memStream, eConnect)
                memStream.Position = 0
                'Use the memory stream to load an Xml document
                Dim xmlDoc As New XmlDocument()
                xmlDoc.Load(memStream)
                memStream.Close()
                'Create a connection string to Microsoft Dynamics GP
                sConnectionString = "data source=MyServer;" _
                    & "initial catalog=TWO; integrated security=SSPI;" _
                    & "persist security info=False; packet size=4096"
                'Request the data from the server
                requesterDoc = entrypoint.eConnect_Requester(sConnectionString, _
                    EnumTypes.ConnectionStringType.SqlClient, xmlDoc.OuterXml)
                'Write the customer XML to a file
                Dim fs As New FileStream("Customer.xml", FileMode.Create)
                Dim writer As New StreamWriter(fs, New UTF8Encoding)
                writer.Write(requesterDoc)
                writer.Close()
            Catch exp As eConnectException
                Debug.Write(exp.ToString)
            Catch ex As System.Exception
                Debug.Write(ex.Message & vbCrLf & ex.StackTrace)
            End Try
        End Sub
    End Module
    
    • 64.105.166.107
  • 03-13-2008 11:56 AM In reply to

    Re: Problem converting eConnect VB sample to C#

    The reason for the 'redim perserve' is to increase the size of the array of objects that you're building. In the end, that array gets added to another object and sent to eConnect

    How about just adding them directly to the eConnect object as you create them, and skipping the array altogether. I've never tried that... but it might work.

    Steve Gray, MCDBA
    Technical Editor
    steve@VSToolsForum.com
    • 71.100.137.9
  • 03-13-2008 4:12 PM In reply to

    Re: Problem converting eConnect VB sample to C#

    Thank you for your response.  Unfortunately, it doesn't seem to work when I add them directly to the eConnect object.  I get an error when I try to run it stating, "Object reference not set to an instance of an object".  Any other suggestions?

    • 64.105.166.107
  • 06-15-2008 4:09 PM In reply to

    Re: Problem converting eConnect VB sample to C#

    I don't think you need Preserve:

     

                eConnectType ect = new eConnectType();  

                XmlSerializer s = new XmlSerializer(ect.GetType());

                eConnectType econnect = new eConnectType();

                RQeConnectOutType eConnectouttype = new RQeConnectOutType();

                eConnectOut eConnectOut = new eConnectOut();

                eConnectMethods entrypoint = new eConnectMethods();

                string requesterDoc = String.Empty;

                string sconn = String.Empty;

     

     

                eConnectOut.DOCTYPE = "Customer";

                eConnectOut.OUTPUTTYPE = 1;

                eConnectOut.INDEX1FROM = "AARONFIT0001";

                eConnectOut.INDEX1TO = eConnectOut.INDEX1FROM;

                eConnectOut.FORLIST = 1; 

                eConnectouttype.eConnectOut = eConnectOut;

     

                econnect.RQeConnectOutType = new RQeConnectOutType[1] { eConnectouttype };

    [and continue with the example...]

    • 76.14.82.77
Page 1 of 1 (4 items)