Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4049
  • Last Modified:

Convert TWideString to TString

Hi all,

I am trying to take the field column name data from an ADO Query and put the column names into a listbox.  Prior to the latest release of Delphi this was easy, but now they save the field names as TWideString and everything I have tried I either get index out of bounds or incompatible types TWideString and TStrings.  Does anyone know how to copy the data from the query to a listbox easily??

Here are some of the things I have tried:
1.    ListBoxSelect.Items.AddStrings(FDB.ADOQuerySalesman.FieldList);    - incompatible - TStringList and TFieldList

2.    FDB.ADOQuerySalesman.GetFieldNames(tempList);     - deprecated

3.    for I := 1 to FDB.ADOQuerySalesman.FieldCount do
       begin
           ListBoxSelect.Items.AddStrings(FDB.ADOQuerySalesman.FieldList.Strings[i]);    - incompatible - TStrings and TWideString
       end;

4.    for i:=1 to FDB.ADOQuerySalesman.FieldCount do
       begin
           ListBoxSelect.Items.Add(FDB.ADOQuerySalesman.FieldList.Strings[i]); - index out of bounds
       end;

I need an answer soon, I have a project due date coming up and have more work to do once I figure this out!  

Thanks for any help
0
Chipmunk77
Asked:
Chipmunk77
  • 4
  • 3
  • 3
  • +1
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:
Have you tried casting as string? If its a WideString, the that should work correctly.

Regards,
Russell

----

for I := 1 to FDB.ADOQuerySalesman.FieldCount do
       begin
           ListBoxSelect.Items.AddStrings(String(OleFDB.ADOQuerySalesman.FieldList.Strings[i]));
       end;
0
 
bernaniCommented:

Hi,

Did you try to typecast explicitly your strings ?  

sth like ListBoxSelect.Items.Add(Strings(FDB.ADOQuerySalesman.FieldList.Strings[i])) or  ListBoxSelect.Items.Add(AnsiStrings(FDB.ADOQuerySalesman.FieldList.Strings[i]));

Not at my Delphi station for the moment, can't verify. I've done sth like this in the past.







0
 
Russell LibbySoftware Engineer, Advisory Commented:
By the way, you should also be enumerating from zero.

eg:

for i:=0 to  FDB.ADOQuerySalesman.FieldCount-1 do

---

Russell

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bernaniCommented:

Sorry, rllibby we answer at the same moment ...


0
 
Russell LibbySoftware Engineer, Advisory Commented:
No problem, happens to all of us from time to time :-)

Russell
0
 
TNameCommented:
Hi, adding a WideString to a listbox works for me without typecast...

var
  ws: WideString;
begin
  ws := 'SomeWideString';
  ListBox1.Items.Add(ws);
0
 
TNameCommented:
Ok, sorry,  StringS and WideStringS!  :/
0
 
Chipmunk77Author Commented:
okay, I was able to cast it as a TStrings, but then I get the error List Index Out of Bounds.  I tried with both a 0 to count - 1 and with 1 to count.  I'm not sure what index is out of bounds?  The list box should be adding items, so it's index should not be out of bounds - and the ado query appears in the grid, so the fields should be populated in the TWideStrings

Any ideas?  Thanks for the quick answers
0
 
Russell LibbySoftware Engineer, Advisory Commented:

What does the count property return for?
>> OleFDB.ADOQuerySalesman.FieldList.Count

0
 
bernaniCommented:
Hi

Did you try the opposite to see if it gives the same error:

for I := FDB.ADOQuerySalesman.FieldCount -1 downto 0 do
begin
// Check the value of your FieldCount to be sure it contains a valid and correct value
// Check if the result of each Strings[i] to see if it really contains the correct string
// use Debug.Print to visually see the results
// if OK try the following line
ListBoxSelect.Items.AddStrings(String(OleFDB.ADOQuerySalesman.FieldList.Strings[i]));
end;


0
 
TNameCommented:
Are you sure it was a conversion issue after all?
Because you've tried
  ListBoxSelect.Items.Add(FDB.ADOQuerySalesman.FieldList.Strings[i]);
in the beginning, and ...FieldList.Strings[i] should give you a single WideString at a time (for each i), and the listbox should accept it with or without cast....

Are you sure it's not something else? What happens if you uncomment the line in the loop or the whole loop?
(The Fieldlist.Count that Russel suggested could be the best next move...)
0
 
Chipmunk77Author Commented:
Thank you, thank you, thank you - the FieldList.Count, instead of FieldCount was all it took.  It is working correctly now!  Th
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now