Chipmunk77
asked on
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.AddStr ings(FDB.A DOQuerySal esman.Fiel dList); - incompatible - TStringList and TFieldList
2. FDB.ADOQuerySalesman.GetFi eldNames(t empList); - deprecated
3. for I := 1 to FDB.ADOQuerySalesman.Field Count do
begin
ListBoxSelect.Items.AddStr ings(FDB.A DOQuerySal esman.Fiel dList.Stri ngs[i]); - incompatible - TStrings and TWideString
end;
4. for i:=1 to FDB.ADOQuerySalesman.Field Count do
begin
ListBoxSelect.Items.Add(FD B.ADOQuery Salesman.F ieldList.S trings[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
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.AddStr
2. FDB.ADOQuerySalesman.GetFi
3. for I := 1 to FDB.ADOQuerySalesman.Field
begin
ListBoxSelect.Items.AddStr
end;
4. for i:=1 to FDB.ADOQuerySalesman.Field
begin
ListBoxSelect.Items.Add(FD
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
Hi,
Did you try to typecast explicitly your strings ?
sth like ListBoxSelect.Items.Add(St
Not at my Delphi station for the moment, can't verify. I've done sth like this in the past.
By the way, you should also be enumerating from zero.
eg:
for i:=0 to FDB.ADOQuerySalesman.Field Count-1 do
---
Russell
eg:
for i:=0 to FDB.ADOQuerySalesman.Field
---
Russell
Sorry, rllibby we answer at the same moment ...
No problem, happens to all of us from time to time :-)
Russell
Russell
Hi, adding a WideString to a listbox works for me without typecast...
var
ws: WideString;
begin
ws := 'SomeWideString';
ListBox1.Items.Add(ws);
var
ws: WideString;
begin
ws := 'SomeWideString';
ListBox1.Items.Add(ws);
Ok, sorry, StringS and WideStringS! :/
ASKER
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
Any ideas? Thanks for the quick answers
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi
Did you try the opposite to see if it gives the same error:
for I := FDB.ADOQuerySalesman.Field Count -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.AddStr ings(Strin g(OleFDB.A DOQuerySal esman.Fiel dList.Stri ngs[i]));
end;
Did you try the opposite to see if it gives the same error:
for I := FDB.ADOQuerySalesman.Field
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.AddStr
end;
Are you sure it was a conversion issue after all?
Because you've tried
ListBoxSelect.Items.Add(FD B.ADOQuery Salesman.F ieldList.S trings[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...)
Because you've tried
ListBoxSelect.Items.Add(FD
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...)
ASKER
Thank you, thank you, thank you - the FieldList.Count, instead of FieldCount was all it took. It is working correctly now! Th
Regards,
Russell
----
for I := 1 to FDB.ADOQuerySalesman.Field
begin
ListBoxSelect.Items.AddStr
end;