Microsoft KB Archive/308476

= PRB: ADOCE Records May Be Lost When Power Turned Off =

Article ID: 308476

Article Last Modified on 7/27/2004

-

APPLIES TO


 * Microsoft eMbedded Visual Basic 3.0

-



This article was previously published under Q308476



SYMPTOMS
Microsoft CE Data Access 3.1 (ADOCE) records that are stored in a persistent database may be lost when the device is suspended, reset, or shut down, even after the database connection has been closed.



MORE INFORMATION
Records are flushed to the database only when the connection object is closed and released.

If you are using recordsets directly to write data into a database without connecting the recordset to the connection object, a temporary connection object will be created and destroyed internally. In this case, the connection string is passed as the second parameter in the Open method of the recordset. The last record will be saved only when the internal record cursor moves from the current location.

In your application, if you already have a connection object that is connected to a database but you are using recordsets directly to write data without explicitly connecting the recordset to the connection object, then the existing connection object will be used and data will be saved only after the connection object has been closed and released.

Steps to Reproduce Behavior
 Start a new Windows CE project in eMbedded Visual Basic. From the Project menu, click Components, and then select Microsoft CE ListView Control 6.0. From the Project menu, click References, and add a reference to the Microsoft CE ADO Control 3.1. Add four command buttons and one listview control to Form1. Do not be concerned with the placement of the controls.  Paste the following code into Form1: Option Explicit Dim cnn As ADOCE.Connection Dim rs As ADOCE.Recordset

Private Sub Form_Load Command1.Move 120, 120, 3255, 255 Command2.Move 120, 480, 3255, 255 Command3.Move 120, 840, 3255, 255 Command4.Move 120, 1200, 3255, 255 ListViewCtrl1.Move 120, 1560, 3255, 1695 Command1.Caption = &quot;Create Database&quot; Command2.Caption = &quot;Add Record&quot; Command3.Caption = &quot;Release Connection Object&quot; Command4.Caption = &quot;View Data&quot; Create_DB End Sub

Private Sub Command1_Click Create_DB End Sub

Private Sub Command2_Click Set cnn = CreateObject(&quot;ADOCE.Connection.3.1&quot;) Set rs = CreateObject(&quot;ADOCE.Recordset.3.1&quot;) cnn.Open &quot;Provider=CEDB;Data Source = \My Documents\db1.cdb&quot; rs.Open &quot;Table1&quot;, cnn, 1, 3 rs.AddNew rs.Fields(&quot;Field1&quot;) = &quot;Edgar&quot; rs.Fields(&quot;Field2&quot;) = &quot;Martinez&quot; rs.Update rs.Close cnn.Close End Sub

Private Sub Command3_Click Set cnn = Nothing End Sub

Private Sub Command4_Click Open_RS End Sub

Private Sub Form_OKClick App.End End Sub

Private Sub Open_RS ListViewCtrl1.ListItems.Clear

Set rs = CreateObject(&quot;adoce.recordset.3.1&quot;) rs.Open &quot;Table1&quot;, &quot;\My Documents\db1.cdb&quot;, 1, 3, 2 'fill listview Dim clmX As ColumnHeader Dim itmX As ListItem ListViewCtrl1.View = lvwReport 'use reportview no icons Set clmX = ListViewCtrl1.ColumnHeaders.Add(,, &quot;Field1&quot;, _       ListViewCtrl1.Width / 2) Set clmX = ListViewCtrl1.ColumnHeaders.Add(,, &quot;Field2&quot;, _       ListViewCtrl1.Width / 2) While Not rs.EOF Set itmX = ListViewCtrl1.ListItems.Add(,, rs.Fields(&quot;Field1&quot;)) itmX.SubItems(1) = rs.Fields(&quot;Field2&quot;) rs.MoveNext Wend End Sub

Private Sub Create_DB Dim sSQL As String Set rs = CreateObject(&quot;ADOCE.Recordset.3.1&quot;) On Error Resume Next

sSQL = &quot;CREATE DATABASE '\My Documents\db1.cdb'&quot; rs.Open sSQL rs.Close Set rs = Nothing Set rs = CreateObject(&quot;ADOCE.Recordset.3.1&quot;) sSQL = &quot;CREATE TABLE Table1 (Field1 Text, Field2 Text)&quot; rs.Open sSQL, &quot;\My Documents\db1.cdb&quot;, 1, 1 rs.Close Set rs = Nothing End Sub  Run the project and click the Create Database command button. Click the Add Record command button.</li> Click the Release Connection Object command button.</li> Click the View Data command button and note that the record was added.</li> Click Add Record again.</li> Click View Data and note that the record has been added.</li> Perform a soft (&quot;warm&quot;) reset of the device and run the eMbedded Visual Basic application again.</li> Click View Data and note that the record was not persisted because the connection object was never set to &quot;nothing&quot;.</li></ol>

<div class="references_section">