Wrox Programmer Forums
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2008, 05:02 PM
Authorized User
 
Join Date: Jun 2008
Posts: 75
Thanks: 15
Thanked 0 Times in 0 Posts
Default Help

Could someone please tell me what the code below does. For example, is this returning the rows where Samplecategories is equal to parentid? Why do we have to have the NULL values and why do we need to know if parentid IS NULL?



Thanks In Advance

Private Sub PopulateRootLevel()

Dim objConn As New SqlConnection(_
"server=JOTEKE\SQLExpress;Trusted_Connection=true; DATABASE=TreeViewSampleDB")
Dim objCommand As New SqlCommand("select id,title,(select count(*) FROM SampleCategories " _
& "WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID IS NULL", _
objConn)



 
Old November 20th, 2008, 11:44 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This is creating a SQL connection object and then a SQL command object. It's not doing anything else apart from that.

As far as your question about the SQL query itself:
It would appear that you have a self referencing categories table that defines a variable depth category tree. The query appears to be giving you a list of top level categories (i.e. ones with no [NULL] parent id) and the number of immediate child categories.

-Peter
compiledthoughts.com
The Following User Says Thank You to planoie For This Useful Post:
dotnetDeveloper (March 13th, 2009)
 
Old November 21st, 2008, 02:27 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts
Default

dotnetDeveloper:

In addition to the response from planoie, my comments references the Query statement only and without know what fields are in the SampleCategories Table:

You are using a Nested SELECT-Statement. This means the inner SELECT will execute first. The inner SELECT will count all records or instances where the parentid = sc.id, if parentid is a reserved word. However it may also error because in your FROM clause the alias sc for SampleCategories IS NOT defined. When the alias is clearly defined you should get a TOTAL or Count of non-blank Categories and this value will be constant;

Looking at the outer SELECT it is not clear whether the childnodecount is a field in the SampleCategories table or a reserved word, but if it is you will get an error on that statement in the WHERE-clause because you have two different parent IDs: parentid and pareentID, decide which one you should use and be consistent. It seems to me, you are looking for those SampleCategories where you have BLANK Parent categories with Child/Children Node(s)or Dangling.

I hope this helps.


========================
Disclaimer: The above comments are solely the opinion of one person and not to be construed as a directive or an incentive to commit fraudulent acts.
The Following User Says Thank You to peace95 For This Useful Post:
dotnetDeveloper (March 13th, 2009)









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.