Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 2nd, 2004, 07:26 AM
Chris Ulliott Chris Ulliott is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 9 Chris Ulliott User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Recursive procedures

Hi All,

I am seeking advice with Delphi and Recursive Procedures. Basically I want a procedure to call itself BUT some of the member variables are TADOConnection and TADORecordset. When calling itself, and attempting to create these members, the code bombs with an exception.

Are the variables not recognized as completely new members? In C++ the variables would be NULL in each call of the procedure until I explicitly create them... Is this not the case in delphi?

Thanks in advance,

Chris

Reply With Quote
  #2  
Old October 2nd, 2004, 08:05 AM
Chris Ulliott Chris Ulliott is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 9 Chris Ulliott User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Update...

Just an update..

I removed the TADOConnection and TADOQuery variables to be global throughout the form... but still I have a problem with accessing variables in the recursive calls... any ideas? The code is below:

Code:
procedure TfrmPromotions.PopulateTreeNode(nodeItem : TMyNode; nID : Integer);
Var
  strSQL: String;
  Item: TMyNode;
begin
// Populate the tree here...
  strSQL := 'SELECT PROMOTION_ID, [DATETIME], SHORT_DESC, LONG_DESC, LENGTH_OF_EFFECT, PARENT_PROMO_ID FROM PROMOTION WHERE PARENT_PROMO_ID = ';
  strSQL := strSQL + IntToStr(nID);
  strSQL := strSQL + ' ORDER BY PARENT_PROMO_ID;';

  rs.SQL.Text := strSQL;
  rs.Open();

  while rs.Eof <> TRUE do
  begin
       Item := TMyNode(m_treeview.Items.Add(nodeItem, rs.Fields[2].AsString));
       Item.m_nID :=  Integer(rs.Fields[0].Value);
       PopulateTreeNode(Item, Item.m_nID);   // recursive call...
       rs.Next();
  end;
  rs.Close();
end;

Reply With Quote
  #3  
Old November 12th, 2004, 07:43 PM
Chris Ulliott Chris Ulliott is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 9 Chris Ulliott User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Can Anyone Help Pls? Tq..

Can anyone help with this?

Thank you,

Chris

Reply With Quote
  #4  
Old November 14th, 2004, 06:51 PM
Chris Ulliott Chris Ulliott is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 9 Chris Ulliott User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question

Hi,

Thanks for replying. Actually the rs used to be defined within the procedure itself. I was just simply trying somthing out by removing it and making it a public variable.

PARENT_PROMO_ID tells the tree which node is it's parent item. So I call the code initially getting all of the top level items (PARENT_PROMO_ID = -1) and then for each item, I wish to call it again to find all the children etc...

Problem is, it doesnt seem to like re-using the rs. In C++ the object will get created everytime you call the procedure when it is defined locally but Delphi just seems to bomb out even when rs is defined locally OR as a member of the form.

The first call is always fine in both instances, but the second iterative call to the procedure always always bombs out.

Thank you for your help.

Chris

Reply With Quote
  #5  
Old November 14th, 2004, 07:25 PM
tim snl's Avatar
tim snl tim snl is offline
Code Cruncher
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Tasmania, Australia
Posts: 119 tim snl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 21 m 20 sec
Reputation Power: 8
I see a problem here.
I assume "rs" is a dataset defined outside this procedure, you are opening it and closing it with the recursive call in the middle, close your dataset before doing the recursive call.

Why do you think you need recursion for this code?
What are you trying to do here with this tree?

The procedure you have written will fill the tree for you if you just take the recursive call out alltogether.

Recursion is usually used to traverse the tree itself, not to populate it from the database in this way.
__________________
Beware of a programmer with a screwdriver!

Reply With Quote
  #6  
Old November 14th, 2004, 07:53 PM
Chris Ulliott Chris Ulliott is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 9 Chris Ulliott User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking

Oops, clicked the wrong reply button... please see the thread for my reply.

Thanks,
Chris

Quote:
Originally Posted by tim snl
I see a problem here.
I assume "rs" is a dataset defined outside this procedure, you are opening it and closing it with the recursive call in the middle, close your dataset before doing the recursive call.

Why do you think you need recursion for this code?
What are you trying to do here with this tree?

The procedure you have written will fill the tree for you if you just take the recursive call out alltogether.

Recursion is usually used to traverse the tree itself, not to populate it from the database in this way.

Reply With Quote
  #7  
Old November 14th, 2004, 09:06 PM
tim snl's Avatar
tim snl tim snl is offline
Code Cruncher
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Tasmania, Australia
Posts: 119 tim snl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 21 m 20 sec
Reputation Power: 8
The first procedure you had sounds like it should work.
If you are still having a problem with this post that code and I will have a look at it for you.

Reply With Quote
  #8  
Old November 16th, 2004, 06:42 PM
Chris Ulliott Chris Ulliott is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 9 Chris Ulliott User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs down

Hi,

I put the rs back into the local function and still get the error. An access violation is generated when the PopulateTreeNode procedure calls itself and when it hits the line that reads rs := TADOQuery.Create(nil);

This is very simple in VC++ so I guess I am missing something very simple in Delphi. Any help you can offer will be greatly appreciated.

Code is:

Code:
procedure TfrmPromotions.FormCreate(Sender: TObject);
begin
  ADOMasterConnection := TADOConnection.Create(nil);
  ADOMasterConnection.ConnectionString := 'Provider=MSDASQL.1;Persist Security Info=False;Extended Properties="DSN=MS Access Database;DBQ=HPMonMaster.mdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;UID=admin;"';
  PopulateTreeNode(nil, -1);
end;

procedure TfrmPromotions.PopulateTreeNode(nodeItem : TMyNode; nID : Integer);
Var
  strSQL: String;
  Item: TMyNode;
  rs : TADOQuery;
begin
// Populate the tree here...
  rs := TADOQuery.Create(nil);
  rs.Connection := ADOMasterConnection;
  
  strSQL := 'SELECT PROMOTION_ID, [DATETIME], SHORT_DESC, LONG_DESC, LENGTH_OF_EFFECT, PARENT_PROMO_ID FROM PROMOTION WHERE PARENT_PROMO_ID = ';
  strSQL := strSQL + IntToStr(nID);
  strSQL := strSQL + ' ORDER BY PARENT_PROMO_ID;';

  rs.SQL.Text := strSQL;
  rs.Open();

  while rs.Eof <> TRUE do
  begin
       Item := TMyNode(m_treeview.Items.Add(nodeItem, rs.Fields[2].AsString));
       Item.m_nID :=  Integer(rs.Fields[0].Value);
       PopulateTreeNode(Item, Item.m_nID);   // recursive call...
       rs.Next();
  end;
  rs.Close();
end;


Thank you,
Chris

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Recursive procedures


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT