簡體   English   中英

C#Microsoft Dynamics GP將新員工添加到taCreateEmployee

[英]C# Microsoft Dynamics GP add new employee to taCreateEmployee

如何從taCreateEmployee獲取下一個可用的員工ID?

我構建了一個小的Windows窗體程序,該程序使用eConnect工具將新員工添加到Microsoft Dynamics Great Plains數據庫。 我能夠成功構建xml文檔並將其發送到服務器,但是卻收到錯誤消息:


'taCreateEmployee'期望未提供參數'@I_vEMPLOYID'。

因此,我想我需要在插入記錄之前獲取員工ID,但是如何獲取員工ID? 這是xml。

 <?xml version="1.0" encoding="utf-8"?> <eConnect xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <UPRCreateEmployeeType><eConnectProcessInfo xsi:nil="true" /> <taRequesterTrxDisabler_Items xsi:nil="true" /> <taCreateEmployee> <EMPLOYID /> //HAVE ALSO TRIED <EMPLOYID></EMPLOYID> AND <EMPLOYID> </EMPLOYID> with a space <EMPLCLAS>SEASONAL</EMPLCLAS> <LASTNAME>Lastname</LASTNAME> <FRSTNAME>Firstname</FRSTNAME> <ADRSCODE>PRIMARY</ADRSCODE> <ADDRESS1>111 Main St</ADDRESS1> <CITY>City</CITY> <STATE>State</STATE> <ZIPCODE>66000</ZIPCODE> <PHONE1>5730000000</PHONE1> <SOCSCNUM>00000000</SOCSCNUM> <BRTHDATE>1930-10-30 0:00:00.000</BRTHDATE> <LOCATNID>PITS</LOCATNID> <SUTASTAT>HI</SUTASTAT> <BIRTHDAY>129</BIRTHDAY> <BIRTHMONTH>10</BIRTHMONTH> </taCreateEmployee> <taCreateInternetAddresses_Items xsi:nil="true"/> </UPRCreateEmployeeType> </eConnect> 

這是我構建xml對象的代碼:

  private void SerializeObject(AllASEmlpoyees employeeList) { try { eConnectType econnect = new eConnectType(); UPRCreateEmployeeType[] value = new UPRCreateEmployeeType[employeeList.Candidates.Count()]; var count = 0; foreach (var item in employeeList.Candidates) { UPRCreateEmployeeType employee = new UPRCreateEmployeeType(); //employee record taCreateEmployee employeerecord = new taCreateEmployee(); //Console.WriteLine("First Name: " + item.FirstName + " Last Name " + item.LastName); var _with1 = employeerecord; _with1.EMPLOYID = ""; _with1.EMPLCLAS = item.GPEmloyeeClass; _with1.INACTIVE = 0; _with1.FRSTNAME = item.FirstName; _with1.LASTNAME = item.LastName; //_with1.MIDLNAME = ""; _with1.ADRSCODE = item.GPAddressCode; _with1.ADDRESS1 = item.Address1; _with1.ADDRESS2 = item.Address2; _with1.CITY = item.City; _with1.STATE = item.GPStateFullName; _with1.ZIPCODE = item.Zip; _with1.PHONE1 = item.Phone; _with1.SOCSCNUM = item.SSNum; _with1.BIRTHDAY = (short)item.GPBirthday.Day; _with1.BIRTHMONTH = (short)item.GPBirthday.Month; _with1.BRTHDATE = item.GPBirthdayAsString; _with1.LOCATNID = item.GPLocationId; _with1.SUTASTAT = item.GPStateAbbreviation; _with1.EMPLOYMENTTYPE = item.GPEmployeementType; _with1.UpdateIfExists = 1; employee.taCreateEmployee = employeerecord; value[count] = employee; //add array to xml table count++; } econnect.UPRCreateEmployeeType = value; FileStream fs = new FileStream(directory + @"\\" + file, FileMode.Create); XmlTextWriter writer = new XmlTextWriter(fs, new UTF8Encoding()); XmlSerializer serializer = new XmlSerializer(typeof(eConnectType)); serializer.Serialize(writer, econnect); writer.Close(); } catch (ApplicationException ex) { Console.WriteLine("Exception: " + ex.ToString()); } } 

這是我使用econnect工具撥打電話的地方:

  public void eConnectSend(AllASEmlpoyees employeeList) { //Serialized XML File string xmldocument = null; //Connection String string connectString = null; //Result string xmlobject = null; using (eConnectMethods eConCall = new eConnectMethods()) { try { SerializeObject(employeeList); System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument(); xmldoc.Load(directory + @"\\" + file); xmldocument = xmldoc.OuterXml; connectString = "thisismyconnectionstringwhichisworking"; //send data to Great Plains xmlobject = eConCall.CreateTransactionEntity(connectString, xmldocument); Console.WriteLine("Object returned: " + xmlobject.ToString()); } catch (eConnectException exp) { Console.WriteLine("Exception: " + exp.ToString()); //Interaction.MsgBox(exp.ToString); } catch (System.Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); //Interaction.MsgBox(ex.ToString()); } finally { //eConCall.Dispose(); Console.WriteLine("Done"); Console.ReadLine(); this.cleanUpObjectsOnComplete(); } } } 

任何幫助我都會很高興!

我不知道這是否可以解決,但這就是我解決問題的方式。 我向數據庫添加了三個存儲過程。

存儲過程添加:

  1. 使用選擇查詢檢查員工是否存在,如果存在則返回ID
  2. 查詢UPR40201表以獲取下一個可用的id
  3. 成功添加記錄后更新UPR40201表

第一個接受ss#作為輸入參數。 使用傳遞的參數,我在表上運行查詢以查找已經存在的員工。 如果是這樣,那么我返回ID並在taCreateEmployee上調用UpdateIfExits。

ALTER PROCEDURE [dbo].[MY_Emloyee_GetEmployeeIdBySocialSecurityNumber]
-- Add the parameters for the stored procedure here
@ssnum AS varchar(255),
@EmployeeID As varchar(10) OUTPUT

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;


    BEGIN TRY
        -- Insert statements for procedure here
        SELECT @EmployeeID=taCreateEmployee.EMPLOYID FROM taCreateEmployee WHERE     taCreateEmployee.SOCSNUM = @ssnum
        return 1
    END TRY
    BEGIN CATCH
        DECLARE
        @ErrorSeverity INT,
        @ErrorNumber   INT,
        @ErrorMessage  NVARCHAR(4000),
        @ErrorState    INT
        SET @ErrorSeverity = ERROR_SEVERITY()
        SET @ErrorNumber = ERROR_NUMBER()
        SET @ErrorMessage = ERROR_MESSAGE()
        SET @ErrorState = ERROR_STATE()
        IF @ErrorState = 0
        SET @ErrorState = 1
        RAISERROR ('ERROR OCCURED:%d',
                    @ErrorSeverity,
                    @ErrorState,
                    @ErrorNumber)
        IF XACT_STATE() < 0
        RETURN @ErrorState
    END CATCH
END
END


如果不存在,則調用第二個存儲過程,該過程從表UPR40201的NEXTEMPID列中獲取下一個可用的雇員ID。 該表僅包含一個值,也就是下一個可用的ID。

ALTER PROCEDURE [dbo].[MY_Employee_GetNewEmployeID]
-- Add the parameters for the stored procedure here
@EmployeeID AS varchar(10) OUTPUT

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


BEGIN TRY
    -- Insert statements for procedure here
    SELECT @EmployeeID=NEXTEMPID FROM UPR40201
    RETURN 1
END TRY
BEGIN CATCH
    DECLARE
    @ErrorSeverity INT,
    @ErrorNumber   INT,
    @ErrorMessage  NVARCHAR(4000),
    @ErrorState    INT
    SET @ErrorSeverity = ERROR_SEVERITY()
    SET @ErrorNumber = ERROR_NUMBER()
    SET @ErrorMessage = ERROR_MESSAGE()
    SET @ErrorState = ERROR_STATE()
    IF @ErrorState = 0
    SET @ErrorState = 1
    RAISERROR ('ERROR OCCURED:%d',
                @ErrorSeverity,
                @ErrorState,
                @ErrorNumber)
    IF XACT_STATE() < 0
    RETURN @ErrorState
END CATCH
END


有了有效的員工ID后,我將使用eConnect工具更新記錄。 成功后,我通過調用最終存儲過程來更新表UPR40201列NEXTEMPID中的值,並增加ID。

ALTER PROCEDURE [dbo].[MY_Employee_UpdateNextEmployeeId]
-- Add the parameters for the stored procedure here
@NEXTEMPID AS varchar(40)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--BEGIN COMMANDS
BEGIN TRY
BEGIN TRANSACTION
    -- Insert statements for procedure here
    DELETE FROM UPR40201 WHERE NEXTEMPID != '0';
    INSERT INTO UPR40201 (NEXTEMPID) VALUES (@NEXTEMPID);
    COMMIT TRANSACTION
RETURN 1
END TRY
BEGIN CATCH
  DECLARE
    @ErrorSeverity INT,
    @ErrorNumber   INT,
    @ErrorMessage  NVARCHAR(4000),
    @ErrorState    INT
  SET @ErrorSeverity = ERROR_SEVERITY()
  SET @ErrorNumber = ERROR_NUMBER()
  SET @ErrorMessage = ERROR_MESSAGE()
  SET @ErrorState = ERROR_STATE()
  IF @ErrorState = 0
    SET @ErrorState = 1
  RAISERROR ('ERROR OCCURED:%d',
             @ErrorSeverity,
             @ErrorState,
             @ErrorNumber)
  IF XACT_STATE() < 0
    ROLLBACK TRANSACTION
    RETURN @ErrorState
END CATCH
END

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM