Solved

Convert TWideString to TString

Posted on 2006-11-20
12
3,949 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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