Write and read uncommitted entry within transaction?

draken
draken Member Posts: 9
Hi,

The following simplified code is trying to read an uncommitted record, from Navision 4, using ADO:
CREATE(adoConn);  // Automation 'Microsoft ActiveX Data Objects 2.8 Library'.Connection
CREATE(adoRS);    // Automation 'Microsoft ActiveX Data Objects 2.8 Library'.Recordset

adoConn.IsolationLevel(256);  // Set Uncommitted Read

adoConn.Open(_strConnString);

adoConn.BeginTrans;

adoConn.Execute('INSERT INTO Item (Name) VALUES ('Item Name')', intRecAffected);  // intRecAffected returns 1 if the records was inserted

// ... and now for the troubled part...
adoRS := adoConn.Execute('SELECT [ID] FROM Item WHERE Name = 'Item Name'');

adoRS.MoveFirst;
intCount := adoRS.RecordCount;  // Returns -1

adoConn.RollbackTrans;

// ... Close and Clean up


The item table I'm dealing with is an external application which synchronises with Navision items. When an item is inserted in this application, an identity column provides the primary key, which I need to capture in Navision. Both Navision and the external app have SQL 2000 backends.

Am I missing something or can I not do this?

draken
nosce te ipsum

Answers

  • jsnayberk
    jsnayberk Member Posts: 58
    Message deleted. Sorry.
    --
    Josef Snayberk
  • kine
    kine Member Posts: 12,562
    And what is the problem?
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • draken
    draken Member Posts: 9
    The problem is that I can not retrieve the record I inserted.
    Put another way, in query analyzer I can do the following:
    BEGIN TRANSACTION
    INSERT INTO Item ([Name]) VALUES ('Item Name')
    SELECT [ID] FROM Item WHERE [Name] = 'Item Name'
    ROLLBACK TRANSACTION
    
    

    The SELECT statement will return the ID of the just inserted record. When I try to do this in Navision through ADO automation, the select statement returns nothing.
    nosce te ipsum
  • kine
    kine Member Posts: 12,562
    Have you tried to set the isolation level after opening the session or beginning the transaction?
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • draken
    draken Member Posts: 9
    Thanks Kine, I've tried setting the isolation level pretty much anywhere in the code. no success
    nosce te ipsum
  • trag
    trag Member Posts: 23
    Try this, probably this works:
    INSERT INTO Item ([Name]) VALUES ('Item Name')
    SELECT @@IDENTITY As ItemId
    
  • draken
    draken Member Posts: 9
    I found that even though adoRS.RecordCount returns -1, the following statement returns a value:

    adoRS.Fields.Item(0).Value)

    No doubt some subtlety just beyond my realm of perception. Anyway, problem no more, thanks for your input.

    draken
    nosce te ipsum