I’ve got a long-standing issue to which I’ve never found a solution. This is using Delphi against an MSSQL server with FireDAC.
Any text or varchar(max) type columns come through into my strongly-typed classes as properties of type IROStrings.
If I’m creating/updating a record and want to assign a simple text string to such a properties, I just use the Text property of the IROStrings wrapper.
Whilst this works, it always puts a CR/LF pair at the end of the string, which is obviously a hold-over from TStrings which has a habit of doing this.
Is there any way I can prevent this and assign a string to such a property without the CR/LF on the end?
After some experimentation, using FieldByName(‘field’).AsString appears to work.
I think I’ll add additional properties to my own derived interfaces and client rules classes to implement separate “FieldAsString” type properties for this purpose.
As this will be a little tedious to implement for every such field across my schemas, I could modify the code generator logic so these additional properties are automatically created when I generate my strongly typed classes.
Not sure whether that’s really recommended though as I’d have to re-implement my changes every time I upgraded DA.
procedure TCustomersDataTableRules.Remarks_OnChange(Sender: TObject);
begin
if not f_DisableRemarksEvent and DataTable.Editing then DataTable.Fields[idx_CustomersRemarks].AsVariant := TStringList(Sender).Text;
end;
new:
procedure TCustomersDataTableRules.Remarks_OnChange(Sender: TObject);
begin
if not f_DisableRemarksEvent and DataTable.Editing then DataTable.Fields[idx_CustomersRemarks].AsVariant := TrimRight(TStringList(Sender).Text);
end;
Without testing, I’d have thought that would work yes.
However, it would mean changing the auto-generated strongly-typed classes and such a change couldn’t be implemented in the codegen as it would change the existing behaviour and some users may well be depending on the trailing CRLF being added.
I actually did go through all my classes last night and create new AsString functions & properties in my own derived interfaces and classes which just use the AsString property of the underlying field directly.
This works perfectly and means I can access such fields either way.
When I get a chance I’ll try modifying the codegen to do the same automatically and submit it so you can see what you think.
This trailing CRLF problem is a long-standing ‘quirk’ of Delphi’s TStringList class and has always been very annoying.
We can implement it via definition.
if correspondent definition is defined in DataAbstract_user.inc, it will cut trailing CRLF, otherwise it will use default behavior.