title: KeyNotFoundException: SQLCE Database Exception
body: <p>I am using a dataSet in C#. I have done a direct count for the number of items in
  the SQL CE database, so I know how many items that I have in the database. There are no deletions in the the database.</p>
  
  <p>I get the following unexplained exception(s) :</p>
  
  <p><strong>A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll</strong></p>
  
  <p><strong>A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll KeyNotFoundException</strong></p>
  
  <p>This manifests where the communication locks with the SQL CE database. </p>
  
  <p>Does anyone know what causes this and how this can be fixed?</p>
  
  <p>I have searched online and am having no luck to any solutions for this :-(</p>
  
  <pre><code>        // List to be created
          List&lt;TableDat&gt; result = null;
          lock (_sqlcn)
          {
              // Assumes that connection is a valid SqlConnection object.
              SqlCeDataAdapter adapter = new SqlCeDataAdapter(orderSQL, _sqlcn);
              try
              {
                  // DataSet that will be returned.
                  System.Data.DataSet ds = new System.Data.DataSet();
                  int count = adapter.Fill(ds, currentIndex, PageSize, "Logs");
  
  
                  //Iterate through the table and in the List 
                  foreach (System.Data.DataRow myDataRow in ds.Tables["Logs"].Rows)
                  {
                      if (result == null)
                      {
                          result = new List&lt;TableDat&gt;();
                      }
                      result.Add(
                                new TableDat(
                                    (int)myDataRow[Resources.ID],
                                    (DateTime)myDataRow[Resources.TimeStamp],
                                    (int)myDataRow[Resources.EVENTLOG_SID],
                                    (int)myDataRow[Resources.EVENTLOG_EID])
                                );
  
                  }
                  // Set the dataSet object to NULL : No longer needed as the list 
                  // has  been populated
                  ds = null;
              }
  </code></pre>
  
  <p>The biggest problem I see is that this stops the SQL CE database working. Soz, I cannot make out what I should do in the exception handling here as I get nothing in my list which means that the caller has no details for the list??</p>
  
  <p>This just manifests itself. I am looking for a solution which can handle the exception and also to know why my database is behaving as such :-(</p>
  
  <p>Stack Trace looks like:</p>
  
  <pre><code>A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
  A first chance exception of type 'System.IO.IOException' occurred in System.dll
  A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
  A first chance exception of type 'System.IO.IOException' occurred in System.dll
  A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
  A first chance exception of type 'System.IO.IOException' occurred in System.dll
  A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
  A first chance exception of type 'System.IO.IOException' occurred in System.dll
  A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
  A first chance exception of type 'System.IO.IOException' occurred in System.dll
  A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
  A first chance exception of type 'System.IO.IOException' occurred in System.dll
  A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
  A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll
  KeyNotFoundException
  A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
  </code></pre>