我TADOQUERY(ADOQueChA)组件读取数据库时,当有个记录值为空时,出现提示错误,说不把把空值转换为字符,请问怎么解决这个问题啊
procedure TFrmNumeralTV.ShowChanNum;       ////窗体显示时,频道分类标题同步显示
var
i:integer;
str1,str2:string;
begin
for i:=1 to 8 do
  begin
   With ADOQueCha do
    begin
      close;
      SQL.Clear;
      SQL.Add('select ParentKind from Chanel where ParentKindID=:ID');
      parameters.ParamByName('ID').Value:=i;
      open;      STR1:=LEFTSTR(ADOQueChA.FieldValues['ParentKind'],3);  //出现错误提示:不能把NULL转换为字符(STRING)
     Case i of
       1: LabTitle1.Caption:=STR1;
       2: LabTitle2.Caption:=STR1;
       3: LabTitle3.Caption:=STR1;
       4: LabTitle4.Caption:=STR1;
       5: LabTitle5.Caption:=STR1;
       6: LabTitle6.Caption:=STR1;
       7: LabTitle7.Caption:=STR1;
       8: LabTitle8.Caption:=STR1;
      end;
     STR2:=ADOQueChA.FieldValues['ParentKind'];      //显示完整的标题
      Case i of
       1: LabTitle1.Hint:=STR2;
       2: LabTitle2.Hint:=STR2;
       3: LabTitle3.Hint:=STR2;
       4: LabTitle4.Hint:=STR2;
       5: LabTitle5.Hint:=STR2;
       6: LabTitle6.Hint:=STR2;
       7: LabTitle7.Hint:=STR2;
       8: LabTitle8.Hint:=STR2;
      end;
     end;
    end;end;

解决方案 »

  1.   

    楼主试试改成这样:
    STR1:=LEFTSTR(ADOQueChA.FieldByName('ParentKind').AsString,3);因为我没遇到过这个问题,而我一直是这样取值的,所以我觉得这样应该能解决楼主的问题。
      

  2.   

    还是不行啊,错误为Invalid variant operation
      

  3.   

    晕,那就先判断一下:if VarIsNull(ADOQueChA.FieldValues['ParentKind']) then
      Str1 := ''
    else
      Str1 := LeftStr(ADOQueChA.FieldValues['ParentKind'], 3);
      

  4.   

    STR1:=LEFTSTR(ADOQueChA.FieldValues['ParentKind'],3);  //出现错误提示:不能把NULL转换为字符(STRING) 因为读取的字符串为Null所以执行LeftStr时错误改为
    Str1:= ADOQueChA.FieldValues['ParentKind']
    if Trim(Str1)< 3 then Continue;