title: dbExpress/No key specified
body: <p>I am working on a database program, using the dbExpress components (Delphi 7). The data is retrieved from the database via the following components:  TSQLDataSet -> TDataSetProvider -> TClientDataSet -> TDatasource -> TDBEdit. Until now, the form has worked correctly. The query in the TSQLDataset is</p>
  
  <pre><code>select id, name, byteken, timeflag from scales where id = :p1
  </code></pre>
  
  <p>I added a large (2048) varchar field to the database table; when I add this field to the above query (and connect either a TDBMemo or a TDBRichEdit) to the TDatasource), I receive the following message when I try to edit the value in the new text field</p>
  
  <pre><code>Unable to find record. No key specified.
  </code></pre>
  
  <p>I get the same error when there is no TDBMemo on the form (but with the varchar field in the query). As soon as I remove the varchar field from the query, everything works properly again.</p>
  
  <p>What could be the cause of this problem? </p>
  
  <p>==== More information ====</p>
  
  <p>I have now defined persistent fields in the form. The field which holds the key to the table has its provider flags set to [pfInUpdate,pfInWhere,pfInKey], whereas all the other fields have their flags as [pfInUpdate,pfInWhere]. This doesn't solve the problem.</p>
  
  <p>The persistent fields were defined on the clientdataset. When I defined them on the TSQLDataSet, the error message about 'no key specified' does not occur. The program still puts out this error message (which I neglected to mention earlier):</p>
  
  <pre><code>EDatabase error: arithmetic exception, numeric overflow or string truncation
  </code></pre>
  
  <p>The large string field has the correct value in 'displaywidth' and 'size'.</p>
  
  <p>==== Even more information ====</p>
  
  <p>I rewrote the form to use non-data aware components. One query retrieves the data from the database (using exactly the same query string as I am using in the TSQLDataSet); the data is then transferred to the controls. After the user presses the OK button on the form, the data is passed back to the database via another query which performs an update or an insert. As this works correctly, I don't see what the problem is with the data aware components.</p>
  
  <p>==== Yet another snippet of information ====</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/3356560/string-truncation-on-transfer-to-clientdataset">this question</a> on Stack Overflow which seems to address a similar issue. I changed the query to be</p>
  
  <pre><code>select id, name, name, byteken, timeflag, 
  cast (constext as varchar (2048)) as fconstext
  from scales
  where id = :p1
  </code></pre>
  
  <p>and set the dbMemo's datafield to be 'fconstext'. After adding text to the dbMemo, the 'applyupdates' call now fails with the following message</p>
  
  <pre><code>column unknown 'fconstext'
  </code></pre>
  
  <p>despite the fact that there is a persistent field created with that name.</p>
  
  <p>I don't know whether this helps or simply muddies the water.</p>
  
  <p>==== More information, 23 April ====</p>
  
  <p>I dropped the field from the database table, then added it back. The program as written works fine as long as the string being entered into the problematic data field is less than about 260 chars. I added ten characters at a time several times without problem until the string length was 256. Then I added some more characters (without counting), tried to save - and got the error. From this point on, trying to add even <strong>one more character</strong> causes the error message (which comes at the 'applyupdates' method of the clientdataset). </p>
  
  <p>Originally, the field contained 832 characters, so there is not a hard limit to the number of characters which I can successfully store. But once the error message appears, it always appears, as if the database remembers that there is an error.</p>
  
  <p>==== More information, 24 April ====</p>
  
  <p>Once again, I dropped the field from the database then added it back; the character set is WIN1251, for reasons which are not clear to me now (I don't need Cyrillic characters). The maximum number of characters which I can enter using <strong>data-aware controls</strong> seems to be about 280, regardless of how the field itself is defined.</p>
  
  <p>I have since moved to using <strong>non-data aware controls</strong> in the real program where this problem occurs, and I can assure you that this limit does not exists there. Thus I am fairly sure that the problem is not due to a mismatch in character size, as has been suggested. Don't forget that I am using Delphi 7, which does not have unicode strings. I think that there is a bug in one of the components, but as I'm using old versions, I imagine that the problem has been solved, but not in the versions which I use.</p>
  
  <p>==== Hopefully final edit, 25/04/12 ====</p>
  
  <p>Following mosquito's advice, I created a new database whose default character set is WIN1252 (UTF-8 did not appear as a choice and and anyway my programs are not unicode). In this clean database I defined the one table, where the 'constext' string's character set was also defined as WIN1252. I ran the data-aware version of the problematic form and was able to enter text without problem (currently over 1700 characters). </p>
  
  <p>It would seem, thus, that the problem was created by having one character set defined for the database and one for the field. I don't know how to check in retrospect what the default character set of the database was defined as, so I can't confirm this.</p>
  
  <p>I now have the small problem of defining a new database (there are 50+ tables) and copying the data from the original database. As this database serves the customer's flagship product, I am somewhat wary of doing this....</p>