Hierarchical table sorting with a parent-child relationship

This code snippet works beautifully. Just replace the table/field names like so: 

  • AccountID with ID of your child record
  • name with name of your child record
  • parentID with the parent ID of of your child record
  • @tbl_accounts with the name of your hierarchical table

Run and done:

with cte as
(
select
    Accountid,
    name,
    parentid,
    cast(row_number()over(partition by parentid order by name) as varchar(max)) as [path]
from @tbl_accounts
where parentid = 0
union all
select
    t.AccountID,
    t.name,
    t.ParentID,
    [path] + cast(row_number()over(partition by t.parentid order by t.name) as varchar(max))
from
30.    cte
join @tbl_accounts t on cte.AccountID = t.ParentID
)
 
select 
    Accountid,
    name,
    ParentID,
    [path]
from cte
order by path

 

Thanks, Kev Riley!

http://ask.sqlservercentral.com/questions/48518/hierarchical-table-sorting-with-a-parent-child-rel.html

How to pass objects back from Backgroundworker thread: Tuple Class

http://thegrayzone.co.uk/blog/2010/07/background-worker-and-multiple-parameters/

 using System;
using System.ComponentModel;

private void StartBackgroundWorker()
{
  BackgroundWorker worker = new BackgroundWorker();

  worker.DoWork += new DoWorkEventHandler(worker_DoWork);
  worker.RunWorkerCompleted += new  RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
 
  // Declare Tuple object to pass multiple params to DoWork method.
  var params = Tuple.Create<int, DateTime, bool>(44, DateTime.Now, false);
  worker.RunWorkerAsync(params);
}

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
  // Get Tuple object passed from RunWorkerAsync() method
  Tuple<int, DateTime, bool> params = e.Argument as Tuple<int, DateTime, bool>

  // Do some long running process
 
  // Set the result using new Tuple object to pass multiple objects to completed event handler
  e.Result = Tuple.Create<CustomObject, bool, string>(customObj, true, “hello”);
}

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
  // Get result objects
  Tuple<CustomObject, bool, string> res = e.Result as Tuple<CustomObject, bool, string>
}

http://msdn.microsoft.com/en-us/library/system.tuple.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

 

Closing Forms in VB.NET

This code finds all open forms and closes them one by one. Then it exits the application. This ensure nothing is left behind leaching memory.

http://bytes.com/topic/visual-basic-net/answers/507363-close-all-forms-but-main

dim Success as Boolean
do
success = true
try
for each f as form in my.application.openforms
if f.name <> “Main” then f.close()
next f
catch ex as exception
success = false
end try
loop until success

Me.close()
Application.Exit()

 

Another note about the above code:http://www.vbforums.com/archive/index.php/t-255285.html

Application.Exit() does NOT raise the Form.Close() event and can leave processes of a Form running in the memory ( thus is classed as unsafe ) , before running Application.Exit() you must use Form.Close() on each Form. here’s a quote direct from MSDN …

CAUTION The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

Refreshers of the Day #sql #excel #word

Updating a column to null in SQL

http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.sqls.doc/sqls872.htm

Updating columns in general

http://www.w3schools.com/sql/sql_update.asp

Deleting rows in general

http://www.w3schools.com/sql/sql_delete.asp

Copying visible cells only from a filtered excel sheet

http://office.microsoft.com/en-us/excel-help/copy-visible-cells-only-HA010244…

Find and Replace Carriage Returns

http://uk.answers.yahoo.com/question/index?qid=20070929072105AAjHDWu