Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi Couldn't convert varient of type (NULL) into type (String) (https://www.delphipraxis.net/92033-couldnt-convert-varient-type-null-into-type-string.html)

Svenkan 13. Mai 2007 17:09

Datenbank: dBase • Zugriff über: BDE

Couldn't convert varient of type (NULL) into type (String)
 
Delphi-Quellcode:
  for count := 1 to Pred(Query2.RecordCount) do begin
  hcode:='<tr>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+Query2.FieldValues['KLASSEN']+'</font></td>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+Query2.FieldValues['S']+'</font></td>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+Query2.FieldValues['FUER']+'</font></td>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+Query2.FieldValues['WER']+'</font></td>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+Query2.FieldValues['FACH']+'</font></td>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+Query2.FieldValues['RAUM']+'</font></td>'+sLineBreak+'<td width=37% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+Query2.FieldValues['BEMER_SCH']+'</font></td>'+sLineBreak+'</tr>'+sLineBreak;
   WriteLn(Datei, hcode);
  end;
Bei diesem Aufruf bekomme ich die im Topic genannte Fehlermeldung.
Zudem wird immer für alle Zeile nur der Inhalt der ersten Zeile ausgegeben.
Hat da jemand eine Idee?

mkinzler 13. Mai 2007 17:10

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Ein tabellenfeld hat den Wert NULL was in Delphi in etwa mit Nil vergleichbar ist, also keinen Wert.

Svenkan 13. Mai 2007 17:12

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Kann Delphi dann nicht einfach akzeptieren, dass es nichts auszugeben gibt?
Wie kann ich das Problem beheben?

mkinzler 13. Mai 2007 17:13

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Vor der Ausgabe auf die Null-Werte reagieren (.isNull)

Christian Seehase 13. Mai 2007 17:15

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Moin Svenkan,

Du könntest Dir auch einfach eine Funktion bauen, der Du die Feldwerte übergibst, und die dann, je nach Inhalt einen für Dich verarbeitbaren Wert zurückgibt.

jbg 13. Mai 2007 17:51

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Zitat:

Zitat von Christian Seehase
Du könntest Dir auch einfach eine Funktion bauen

Du meinst so eine Funktion wie VarToStr aus der Variants Unit? :wink:

mkinzler 13. Mai 2007 17:52

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Ja so ähnlich, dort könntest du auf Null abfragen und ggf 0 oder '' zurückgeben. Bei anderen DBMS kann man das auch direkt in der Abfrage erledigen.

Svenkan 13. Mai 2007 17:56

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Zitat:

Zitat von mkinzler
Vor der Ausgabe auf die Null-Werte reagieren (.isNull)

Dadurch bekomm ich leider nur die Fehlermeldung 'Invalid varient operation'. :(

mkinzler 13. Mai 2007 17:58

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Zeig mal deinen geänderten Code

Svenkan 13. Mai 2007 18:00

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Delphi-Quellcode:
for count := 1 to Pred(Query2.RecordCount) do begin
  Query2.FieldValues['WER'].isNull;
  Query2.FieldValues['FUER'].isNull;
  Query2.FieldValues['RAUM'].isNull;
  Query2.FieldValues['BEMER_SCH'].isNull;

mkinzler 13. Mai 2007 18:02

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Delphi-Quellcode:
if not Query2.FieldValues['WER'].isNull then ... else ...

Svenkan 13. Mai 2007 18:30

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Ich hab das jetzt so gemacht:

Delphi-Quellcode:
hcode2:='<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+if not Query2.FieldValues['WER'].isNull then Query2.FieldValues['WER'] else empty+'</font></td>';
Aber entweder bin ich zu blöd zu kapieren, wie das funktioniert oder es klappt so wirklich nicht..
Zitat:

[Error] Unit2.pas(228): Statement expected, but expression of type 'String' found

mkinzler 13. Mai 2007 18:41

Re: Couldn't convert varient of type (NULL) into type (Strin
 
So geht es natürlich nicht.

Delphi-Quellcode:
function CheckNull( Field: TField; eValue: Variant):Variant;
begin
    if not Field.isNull then result := Field.Value else result := eValue;
end;
...
hcode:='<tr>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+CheckNull(Query2.FieldByName('KLASSEN'], 'empty'))+...

Svenkan 13. Mai 2007 18:51

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Danke soweit schonmal!
Bin irgendwie immer noch nicht so ganz ausm PHP-Konzept raus..
Leider bekomme ich den gleichen fehler mit dem NULL-Dingens immer noch..:(
Ich habe alles nach deinem Schema umgebaut.

Delphi-Quellcode:
   hcode:='<tr>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+CheckNull(Query2.FieldByName('KLASSEN'), 'empty')+'</font></td>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+CheckNull(Query2.FieldByName('S'), 'empty')+'</font></td>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+CheckNull(Query2.FieldByName('FUER'), 'empty')+'</font></td>'+sLineBreak;
   WriteLn(Datei, hcode);
   hcode2:='<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+CheckNull(Query2.FieldByName('WER'), 'empty')+'</font></td>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+CheckNull(Query2.FieldByName('FACH'), 'empty')+'</font></td>'+sLineBreak+'<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+CheckNull(Query2.FieldByName('RAUM'), 'empty')+'</font></td>'+sLineBreak+'<td width=37% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> '+CheckNull(Query2.FieldByName('BEMER_SCH'), 'empty')+'</font></td>'+sLineBreak+'</tr>';
   WriteLn(Datei, hcode2);
Das erste Feld, welches leer (also nil) ist, wäre 'WER'. Und genau in der Zeile befindet sich dann auch wieder der Ausgabefehler.

mkinzler 13. Mai 2007 19:08

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Hast du mal im Debugger geschaut, wo dieser Fehler auftritt?

Svenkan 13. Mai 2007 19:09

Re: Couldn't convert varient of type (NULL) into type (Strin
 
In der Zeile wo hcode2 geschrieben werden sollte.

mkinzler 13. Mai 2007 19:10

Re: Couldn't convert varient of type (NULL) into type (Strin
 
In meiner Funktion

Svenkan 13. Mai 2007 19:14

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Es ist genau die gleiche Stelle, wie vor dem Einbau deiner Funktion.

mkinzler 13. Mai 2007 19:20

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Nein schau dir mal der Ablauf in der Funktion an.

Svenkan 13. Mai 2007 19:23

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Sry wenn ich jetzt so blöd frage, aber..
wo gennau? :D
Da gibts ziemlich viele Debugwindows..

mkinzler 13. Mai 2007 19:24

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Setz doch einen Breakpoint auf die Zeile mit 'Wer' und geh dann mit F7 weiter

Svenkan 13. Mai 2007 19:28

Re: Couldn't convert varient of type (NULL) into type (Strin
 
OK, deine Funktion arbeitet das Programm komplett durch.
Aber dann wenn es in die nächste Zeile zum Schreiben des Codes aus 'hcode2' geht meckert er rum.
Davor macht er alles ganz normal.

mkinzler 13. Mai 2007 19:32

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Was wird zurückgegeben?

Svenkan 13. Mai 2007 19:38

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Siehe Topic. ;)

mkinzler 13. Mai 2007 19:41

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Nein, was liefert meine Funktion zurück, wenn du ihr eine Null-Feld übergibst?

Svenkan 13. Mai 2007 19:41

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Über die Breakpoints, was du mir grade gesagt hast, kommt gar nichts zurück, bis eben der genannte Fehler auftritt.

mkinzler 13. Mai 2007 19:43

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Aber du solltest im Einzelschrittmodus sehen, in welchen Zweig der If-Anweisung er verzweigt.

alzaimar 13. Mai 2007 19:56

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Sorry, wenn ich irgendwas verpasst habe, aber wieso ersetzt Du nicht
Delphi-Quellcode:
Query.FieldValues['xxx']
durch
Delphi-Quellcode:
Query.FieldByName('xxx').AsString
Dann hast Du die Probleme doch erst gar nicht. :gruebel:

marabu 13. Mai 2007 21:28

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Hallo,

ich möchte noch eine Lanze für die PageProducer-Komponenten brechen. Hier wäre eine Gelegenheit für den Einsatz von TDataSetPageProducer. Einfach das HTML-Template zuweisen:

XML-Code:

<tr>
<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> <#FIELD name=KLASSEN></font></td>
<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> <#FIELD name=S></font></td>
<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> <#FIELD name=FUER></font></td>
<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> <#FIELD name=WER></font></td>
<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> <#FIELD name=FACH</font></td>
<td width=9% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> <#FIELD name=RAUM></font></td>
<td width=37% bgcolor=#666666><font color=#FFFFFF size=2 face=Arial> <#FIELD name=BEMER_SCH></font></td>
</tr>
Und ein kleines Stück Code:

Delphi-Quellcode:
procedure TDataModel.DataSetPageProducerHTMLTag(Sender: TObject; Tag: TTag;
  const TagString: String; TagParams: TStrings; var ReplaceText: String);
var
  fldName: string;
  producer: TDataSetPageProducer;
begin
  producer := Sender as TDataSetPageProducer;
  if (Tag = tgCustom) and SameText(TagString, 'FIELD') then
  begin
    fldName := TagParams.Values['name'];
    if Assigned(producer.DataSet.Fields.FindField(fldName))
      then ReplaceText := VarToStr(producer.DataSet.FieldValues[fldName])
      else ReplaceText := Format('[?%s]', [fldName]);
  end;
end;

{ ... }

procedure TDemoForm.ButtonClick(Sender: TObject);
begin
  with DataModel do
  begin
    Query.Open;
    while not Query.Eof do
    begin
      Memo.Lines.Add(DataSetPageProducer.Content);
      Query.Next;
    end;
    Query.Close;
  end;
end;
So richtig gut wird das ganze mit verketteten Producern.

Darüber meditieren ihr solltet.

Gute Nacht

Svenkan 14. Mai 2007 15:35

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Liste der Anhänge anzeigen (Anzahl: 1)
Zitat:

Zitat von alzaimar
Sorry, wenn ich irgendwas verpasst habe, aber wieso ersetzt Du nicht
Delphi-Quellcode:
Query.FieldValues['xxx']
durch
Delphi-Quellcode:
Query.FieldByName('xxx').AsString
Dann hast Du die Probleme doch erst gar nicht. :gruebel:

Sehr schön :)
Immerhin ist der Fehler schonmal weg.
Wie bekomme ich es nun noch hin, dass nicht immer nur das selbe geschrieben wird, sondern jede Zeile auch das, was dafür in der Tabelle steht?

mkinzler 14. Mai 2007 15:39

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Hast du das .Next vergessen?

Svenkan 14. Mai 2007 15:43

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Das tut ja schon fast weh..:D
Genau das wars!
Dankeschön Leute!
Ihr habt mir wie immer super geholfen!
Echt super Forum ;)
(hoffentlich werd ich auch ma bald so gut wie ihr..)

QuickAndDirty 14. Mai 2007 15:46

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Was spricht gegen

Fieldbyname('BLA').asString ???

Svenkan 14. Mai 2007 15:48

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Zitat:

Zitat von QuickAndDirty
Was spricht gegen

Fieldbyname('BLA').asString ???

Das benutze ich ja, auf Anraten von alzaimar, nun auch. ;)

alzaimar 14. Mai 2007 15:48

Re: Couldn't convert varient of type (NULL) into type (Strin
 
Zitat:

Zitat von QuickAndDirty
Was spricht gegen

Fieldbyname('BLA').asString ???

Nix, hatte ich nur schon vorgeschlagen. :mrgreen:


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:52 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz