Solved

Convert TWideString to TString

Posted on 2006-11-20
12
3,869 Views
Last Modified: 2010-08-05
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
Comment
Question by:Chipmunk77
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17980215
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
 
LVL 9

Expert Comment

by:bernani
ID: 17980222

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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17980227
By the way, you should also be enumerating from zero.

eg:

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

---

Russell

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 9

Expert Comment

by:bernani
ID: 17980246

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


0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17980435
No problem, happens to all of us from time to time :-)

Russell
0
 
LVL 28

Expert Comment

by:TName
ID: 17980441
Hi, adding a WideString to a listbox works for me without typecast...

var
  ws: WideString;
begin
  ws := 'SomeWideString';
  ListBox1.Items.Add(ws);
0
 
LVL 28

Expert Comment

by:TName
ID: 17980468
Ok, sorry,  StringS and WideStringS!  :/
0
 

Author Comment

by:Chipmunk77
ID: 17980598
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
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17981238

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

0
 
LVL 9

Expert Comment

by:bernani
ID: 17981341
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
 
LVL 28

Expert Comment

by:TName
ID: 17981398
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
 

Author Comment

by:Chipmunk77
ID: 17982286
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now