Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6178073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:24:09+00:00 2026-05-24T00:24:09+00:00

In this case I have some code which is working without problem in an

  • 0

In this case I have some code which is working without problem in an existing program, but throws an excecption when I use it in a new program.

It may not be the best code, but it is working in every day use …

Function  DoSQlCommandWithResultSet(const command : String; 
                                    AdoConnection : TADOConnection; 
                                    resultSet : TStringList): Boolean;
  var i : Integer;
      AdoQuery : TADOQuery;
begin
  Result := True;
  resultSet.Clear();

  AdoQuery := TADOQuery.Create(nil);
  try
    AdoQuery.Connection := AdoConnection;
    AdoQuery.SQL.Add(command);
    AdoQuery.Open();

    i := 0;
    while not AdoQuery.eof do
    begin
      resultSet.Add(ADOQuery.Fields[i].Value);
      AdoQuery.Next;
      Inc(i);
    end;

  finally
    AdoQuery.Free();
  end;
end;

Yes, it probably needs a try/catch and the boolean result isn’t used, but it works …

…. in the previous program, but in a new one it thows an exception when called …

procedure TForm1.FormCreate(Sender: TObject);
   var my_stringlist : TStringList;
       i : integer;
begin
   AdoConnection := TADOConnection.Create(nil);

   if ConnectToDefaultDatabase(AdoConnection) = False then
      MessageDlg('Agh !', mtError, [mbOK], 0);

   my_stringlist := TStringList.Create();
   if DoSQlCommandWithResultSet('show databases', AdoConnection, my_stringlist) = False then
      MessageDlg('Urk !', mtError, [mbOK], 0);

   for i := 0 to Pred(my_stringlist.Count) do
      memo1.Lines.Add(my_stringlist.Strings[i]);
end;  // FormCreate()

Now, here’s the interesting part … it throws the exception on Inc(i) and, if I replace that while loop with a for loop …

    for i := 0 to Pred(ADOQuery.Fields.count) do
      resultSet.Add(ADOQuery.Fields[i].Value);

it works just fine.

I suppose that I could just use the for loop & move on, but I would like to understand what is going wrong …. can someone explain to me? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:24:10+00:00Added an answer on May 24, 2026 at 12:24 am

    The first thing that jumps out at me is that

    i := 0;
    while not AdoQuery.eof do
    begin
      resultSet.Add(ADOQuery.Fields[i].Value);
      AdoQuery.Next;
      Inc(i);
    end;
    

    and

    for i := 0 to Pred(ADOQuery.Fields.count) do
      resultSet.Add(ADOQuery.Fields[i].Value);
    

    are not semantically equivalent! When you call Next, you’re advancing the current record in the dataset. A loop until you hit EOF will run through each record in the dataset once. But the second loop never calls Next and doesn’t check for EOF; it’s grabbing all the fields from one record.

    If I had to guess what’s causing the exception in the first loop, I’d say that you’ve got more records (rows) than fields (columns) in your dataset, and so after enough iterations, i ends up at ADOQuery.Fields.Count and you get an index out of bounds error.

    What exactly are you trying to do here?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that looks like this: someFunc(value) { switch(value){ case 1: case
Consider this simplified view of some code with which I'm working: @Stateless(...) @Remote(...) @TransactionAttribute(TransactionAttributeType.MANDATORY)
Suppose that I have a Java program within an IDE (Eclipse in this case).
I have a lot of methods (in this case, from web services, but maybe
Imagine this case where I have an object that I need to check a
Scenario: I have a document I created using LaTeX (my resume in this case),
i have this block of xslt if-else case and was wondering if there's a
Take a very simple case as an example, say I have this URL: http://www.example.com/65167.html
I ran across this case of UnboundLocalError recently, which seems strange: import pprint def
I think I know how to handle this case, but I just want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.