wildzero
asked on
AdoTable FieldByName OR FieldValues
Hi there,
I have a database and all runs smooth, except sometimes I get the error, can't convert type NULL to type STRING, and I notice this happens when I use either FieldbyName or FieldValues (can't quite remember)
Just wonder which is the better to use when getting data from a database which may not be all there... (ie contain NULL values)
Thanks
Nick
I have a database and all runs smooth, except sometimes I get the error, can't convert type NULL to type STRING, and I notice this happens when I use either FieldbyName or FieldValues (can't quite remember)
Just wonder which is the better to use when getting data from a database which may not be all there... (ie contain NULL values)
Thanks
Nick
You can check FieldByName('FieldName').I sNull but I don't have to do this with ADO and SQL Server using FieldByName. I do have to do it when reading dates though.
If memory serves me right the FieldByName function looks up the field index and then calls FieldValue[ndx] so you should get the error no matter which one you use.
If memory serves me right the FieldByName function looks up the field index and then calls FieldValue[ndx] so you should get the error no matter which one you use.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you are looping through a table then you could also use
if <TableName>.Fields[iCounte r].IsNull then <do something>
if <TableName>.Fields[iCounte
NZ ?? Cool, sounds like Access though :)
kfoster: Exactly where the name comes from... hehe
Hello Sir,
You could check it as follows
if ADOTable1.FieldByName('Inv No').IsNul l or (ADOTable2.FieldByName('In vNo').AsSt ring='') then...
where InvNo is assumed to be of String.
with regards
You could check it as follows
if ADOTable1.FieldByName('Inv
where InvNo is assumed to be of String.
with regards
ASKER
It seems that doing
SomeADOTable.FieldByName(' SomeField' ).AsString
Doesn't throw an exception like
SomeADOTable.FieldValues ['SomeField'] does when the value is null...
SomeADOTable.FieldByName('
Doesn't throw an exception like
SomeADOTable.FieldValues ['SomeField'] does when the value is null...
you should either use
FieldByName('SomeField').A sString
or
VarToStr(FieldValues ['SomeField'])
Using null variants for string operations can raise that kind of exceptions.
FieldByName('SomeField').A
or
VarToStr(FieldValues ['SomeField'])
Using null variants for string operations can raise that kind of exceptions.
yes, Imthiyz_ph is right, if you use FieldByName(SomeField).asS tring an empty string will be returned if the value in the DB is NULL.
The answer is in the db.pas source file.
With FieldValues[] it is returning a variant. there is no internal check for NULL it is just returned as NULL.
With a TStringField which is what FieldByName returns if the field is a string the following function translates the Variant into a string for you so a STRING is returned.
function TStringField.GetAsString: string;
begin
if not GetValue(Result) then Result := '';
end;
if you dig deeper into the code you will find that the .GetValue function returns false is the datasize=0. This results in an emptystring being returned ''.
So FieldValues may be a little faster (it still uses variants) because it removes 1 layer of encapsulation and it also removes the lookup of the FieldName in FieldByName(FieldName). But, unless you then lose some of this 'time saved' because you will have to check for NULL yourself.
Also keep in mind that with FieldValues you are requiring the field order to be the same.
If you are doing a query 'SELECT * FROM TABLENAME' and the fields returned are
RID : Integer
Name : String
SomethingElse : String;
and you add a new field to the table 'It must be at the end of the table or your indexes will be off'
WHEREAS; 'Select RID,NAME,SOMETHINGELSE from Table' will always result in the correct field order no matter where the new field is in the table structure.
Cheers...
With FieldValues[] it is returning a variant. there is no internal check for NULL it is just returned as NULL.
With a TStringField which is what FieldByName returns if the field is a string the following function translates the Variant into a string for you so a STRING is returned.
function TStringField.GetAsString: string;
begin
if not GetValue(Result) then Result := '';
end;
if you dig deeper into the code you will find that the .GetValue function returns false is the datasize=0. This results in an emptystring being returned ''.
So FieldValues may be a little faster (it still uses variants) because it removes 1 layer of encapsulation and it also removes the lookup of the FieldName in FieldByName(FieldName). But, unless you then lose some of this 'time saved' because you will have to check for NULL yourself.
Also keep in mind that with FieldValues you are requiring the field order to be the same.
If you are doing a query 'SELECT * FROM TABLENAME' and the fields returned are
RID : Integer
Name : String
SomethingElse : String;
and you add a new field to the table 'It must be at the end of the table or your indexes will be off'
WHEREAS; 'Select RID,NAME,SOMETHINGELSE from Table' will always result in the correct field order no matter where the new field is in the table structure.
Cheers...
Also, I think you should just use PierreC's NZ function and get on with it :)
ASKER
Thanks guys :)
if FieldByName('NAME').IsNull
sorry,dont use ADO here, just guessing