Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread TDataSet column exists
Wed, May 31 2017 5:15 AMPermanent Link

thomh

In order to check if a column exists in a dataset I tried

LCol := DataSet.Columns['MyColumn'];
if Assigned(LCol) then
begin
end;

This gives me an Application Error:

 Column "MyColumn" not found
 Line: 1

Wrapping the call in a try..except silences the exception:

try
 LCol := DataSet.Columns['MyColumn'];
except
end
if Assigned(LCol) then
begin
end;

But is this the correct way to check if a column exists?

// Thom
Wed, May 31 2017 5:33 AMPermanent Link

Walter Matte

Tactical Business Corporation



 if  Dataset.Columns.IndexOf('MyField') = -1 then
 begin
    // Field Does Not Exist
 end;

Walter
Wed, May 31 2017 5:41 AMPermanent Link

Uli Becker

function MyForm.ColumnExists(ADataset: TDataset; AColumnName: string):
boolean;
var
   i: integer;
begin
   Result := false;
   for i := 0 to ADataset.Columns.Count -1 do
   begin
      if SameText(ADataset.Columns[i].Name,AColumnName) then
      begin
         Result := true;
         break;
      end;
   end;
end;

Uli
Wed, May 31 2017 5:41 AMPermanent Link

Uli Becker

Walter,

>   if  Dataset.Columns.IndexOf('MyField') = -1 then
>   begin
>      // Field Does Not Exist
>   end;

Simple and effective!

Uli
Wed, May 31 2017 11:03 AMPermanent Link

thomh

>Uli Becker wrote:
>
>Walter,
>
>>   if  Dataset.Columns.IndexOf('MyField') = -1 then
>>   begin
>>      // Field Does Not Exist
>>   end;
>
>Simple and effective!
>
>Uli

I agree, Uli.

Thanks, guys.

// Thom
Image