<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Shimon Krokhmal's blog - Architectural solutions</title>
    <link>http://www.krokhmal.com/</link>
    <description>medium : .NET | JavaScript | Secure coding | Databases | Sql Server | Oracle | CodeSmith | SPS | Life</description>
    <language>en-us</language>
    <copyright>Shimon Krokhmal</copyright>
    <lastBuildDate>Wed, 17 Oct 2007 10:15:49 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>Shimonkr@gmail.com</managingEditor>
    <webMaster>Shimonkr@gmail.com</webMaster>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=68f84cff-ecab-4c94-ac9d-ce2ed3428e87</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,68f84cff-ecab-4c94-ac9d-ce2ed3428e87.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,68f84cff-ecab-4c94-ac9d-ce2ed3428e87.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=68f84cff-ecab-4c94-ac9d-ce2ed3428e87</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
As part of a development process, deployment, if handled wrong, may consume much of
your precius time.<br />
my goal is to automate as much as possible the deployment process.
</p>
        <p>
I've read recently a post by <a href="http://blogs.madtechnology.net/blogs/chris/default.aspx">Chris
Burrows</a>, which talks about "<a href="http://blogs.madtechnology.net/blogs/chris/archive/2006/11/22/665.aspx">Setting
Up Your Build Environment with TFS</a>".<br />
I strongly suggest paying him a visit, it contains many insights about the process.
</p>
        <p>
he talks about :<br /></p>
        <li>
Continuous Integration 
</li>
        <li>
Dependency Replication 
</li>
        <li>
Automated Unit Testing and Code Coverage 
</li>
        <li>
Automated Deployment 
</li>
        <li>
Build Verification Tests 
</li>
        <li>
Deployment Verification Tests 
</li>
        <p>
this post is more as a question than just plain info,<br />
How do you practice these subjects ?<br />
Which tools/templates/methodologies do you use to elevate your process ?
</p>
        <img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=68f84cff-ecab-4c94-ac9d-ce2ed3428e87" />
        <br />
        <hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>Automating development process</title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,68f84cff-ecab-4c94-ac9d-ce2ed3428e87.aspx</guid>
      <link>http://www.krokhmal.com/2007/10/17/AutomatingDevelopmentProcess.aspx</link>
      <pubDate>Wed, 17 Oct 2007 10:15:49 GMT</pubDate>
      <description>&lt;p&gt;
As part of a development process, deployment, if handled wrong, may consume much of
your precius time.&lt;br&gt;
my goal is to automate as much as possible the deployment process.
&lt;/p&gt;
&lt;p&gt;
I've read recently a post by&amp;nbsp;&lt;a href="http://blogs.madtechnology.net/blogs/chris/default.aspx"&gt;Chris
Burrows&lt;/a&gt;, which talks about "&lt;a href="http://blogs.madtechnology.net/blogs/chris/archive/2006/11/22/665.aspx"&gt;Setting
Up Your Build Environment with TFS&lt;/a&gt;".&lt;br&gt;
I strongly suggest paying him a visit, it contains many insights about the process.
&lt;/p&gt;
&lt;p&gt;
he talks about :&lt;br&gt;
&lt;/p&gt;
&lt;li&gt;
Continuous Integration 
&lt;li&gt;
Dependency Replication 
&lt;li&gt;
Automated Unit Testing and Code Coverage 
&lt;li&gt;
Automated Deployment 
&lt;li&gt;
Build Verification Tests 
&lt;li&gt;
Deployment Verification Tests 
&lt;/li&gt;
&lt;p&gt;
this post is more as a question than just plain info,&lt;br&gt;
How do you practice these subjects ?&lt;br&gt;
Which tools/templates/methodologies do you use to elevate your process ?
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=68f84cff-ecab-4c94-ac9d-ce2ed3428e87" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,68f84cff-ecab-4c94-ac9d-ce2ed3428e87.aspx</comments>
      <category>.Net</category>
      <category>Architectural solutions</category>
    </item>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=847401fa-44b9-4eb4-b999-1d98669dc901</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,847401fa-44b9-4eb4-b999-1d98669dc901.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,847401fa-44b9-4eb4-b999-1d98669dc901.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=847401fa-44b9-4eb4-b999-1d98669dc901</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
for those folks who worked with oracle, know that sequence is an internal database
object which has it's own methods and properties.<br /><strong>hey, why shouldn't i use this wonderful auto number feature that sql server
has ?<br /></strong>well, auto number will give you a good solution to most of your needs that
a sequence provided in oracle.<br />
but, here is some conditions where auto number just won't suffice :
</p>
        <ul>
          <li>
Managing more than one "auto number" in one table - auto number can be defined only
at one field.</li>
          <li>
Managing the same auto number across multiple tables - the auto number can be asigned
only to a specified table.</li>
        </ul>
        <p>
the solution is simple: 
<br />
we will create some framework that will allow us to manage sequences and work with
them.
</p>
        <p>
first we will create one table to contain the sequences.
</p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">CREATE</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">TABLE</span> [dbo].[sysSequences]
(<br />
    [SequenceName] [<span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">char</span>]
(30) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">COLLATE</span> Hebrew_CI_AS <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NOT</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NULL</span> ,<br />
    [Incrasement] [<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>] <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NOT</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NULL</span> ,<br />
    [StartValue] [<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>] <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NOT</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NULL</span> ,<br />
    [CurrentValue] [<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>] <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NOT</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NULL</span><br />
) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> [<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">PRIMARY</span>]<br />
GO</span>
        </p>
        <p>
next thing we need to do is create some stored procedures that will handle these sequences.
</p>
        <p>
the create sequence procedure :<br /></p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">CREATE</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">procedure</span> dbo.sp_Create_Sequence<br />
( 
<br />
    @seqName     <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">varchar</span>(30),<br />
    @Increasement     <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>,<br />
    @startValue    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span><br />
)<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">as</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">begin</span><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">declare</span> @r <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">begin</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">transaction</span><br /><br />
    <span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--
Check if the sequence exists</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> @r
=(    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">select</span><span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">count</span>(*) 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">from</span> sysSequences <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">where</span> SequenceName
= @SeqName)<br />
    <br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">if</span> @r
&lt;&gt; 0 <span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--if
error is raised</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">begin</span><br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">goto</span> LogError<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">end</span><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">insert</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">into</span> sysSequences
(SequenceName,Incrasement,StartValue,CurrentValue)<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">values</span> (@seqName,@Increasement,@startValue,@startValue)    <br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">commit</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">transaction</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">goto</span> ProcEnd<br /><br />
LogError:<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">rollback</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">transaction</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">RAISERROR</span> (<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'Can
not create sequence with name %s, sequence exists already.'</span>,16,1,@seqName)<br /><br />
ProcEnd:<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">end</span></span>
        </p>
once we created the "object" (not really an object, more as a row in the sequence
table), lets get his current value<br /><p class="HighlightedCode"><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">create</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">procedure</span> dbo.sp_SequenceCurrVal<br />
( 
<br />
    @seqName     <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">varchar</span>(30)<br /><br />
)<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">as</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">begin</span><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">declare</span> @rows <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">begin</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">transaction</span><br />
    <span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--
Check if the sequence exists</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> @rows
=(    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">select</span><span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">count</span>(*) 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">from</span> sysSequences <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">where</span> SequenceName
= @SeqName)<br />
    <br />
    <span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--
if no such row</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">if</span> @rows
= 0 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">begin</span><br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">goto</span> LogError<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">end</span><br /><br />
    <span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--
Get the new value</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">select</span> CurrentValue <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">from</span> sysSequences<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">where</span> SequenceName
= @SeqName<br />
    <br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">commit</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">transaction</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">goto</span> ProcEnd<br /><br />
LogError:<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">rollback</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">transaction</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">RAISERROR</span> (<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'sequence
named %s, does not exists.'</span>,16,1,@seqName)<br /><br />
ProcEnd:<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">end</span></span></p><p>
and the "get next value" method (very similar to the get current procedure.)
</p><p class="HighlightedCode"><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">create</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">procedure</span> dbo.sp_SequenceNextVal<br />
( 
<br />
    @seqName     <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">varchar</span>(30)<br /><br />
)<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">as</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">begin</span><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">declare</span> @rows <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">begin</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">transaction</span><br />
    <span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--
Check if the sequence exists</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> @rows
=(    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">select</span><span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">count</span>(*) 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">from</span> sysSequences <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">where</span> SequenceName
= @SeqName)<br />
    <br />
    <span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--
if no such row</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">if</span> @rows
= 0 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">begin</span><br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">goto</span> LogError<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">end</span><br /><br /><br />
    <span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--
Update the sequence increasement</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">update</span> sysSequences<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SET</span> CurrentValue
= CurrentValue + Incrasement<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">where</span> SequenceName
= @SeqName<br /><br />
    <span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--
Get the new value</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">select</span> CurrentValue <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">from</span> sysSequences<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">where</span> SequenceName
= @SeqName<br />
    <br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">commit</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">transaction</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">goto</span> ProcEnd<br /><br />
LogError:<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">rollback</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">transaction</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">RAISERROR</span> (<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'sequence
named %s, does not exists.'</span>,16,1,@seqName)<br /><br />
ProcEnd:<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">end</span></span></p><p><br />
thats it, done.<br />
now we have some sql server sequences
</p><p>
 
</p><img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=847401fa-44b9-4eb4-b999-1d98669dc901" /><br /><hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>Sequence implementation in SQL server 2000</title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,847401fa-44b9-4eb4-b999-1d98669dc901.aspx</guid>
      <link>http://www.krokhmal.com/2007/07/18/SequenceImplementationInSQLServer2000.aspx</link>
      <pubDate>Wed, 18 Jul 2007 19:06:10 GMT</pubDate>
      <description>&lt;p&gt;
for those folks who worked with oracle, know that sequence is an internal database
object which has it's own methods and properties.&lt;br&gt;
&lt;strong&gt;hey, why shouldn't i use this wonderful auto number feature that sql server
has ?&lt;br&gt;
&lt;/strong&gt;well, auto number will give you a good solution to most of your needs that
a sequence provided in oracle.&lt;br&gt;
but, here is some conditions where auto number just won't suffice :
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Managing more than one "auto number" in one table - auto number can be defined only
at one field.&lt;/li&gt;
&lt;li&gt;
Managing the same auto number across multiple tables - the auto number can be asigned
only to a specified table.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
the solution is simple: 
&lt;br&gt;
we will create some framework that will allow us to manage sequences and work with
them.
&lt;/p&gt;
&lt;p&gt;
first we will create one table to contain the sequences.
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;CREATE&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;TABLE&lt;/span&gt; [dbo].[sysSequences]
(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[SequenceName] [&lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;char&lt;/span&gt;]
(30) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;COLLATE&lt;/span&gt; Hebrew_CI_AS &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NOT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NULL&lt;/span&gt; ,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[Incrasement] [&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;] &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NOT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NULL&lt;/span&gt; ,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[StartValue] [&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;] &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NOT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NULL&lt;/span&gt; ,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[CurrentValue] [&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;] &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NOT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NULL&lt;/span&gt; 
&lt;br&gt;
) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; [&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;PRIMARY&lt;/span&gt;]&lt;br&gt;
GO&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
next thing we need to do is create some stored procedures that will handle these sequences.
&lt;/p&gt;
&lt;p&gt;
the create sequence procedure :&lt;br&gt;
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;CREATE&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;procedure&lt;/span&gt; dbo.sp_Create_Sequence&lt;br&gt;
( 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@seqName &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;varchar&lt;/span&gt;(30),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Increasement &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@startValue&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;
&lt;br&gt;
)&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;as&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;begin&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;declare&lt;/span&gt; @r &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;begin&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;transaction&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--
Check if the sequence exists&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; @r
=(&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;select&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;count&lt;/span&gt;(*) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;from&lt;/span&gt; sysSequences &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;where&lt;/span&gt; SequenceName
= @SeqName)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt; @r
&amp;lt;&amp;gt; 0 &lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--if
error is raised&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;begin&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;goto&lt;/span&gt; LogError&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;end&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;insert&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;into&lt;/span&gt; sysSequences
(SequenceName,Incrasement,StartValue,CurrentValue)&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;values&lt;/span&gt; (@seqName,@Increasement,@startValue,@startValue)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;commit&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;transaction&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;goto&lt;/span&gt; ProcEnd&lt;br&gt;
&lt;br&gt;
LogError:&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;rollback&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;transaction&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;RAISERROR&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'Can
not create sequence with name %s, sequence exists already.'&lt;/span&gt;,16,1,@seqName)&lt;br&gt;
&lt;br&gt;
ProcEnd:&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
once we created the "object"&amp;nbsp;(not really an object, more as a row in the sequence
table), lets get his current value&lt;br&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;create&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;procedure&lt;/span&gt; dbo.sp_SequenceCurrVal&lt;br&gt;
( 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@seqName &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;varchar&lt;/span&gt;(30)&lt;br&gt;
&lt;br&gt;
)&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;as&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;begin&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;declare&lt;/span&gt; @rows &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;begin&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;transaction&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--
Check if the sequence exists&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; @rows
=(&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;select&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;count&lt;/span&gt;(*) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;from&lt;/span&gt; sysSequences &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;where&lt;/span&gt; SequenceName
= @SeqName)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--
if no such row&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt; @rows
= 0 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;begin&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;goto&lt;/span&gt; LogError&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;end&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--
Get the new value&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;select&lt;/span&gt; CurrentValue &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;from&lt;/span&gt; sysSequences&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;where&lt;/span&gt; SequenceName
= @SeqName&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;commit&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;transaction&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;goto&lt;/span&gt; ProcEnd&lt;br&gt;
&lt;br&gt;
LogError:&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;rollback&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;transaction&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;RAISERROR&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'sequence
named %s, does not exists.'&lt;/span&gt;,16,1,@seqName)&lt;br&gt;
&lt;br&gt;
ProcEnd:&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
and the "get next value" method (very similar to the get current procedure.)
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;create&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;procedure&lt;/span&gt; dbo.sp_SequenceNextVal&lt;br&gt;
( 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@seqName &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;varchar&lt;/span&gt;(30)&lt;br&gt;
&lt;br&gt;
)&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;as&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;begin&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;declare&lt;/span&gt; @rows &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;begin&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;transaction&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--
Check if the sequence exists&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; @rows
=(&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;select&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;count&lt;/span&gt;(*) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;from&lt;/span&gt; sysSequences &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;where&lt;/span&gt; SequenceName
= @SeqName)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--
if no such row&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt; @rows
= 0 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;begin&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;goto&lt;/span&gt; LogError&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;end&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--
Update the sequence increasement&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;update&lt;/span&gt; sysSequences&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SET&lt;/span&gt; CurrentValue
= CurrentValue + Incrasement&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;where&lt;/span&gt; SequenceName
= @SeqName&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--
Get the new value&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;select&lt;/span&gt; CurrentValue &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;from&lt;/span&gt; sysSequences&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;where&lt;/span&gt; SequenceName
= @SeqName&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;commit&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;transaction&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;goto&lt;/span&gt; ProcEnd&lt;br&gt;
&lt;br&gt;
LogError:&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;rollback&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;transaction&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;RAISERROR&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'sequence
named %s, does not exists.'&lt;/span&gt;,16,1,@seqName)&lt;br&gt;
&lt;br&gt;
ProcEnd:&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
thats it, done.&lt;br&gt;
now we have some sql server sequences
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=847401fa-44b9-4eb4-b999-1d98669dc901" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,847401fa-44b9-4eb4-b999-1d98669dc901.aspx</comments>
      <category> Sql Server </category>
      <category>Architectural solutions</category>
      <category>Sql Server 2000</category>
    </item>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=cf8c1f95-d0cd-4dc4-acdd-75355936f76c</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,cf8c1f95-d0cd-4dc4-acdd-75355936f76c.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,cf8c1f95-d0cd-4dc4-acdd-75355936f76c.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=cf8c1f95-d0cd-4dc4-acdd-75355936f76c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
No, I'm no retiring from the coding field...<br />
actually I'm in the opposite direction.<br />
the last few months I've been working on a new system thats about to go online in
the next few months.<br />
busy as hell in architecture issues, coding issues and more...<br />
i decided to take the opportunity of this project and Finlay get into the asp.net
2.0 .<br />
so i tried to do some coding here
</p>
        <p class="HighlightedCode">
          <span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;">Convert.ToDotNet2(Shimon);</span>
        </p>
        <p>
but that only returned an exception of InvalidCasting.<br />
so, i sat on my bottom and started to learn all that is to know to introduce myself
to the new features (yes, i know it's about time).
</p>
        <p>
from what i can see, asp.net 2.0 will introduce to the world a lot of "drag&amp;drop"
programmers.<br />
take for example the login controls that come built-in:<br /></p>
        <ul>
          <li>
it's great for the beginner programmer. 
</li>
          <li>
this is one well of a !#@#!!# for an architect.</li>
        </ul>
        <p>
i mean, they coupled a GUI to a "generic" DB structure, Why ???<br />
what if i want to manage my roles and permissions in other manner (say hierarchical
roles...),<br />
or just manage the user in my DB with all the other tables so i can do some easy data
mining ?<br />
to do that i need to write some providers to the controls that equals to implementing
the business logic from scratch.<br />
i will extent later on that matter (some other post...)
</p>
        <p>
building this project as a one man show gave me an oppertunity of a vast view on a
system besides playing with one module.<br />
from now on, i'll continue update here the progress of the project.
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=cf8c1f95-d0cd-4dc4-acdd-75355936f76c" />
        <br />
        <hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>Why did Shimon stoped posting ?</title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,cf8c1f95-d0cd-4dc4-acdd-75355936f76c.aspx</guid>
      <link>http://www.krokhmal.com/2007/06/09/WhyDidShimonStopedPosting.aspx</link>
      <pubDate>Sat, 09 Jun 2007 21:51:44 GMT</pubDate>
      <description>&lt;p&gt;
No, I'm no retiring from the coding field...&lt;br&gt;
actually I'm in the opposite direction.&lt;br&gt;
the last few months I've been working on a new system thats about to go online in
the next few months.&lt;br&gt;
busy as hell in architecture issues, coding issues and more...&lt;br&gt;
i decided to take the opportunity of this project and Finlay get into the asp.net
2.0 .&lt;br&gt;
so i tried to do some coding here
&lt;/p&gt;
&lt;p class="HighlightedCode"&gt;
&lt;span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"&gt;Convert.ToDotNet2(Shimon);&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
but that only returned an exception of InvalidCasting.&lt;br&gt;
so, i sat on my bottom and started to learn all that is to know to introduce myself
to the new features (yes, i know it's about time).
&lt;/p&gt;
&lt;p&gt;
from what i can see, asp.net 2.0 will introduce to the world a lot of "drag&amp;amp;drop"
programmers.&lt;br&gt;
take for example the login controls that come built-in:&lt;br&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
it's great for the beginner programmer. 
&lt;/li&gt;
&lt;li&gt;
this is one well of a !#@#!!# for an architect.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
i mean, they coupled a GUI to a "generic" DB structure, Why ???&lt;br&gt;
what if i want to manage my roles and permissions in other manner (say hierarchical
roles...),&lt;br&gt;
or just manage the user in my DB with all the other tables so i can do some easy data
mining ?&lt;br&gt;
to do that i need to write some providers to the controls that equals to implementing
the business logic from scratch.&lt;br&gt;
i will extent later on that matter (some other post...)
&lt;/p&gt;
&lt;p&gt;
building this project as a one man show gave me an oppertunity of a vast view on a
system besides playing with one module.&lt;br&gt;
from now on, i'll continue update here the progress of the project.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=cf8c1f95-d0cd-4dc4-acdd-75355936f76c" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,cf8c1f95-d0cd-4dc4-acdd-75355936f76c.aspx</comments>
      <category>.Net 2.0</category>
      <category>Architectural solutions</category>
      <category>Life</category>
      <category>NightDuck</category>
    </item>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=84b017d2-f4b3-4481-8847-221bf47dd26b</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,84b017d2-f4b3-4481-8847-221bf47dd26b.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,84b017d2-f4b3-4481-8847-221bf47dd26b.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=84b017d2-f4b3-4481-8847-221bf47dd26b</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
couple weeks ago, my friend and co-worker, <a href="http://www.human-debugger.net/">shani
raba</a>, presented me a problem.<br />
they have some sealed application as an .exe file that throws some exceptions and
crashes their application.
</p>
        <p>
So, i Thought that is a good idea to wrap the targeted application with reflection,
and catch the unhandled thrown exceptions.
</p>
        <p>
to demonstrate this,<br />
I've created an application with a button that throws an exception.
</p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> button1_Click(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> sender,
System.EventArgs e)<br />
{<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"my
Exception, need to be wrapped"</span>);<br />
}</span>
        </p>
        <p>
the next thing that we need to create is the wrapper.
</p>
        <p>
so, creating a console application with this code should have solved the problem:
</p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">[STAThread]<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br /><br />
    Assembly assembly <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Assembly.LoadFrom
(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"cashTester.exe"</span>);<br /><br />
    Type t <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> assembly.GetType(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"cashTester.Form1"</span>);<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> o <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Activator.CreateInstance(t);<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">try</span><br />
    {<br />
        Application.Run((Form)o); 
<br />
    }<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">catch</span>(Exception
ex)<br />
    {<br />
        Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"exception
was thrown : "</span><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">+</span> ex.Message);<br />
    }<br />
}</span>
        </p>
        <p>
running this code in debug mode successfully catch the exception from the winform.<br />
but, for some reason, in a normal run, this code won't catch the exception.
</p>
        <p>
makes you wonder, huh ?
</p>
        <p>
so i did some thinking, what on earth can make this phenomena ?<br />
The answer is : Threads.<br />
Yes, like it or not, but this is the subject that everyone tries to avoid it.<br />
everyone knows it exists, and no one really likes it,<br />
but we can't run from the problem, we need to confront it.
</p>
        <p>
So, what can we do ?
</p>
        <p>
since <font size="2">Application.Run launches a new thread, </font>we can add to the
Application.ThreadException event handler, an exception handling method.<br /></p>
        <p>
like this class :
</p>
        <p>
 
</p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
The Wrapper class</span>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Wrapper<br />
{<br /><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Public cunstructor</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Wrapper()<br />
    {<br />
    }<br /><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This function will initialize the exception handling</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Init()<br />
    {<br />
        <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
define handlers for unhandled exceptions</span><br />
        AppDomain.CurrentDomain.UnhandledException
+= <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> UnhandledExceptionEventHandler(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">this</span>.exp);<br />
        Application.ThreadException +=<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> System.Threading.ThreadExceptionEventHandler(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">this</span>.ThreadExp);<br />
    }<br /><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method is for the threads exceptions</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="o"&gt;the object&lt;/param&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="args"&gt;Thread exception args&lt;/param&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> ThreadExp(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> o,
System.Threading.ThreadExceptionEventArgs args)<br />
    {<br />
        <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
Write the message to the console</span><br />
        Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Unhandled
thread exception was thrown : "</span><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">+</span> args.Exception.Message);<br />
    }<br /><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method is for the unhandled exceptions from the main thread</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="o"&gt;the object&lt;/param&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="args"&gt;exception arguments&lt;/param&gt;</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> exp(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> o,System.UnhandledExceptionEventArgs
args)<br />
    {<br />
        <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
Write the message to the console</span><br />
        Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Unhandled
exception was thrown : "</span><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">+</span> ((Exception)args.ExceptionObject).Message);<br />
    }<br /><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Run()<br />
    {<br />
        <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
Load the assembly</span><br />
        Assembly assembly <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Assembly.LoadFrom
(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"cashTester.exe"</span>);<br /><br />
        <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
get the type of the object</span><br />
        Type t <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> assembly.GetType(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"cashTester.Form1"</span>);<br /><br />
        <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
invoke it</span><br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> o <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Activator.CreateInstance(t);<br /><br />
        <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
Run the application - note that this line starts an additional thread</span><br />
        Application.Run((Form)o);<br />
    }<br />
}</span>
        </p>
        <p>
 
</p>
        <p>
now, all we need is to launch it :
</p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
The main entry point for the application.</span>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <br />
[STAThread]<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br />
    Wrapper w <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Wrapper();<br />
    w.Init();<br />
    w.Run();<br />
}</span>
        </p>
        <p>
 
</p>
        <p>
now we got an exception wrapper for launching applications.
</p>
        <p>
          <a href="http://www.human-debugger.net/">Shani</a>, tell me if that helped ...
</p>
        <p>
p.s.<br />
There are more ways to do it, but this is the simplest one
</p>
        <img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=84b017d2-f4b3-4481-8847-221bf47dd26b" />
        <br />
        <hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>Wrapping Unhandled exceptions from a WinForm application</title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,84b017d2-f4b3-4481-8847-221bf47dd26b.aspx</guid>
      <link>http://www.krokhmal.com/2007/03/04/WrappingUnhandledExceptionsFromAWinFormApplication.aspx</link>
      <pubDate>Sun, 04 Mar 2007 18:35:22 GMT</pubDate>
      <description>&lt;p&gt;
couple weeks ago, my friend and co-worker, &lt;a href="http://www.human-debugger.net/"&gt;shani
raba&lt;/a&gt;,&amp;nbsp;presented me a problem.&lt;br&gt;
they have some sealed application as an .exe file that throws some exceptions and
crashes their application.
&lt;/p&gt;
&lt;p&gt;
So, i Thought that is a good idea to wrap the targeted application with reflection,
and catch the unhandled thrown exceptions.
&lt;/p&gt;
&lt;p&gt;
to demonstrate this,&lt;br&gt;
I've created an application with a button that throws an exception.
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; button1_Click(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; sender,
System.EventArgs e)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"my
Exception, need to be wrapped"&lt;/span&gt;);&lt;br&gt;
}&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
the next thing that we need to create is the wrapper.
&lt;/p&gt;
&lt;p&gt;
so, creating a console application with this code should have solved the problem:
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;[STAThread]&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assembly assembly &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Assembly.LoadFrom
(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"cashTester.exe"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type t &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; assembly.GetType(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"cashTester.Form1"&lt;/span&gt;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; o &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Activator.CreateInstance(t);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;try&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Application.Run((Form)o); 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;catch&lt;/span&gt;(Exception
ex)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"exception
was thrown : "&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;+&lt;/span&gt; ex.Message);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
}&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
running this code in debug mode successfully catch the exception from the winform.&lt;br&gt;
but, for some reason, in a normal run, this code won't catch the exception.
&lt;/p&gt;
&lt;p&gt;
makes you wonder, huh ?
&lt;/p&gt;
&lt;p&gt;
so i did some thinking, what on earth can make this phenomena ?&lt;br&gt;
The answer is : Threads.&lt;br&gt;
Yes, like it or not, but this is the subject that everyone tries to avoid it.&lt;br&gt;
everyone knows it exists, and no one really likes it,&lt;br&gt;
but we can't run from the problem, we need to confront it.
&lt;/p&gt;
&lt;p&gt;
So, what can we do ?
&lt;/p&gt;
&lt;p&gt;
since &lt;font size=2&gt;Application.Run launches a new thread, &lt;/font&gt;we can add to the
Application.ThreadException event handler, an exception handling method.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
like this class :
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
The Wrapper class&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Wrapper&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Public cunstructor&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Wrapper()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This function will initialize the exception handling&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Init()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
define handlers for unhandled exceptions&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AppDomain.CurrentDomain.UnhandledException
+= &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; UnhandledExceptionEventHandler(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.exp);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Application.ThreadException +=&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; System.Threading.ThreadExceptionEventHandler(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.ThreadExp);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method is for the threads exceptions&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="o"&amp;gt;the object&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="args"&amp;gt;Thread exception args&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; ThreadExp(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; o,
System.Threading.ThreadExceptionEventArgs args)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
Write the message to the console&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Unhandled
thread exception was thrown : "&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;+&lt;/span&gt; args.Exception.Message);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method is for the unhandled exceptions from the main thread&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="o"&amp;gt;the object&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="args"&amp;gt;exception arguments&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; exp(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; o,System.UnhandledExceptionEventArgs
args)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
Write the message to the console&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Unhandled
exception was thrown : "&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;+&lt;/span&gt; ((Exception)args.ExceptionObject).Message);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Run()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
Load the assembly&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assembly assembly &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Assembly.LoadFrom
(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"cashTester.exe"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
get the type of the object&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type t &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; assembly.GetType(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"cashTester.Form1"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
invoke it&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; o &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Activator.CreateInstance(t);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
Run the application - note that this line starts an additional thread&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Application.Run((Form)o);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
}&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
now, all we need is to launch it :
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
The main entry point for the application.&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;br&gt;
[STAThread]&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Wrapper w &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Wrapper();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;w.Init();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;w.Run();&lt;br&gt;
}&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
now we got an exception wrapper for launching applications.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.human-debugger.net/"&gt;Shani&lt;/a&gt;, tell me if that helped ...
&lt;/p&gt;
&lt;p&gt;
p.s.&lt;br&gt;
There are more ways to do it, but this is the simplest one
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=84b017d2-f4b3-4481-8847-221bf47dd26b" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,84b017d2-f4b3-4481-8847-221bf47dd26b.aspx</comments>
      <category>.Net</category>
      <category>Architectural solutions</category>
      <category>Threading</category>
    </item>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=c3d700fe-310b-4350-96ba-86cdade9d364</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,c3d700fe-310b-4350-96ba-86cdade9d364.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,c3d700fe-310b-4350-96ba-86cdade9d364.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=c3d700fe-310b-4350-96ba-86cdade9d364</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Yesterday, good friend of mine, Lev rosenblit, asked me a good question.<br />
what are the life cycle of static objects in an aspx page.<br />
so, at first without any hesitation, i answered that the object will die after the
request event ends.<br />
the dude insists that I'm wrong on that matter, so i decided to check it out .
</p>
        <p>
here is some code snippet to check it out :
</p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">        private</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> myStaticInt <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;<br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Page_Load(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> sender,
System.EventArgs e)<br />
        {<br />
            myStaticInt++;<br />
            Response.Write(myStaticInt);<br />
        }</span>
        </p>
        <p>
by theory (which is objects in an aspx page die after the end of the request), this
code should print the number "1" on each request.<br />
but the result was different, on each request, the result increased.
</p>
        <p>
digging a little on the web, i found that static objects live inside the App domain
and not in the page context.<br />
This interesting fact raised some interesting question, an architectural one:<br />
what would happen if the code run in NLB configuration (Network Load Balancing)?
</p>
        <p>
on that matter their is a session state issue which is solved by getting the session
state from a shared database,<br />
but what about the static objects that live inside the App domain ? 
<br />
what can be done to share those object ?
</p>
        <p>
from my point of view, this is a bad choice in architecture when you choosing to use
static objects inside your page.<br />
thus, it won't always work, and will be hardware Dependant system.
</p>
        <img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=c3d700fe-310b-4350-96ba-86cdade9d364" />
        <br />
        <hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>Static objects in aspx page - Bad idea</title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,c3d700fe-310b-4350-96ba-86cdade9d364.aspx</guid>
      <link>http://www.krokhmal.com/2007/03/04/StaticObjectsInAspxPageBadIdea.aspx</link>
      <pubDate>Sun, 04 Mar 2007 12:17:48 GMT</pubDate>
      <description>&lt;p&gt;
Yesterday, good friend of mine, Lev rosenblit, asked me a good question.&lt;br&gt;
what are the life cycle of static objects in an aspx page.&lt;br&gt;
so, at first without any hesitation, i answered that the object will die after the
request event ends.&lt;br&gt;
the dude insists that I'm wrong on that matter, so i decided to check it out .
&lt;/p&gt;
&lt;p&gt;
here is some code snippet to check it out :
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; myStaticInt &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Page_Load(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; sender,
System.EventArgs e)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;myStaticInt++;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Response.Write(myStaticInt);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
by theory (which is objects in an aspx page die after the end of the request), this
code should print the number "1" on each request.&lt;br&gt;
but the result was different, on each request, the result increased.
&lt;/p&gt;
&lt;p&gt;
digging a little on the web, i found that static objects live inside the App domain
and not in the page context.&lt;br&gt;
This interesting fact raised some interesting question, an architectural one:&lt;br&gt;
what would happen if the code run in NLB configuration (Network Load Balancing)?
&lt;/p&gt;
&lt;p&gt;
on that matter their is a session state issue which is solved by getting the session
state from a shared database,&lt;br&gt;
but what about the static objects that live inside the App domain ? 
&lt;br&gt;
what can be done to share those object ?
&lt;/p&gt;
&lt;p&gt;
from my point of view, this is a bad choice in architecture when you choosing to use
static objects inside your page.&lt;br&gt;
thus, it won't always work, and will be hardware Dependant system.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=c3d700fe-310b-4350-96ba-86cdade9d364" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,c3d700fe-310b-4350-96ba-86cdade9d364.aspx</comments>
      <category>.Net</category>
      <category>Architectural solutions</category>
    </item>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=16b22ae9-7971-4b75-ac7a-78d5e25b27f2</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,16b22ae9-7971-4b75-ac7a-78d5e25b27f2.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,16b22ae9-7971-4b75-ac7a-78d5e25b27f2.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=16b22ae9-7971-4b75-ac7a-78d5e25b27f2</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Well,<br />
after months I'm talking about it and couple of sleepless nights to prepare it, here
it is.
</p>
        <p>
So, what do have here ?
</p>
        <p>
1 powerPoint presentation that explains what is Session Hijacking and XSS.<br />
1 XSS demo.<br />
0 Session hijacking Demo .... (why you ask ? - I'm planning to do another lecture
on the topic for some those who missed it yesterday...)<br />
0 Code included. (like the demos ....will be uploaded some time soon....)
</p>
        <p>
long story short :<br />
this presentation talks about the threats in Session hijacking.<br />
and how to prevent it. - while its not a 100% solution, it certainly raises the bar
for hackers to succeed in the session hijacking attack.
</p>
        <p>
some wise man said to me once,<br />
"for every defense that ever made, someone thought how to break it,<br />
 the only thing we can do, is to narrow down the amount of people that is capable
to perform it."
</p>
        <p>
enjoy.
</p>
        <p>
comments are welcomed :)
</p>
        <p>
          <a href="http://www.krokhmal.com/content/binary/Security.pps">Security.pps (324 KB)</a>
        </p>
        <p>
 
</p>
        <object width="800" height="600">
          <param name="movie" value="http://www.youtube.com/v/TMPnyyrPryc" />
          <embed src="http://www.youtube.com/v/TMPnyyrPryc" type="application/x-shockwave-flash" width="800" height="600">
          </embed>
        </object>
        <img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=16b22ae9-7971-4b75-ac7a-78d5e25b27f2" />
        <br />
        <hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>Session hijacking  - prevention oriented</title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,16b22ae9-7971-4b75-ac7a-78d5e25b27f2.aspx</guid>
      <link>http://www.krokhmal.com/2007/03/02/SessionHijackingPreventionOriented.aspx</link>
      <pubDate>Fri, 02 Mar 2007 13:32:40 GMT</pubDate>
      <description>&lt;p&gt;
Well,&lt;br&gt;
after months I'm talking about it and couple of sleepless nights to prepare it, here
it is.
&lt;/p&gt;
&lt;p&gt;
So, what do have here ?
&lt;/p&gt;
&lt;p&gt;
1 powerPoint presentation that explains what is Session Hijacking and XSS.&lt;br&gt;
1 XSS demo.&lt;br&gt;
0 Session hijacking Demo .... (why you ask ? - I'm planning to do another lecture
on the topic for some those who missed it yesterday...)&lt;br&gt;
0 Code included. (like the demos ....will be uploaded some time soon....)
&lt;/p&gt;
&lt;p&gt;
long story short :&lt;br&gt;
this presentation talks about the threats in Session hijacking.&lt;br&gt;
and how to prevent it. - while its not a 100% solution, it certainly raises the bar
for hackers to succeed in the session hijacking attack.
&lt;/p&gt;
&lt;p&gt;
some wise man said to me once,&lt;br&gt;
"for every defense that ever made, someone thought how to break it,&lt;br&gt;
&amp;nbsp;the only thing we can do, is to narrow down the amount of people that is capable
to perform it."
&lt;/p&gt;
&lt;p&gt;
enjoy.
&lt;/p&gt;
&lt;p&gt;
comments are welcomed :)
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.krokhmal.com/content/binary/Security.pps"&gt;Security.pps (324 KB)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;object width="800" height="600"&gt;
&lt;param name="movie" value="http://www.youtube.com/v/TMPnyyrPryc"&gt;&gt;&lt;embed src="http://www.youtube.com/v/TMPnyyrPryc" type="application/x-shockwave-flash" width="800" height="600"&gt;&lt;/embed&gt;
&lt;/object&gt;&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=16b22ae9-7971-4b75-ac7a-78d5e25b27f2" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,16b22ae9-7971-4b75-ac7a-78d5e25b27f2.aspx</comments>
      <category>.Net</category>
      <category>Architectural solutions</category>
      <category>Lessons</category>
      <category>Security</category>
      <category>XSS</category>
    </item>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=5122c2fc-233d-4f82-9fab-3c7739efbc88</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,5122c2fc-233d-4f82-9fab-3c7739efbc88.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,5122c2fc-233d-4f82-9fab-3c7739efbc88.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=5122c2fc-233d-4f82-9fab-3c7739efbc88</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
an Interesting article I've read about the next .net framework,<br />
apparently the new framework wont contain such drastic changes like .Net 1.1 VS 2.0
at the CLR or at the language features,<br />
but, instead it will bring some architectural solutions for different tasks such as
distributed programming, authentication, presentation and probably many more.
</p>
        <p>
check out <a href="http://www.informit.com/articles/article.asp?p=659520&amp;seqNum=1&amp;rl=1">this
article </a>about the issue
</p>
        <img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=5122c2fc-233d-4f82-9fab-3c7739efbc88" />
        <br />
        <hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>.Net 3.0 - an architectural update for the .Net framework</title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,5122c2fc-233d-4f82-9fab-3c7739efbc88.aspx</guid>
      <link>http://www.krokhmal.com/2006/10/16/Net30AnArchitecturalUpdateForTheNetFramework.aspx</link>
      <pubDate>Mon, 16 Oct 2006 20:42:27 GMT</pubDate>
      <description>&lt;p&gt;
an Interesting article I've read about the next .net framework,&lt;br&gt;
apparently the new framework wont contain such drastic changes like .Net 1.1 VS 2.0
at the CLR or at the language features,&lt;br&gt;
but, instead it will bring some architectural solutions for different tasks such as
distributed programming, authentication, presentation and probably many more.
&lt;/p&gt;
&lt;p&gt;
check out &lt;a href="http://www.informit.com/articles/article.asp?p=659520&amp;amp;seqNum=1&amp;amp;rl=1"&gt;this
article &lt;/a&gt;about the issue
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=5122c2fc-233d-4f82-9fab-3c7739efbc88" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,5122c2fc-233d-4f82-9fab-3c7739efbc88.aspx</comments>
      <category>.Net</category>
      <category>Architectural solutions</category>
    </item>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=eb16227a-d2aa-40af-9d45-aace858ffb8e</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,eb16227a-d2aa-40af-9d45-aace858ffb8e.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,eb16227a-d2aa-40af-9d45-aace858ffb8e.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=eb16227a-d2aa-40af-9d45-aace858ffb8e</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
when planning a software solution, one of the most important things is to design the
solution the best way it can be,<br />
even before writing the first line of code, we need to know exactly how we should
implement it.
</p>
        <p>
so what would you do if you never planned/worked/implemented such type of a solution
?
</p>
        <p>
needless to say that inventing the wheel all over again will be unnecessary ,<br />
first you consult with the "elders" (the more experienced co-workers),<br />
trying maybe google up the solution,<br />
going over the references and the guide lines in developing this kind of a solution
or at the given platform,<br />
starting some thinking team and so ...
</p>
        <p>
finally, you came up with a solution, and....<br />
start implementing it.
</p>
        <p>
now, in a perfect world, once you did the steps mentioned above, you have a perfect
solution.<br />
but in the real world, something must to go wrong.
</p>
        <p>
recently i came across some interesting example of this phenomena.<br />
while designing a "file Version manager solution" on SPS (Share point Server) platform,<br />
following the SPS development guidelines, which says that you (the developer), should <strong>never </strong>approach
the SPS database.<br /><strong>always</strong>, but <strong>always </strong>get the data through the object
model.<br /><br />
so, by doing that, you came up with a working application,but..... catastrophic performance.<br />
after consulting with other staff, we came to the conclusion that the bottleneck is
in the object model itself.<br />
so the only thing we had to do is the direct approach to the database ( a big NO NO
on the guide lines).<br />
and apparently the performance graph suddenly got a dramatic change (better performance).
</p>
        <p>
i think that guide lines did not meant to become laws, but to be just as a suggestions
to most of the cases.<br />
what do you think about this issue ?
</p>
        <img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=eb16227a-d2aa-40af-9d45-aace858ffb8e" />
        <br />
        <hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>Does following Microsoft guidelines(or any guidelines..), is Always a good idea ?</title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,eb16227a-d2aa-40af-9d45-aace858ffb8e.aspx</guid>
      <link>http://www.krokhmal.com/2006/08/17/DoesFollowingMicrosoftGuidelinesorAnyGuidelinesIsAlwaysAGoodIdea.aspx</link>
      <pubDate>Thu, 17 Aug 2006 20:54:25 GMT</pubDate>
      <description>&lt;p&gt;
when planning a software solution, one of the most important things is to design the
solution the best way it can be,&lt;br&gt;
even before writing the first line of code, we need to know exactly how we should
implement it.
&lt;/p&gt;
&lt;p&gt;
so what would you do if you never planned/worked/implemented such type of a solution
?
&lt;/p&gt;
&lt;p&gt;
needless to say that inventing the wheel all over again will be unnecessary ,&lt;br&gt;
first you consult with the "elders" (the more experienced co-workers),&lt;br&gt;
trying maybe google up the solution,&lt;br&gt;
going over the references and the guide lines in developing this kind of a solution
or at the given platform,&lt;br&gt;
starting some thinking team and so ...
&lt;/p&gt;
&lt;p&gt;
finally, you came up with a solution, and....&lt;br&gt;
start implementing it.
&lt;/p&gt;
&lt;p&gt;
now, in a perfect world, once you&amp;nbsp;did the steps mentioned above, you have a perfect
solution.&lt;br&gt;
but in the real world, something must to go wrong.
&lt;/p&gt;
&lt;p&gt;
recently i came across some interesting example of this phenomena.&lt;br&gt;
while designing a "file Version manager solution" on SPS (Share point Server) platform,&lt;br&gt;
following the SPS development guidelines, which says that you (the developer), should &lt;strong&gt;never &lt;/strong&gt;approach
the SPS database.&lt;br&gt;
&lt;strong&gt;always&lt;/strong&gt;, but &lt;strong&gt;always &lt;/strong&gt;get the data through the object
model.&lt;br&gt;
&lt;br&gt;
so, by doing that, you came up with a working application,but..... catastrophic performance.&lt;br&gt;
after consulting with other staff, we came to the conclusion that the bottleneck is
in the object model itself.&lt;br&gt;
so the only thing we had to do is the direct approach to the database ( a big NO NO
on the guide lines).&lt;br&gt;
and apparently the performance graph suddenly got a&amp;nbsp;dramatic change (better performance).
&lt;/p&gt;
&lt;p&gt;
i think that guide lines did not meant to become laws, but to be just as a suggestions
to most of the cases.&lt;br&gt;
what do you think about this issue ?
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=eb16227a-d2aa-40af-9d45-aace858ffb8e" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,eb16227a-d2aa-40af-9d45-aace858ffb8e.aspx</comments>
      <category>.Net</category>
      <category>Architectural solutions</category>
      <category>Microsoft/SPS (sharepoint server)</category>
      <category>Microsoft</category>
    </item>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=8d51eb7c-3975-4fcf-a2e8-75c8b1443661</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,8d51eb7c-3975-4fcf-a2e8-75c8b1443661.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,8d51eb7c-3975-4fcf-a2e8-75c8b1443661.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=8d51eb7c-3975-4fcf-a2e8-75c8b1443661</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
remember this neat syntax that exists in oracle database for hierarchy selects ?<br />
actually , its pretty simple :<br /><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span> last_name,
employee_id, manager_id, LEVEL<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> employees<br /><font color="#0000ff">START</font><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WITH</span> employee_id
= 100<br /><font color="#0000ff">CONNECT <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">BY</span> PRIOR</font> employee_id
= manager_id;</span><br /><br />
ever tried to do the same in sql server ?<br />
well, these kind of syntax just does not exist,<br />
we need to work very hard to create such a feature in our database.<br /></p>
        <p>
so, what do we have:
</p>
        <ul>
          <li>
a table that contains entities 
</li>
          <li>
each entity connected to some father entity 
</li>
          <li>
each connection describes a "father - son" relation between the two entities</li>
        </ul>
        <p>
lets not forget the things we need to relay on , when implementing :
</p>
        <ul>
          <li>
what will happen when we will delete the father of some sub tree ? 
</li>
          <li>
what should we do when we update/add a record ? 
</li>
          <li>
how will we select the data ? 
</li>
          <li>
algorithm efficiency is crucial, if we will need to wait 5 minutes for the data, it's
not worth it</li>
        </ul>
        <p>
the first (but apparently the worst) idea that came to my mind is recursion<br />
lets look at this table :
</p>
        <p>
 
</p>
        <table border="1">
          <tbody>
            <tr>
              <td>
                <strong>EmployeeID</strong>
              </td>
              <td>
                <strong>Name</strong>
              </td>
              <td>
                <strong>BossID</strong>
              </td>
            </tr>
            <tr>
              <td>
                <p align="center">
1
</p>
              </td>
              <td>
                <p align="center">
shimon
</p>
              </td>
              <td>
                <p align="center">
NULL
</p>
              </td>
            </tr>
            <tr>
              <td>
                <p align="center">
2
</p>
              </td>
              <td>
                <p align="center">
yossi
</p>
              </td>
              <td>
                <p align="center">
1
</p>
              </td>
            </tr>
            <tr>
              <td>
                <p align="center">
3
</p>
              </td>
              <td>
                <p align="center">
Gaby
</p>
              </td>
              <td>
                <p align="center">
1
</p>
              </td>
            </tr>
            <tr>
              <td>
                <p align="center">
4
</p>
              </td>
              <td>
                <p align="center">
koby
</p>
              </td>
              <td>
                <p align="center">
3
</p>
              </td>
            </tr>
            <tr>
              <td>
                <p align="center">
5
</p>
              </td>
              <td>
                <p align="center">
jack
</p>
              </td>
              <td>
                <p align="center">
3
</p>
              </td>
            </tr>
          </tbody>
        </table>
        <p>
 
</p>
        <p>
we have we that the employee shimon is the "big boss" (because there is no other boss
above him),<br />
under shimon we have the employees Gaby and yossi,<br />
and under Gaby, we have another 2 employees : Koby and jack
</p>
        <p>
the recursive solution is to write some stored procedure that will receive the employeeID
and return as a data-Table the results<br />
i will not add the code for this solution and surely will not recommend it because
it was many problems :
</p>
        <ul>
          <li>
for each record we received as a descendant , 
<br />
we need to run with the function and get her descendants, 
<br />
and so on, until there are no descendants for the node 
</li>
          <li>
we are limited to 32 levels of hierarchy 
</li>
          <li>
the runtime will depend on the row count that is in the table (we will need to run
on each of the rows one at the time) 
</li>
          <li>
the run on the node will look like this :<br /><img height="200" alt="heirarchy.jpg" src="http://www.krokhmal.com/Blog/content/binary/heirarchy.jpg" width="300" border="0" /><br /><br /></li>
        </ul>
        <p>
by the way, the most common way that I've seen to select hierarchical structure,<br />
is simply by setting a join between the levels in the select query.<br />
for example :
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span> TopBoss.Name
TopBoss, Boss.Name Boss, Employees.Name Employee<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> Employees 
<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">INNER</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">JOIN</span> Employees <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AS</span> Boss <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> Employees.BossID=Boss.EmployeeID<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">INNER</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">JOIN</span> Employees
TopBoss <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> Boss.BossID=TopBoss.EmployeeID</span>
        </p>
        <p>
this apply to the selection of three levels
</p>
        <p>
For each level, you'd need to join the table to itself...not an attractive option
if you have 5 or more levels ,<br />
you don't know how many levels you will have to select, there is no way can control
it!<br />
It would be great if it could join itself as many times as needed. This is called
a recursive join, and though some database products support it (Oracle has the CONNECT
BY syntax) SQL Server is not one of them.
</p>
        <p>
the other way is based on a thread that i read <a href="http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=2828">here</a> about
hierarchies,<br /></p>
        <p>
lets create a table :<br /></p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">CREATE</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">TABLE</span> Tree
(<br />
Node <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NOT</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NULL</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">IDENTITY</span>(100,
1),<br />
ParentNode <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>, 
<br />
EmployeeID <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NOT</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NULL</span>, 
<br />
Depth <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">tinyint</span>,<br />
Lineage <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">varchar</span>(255)
)</span>
        </p>
        <p>
the extra fields that has been added are the "<strong>lineage</strong>" and the "<strong>depth</strong>"
</p>
        <ul>
          <li>
Depth - for saving the current depth of the record in the hierarchy 
</li>
          <li>
Lineage - for saving all the ancestors of the record as a concatenated string</li>
        </ul>
        <p>
after filling the needed data for relations, the table looks like this :
</p>
        <p>
          <table cellspacing="0" cellpadding="2" border="1">
            <tbody>
              <tr>
                <td class="head">
Node</td>
                <td class="head">
ParentNode</td>
                <td class="head">
EmployeeID</td>
                <td class="head">
Depth</td>
                <td class="head">
Lineage</td>
              </tr>
              <tr>
                <td class="n">
100</td>
                <td class="n">
NULL</td>
                <td class="n">
1001</td>
                <td class="n">
NULL</td>
                <td class="n">
NULL</td>
              </tr>
              <tr>
                <td class="n">
101</td>
                <td class="n">
100</td>
                <td class="n">
1002</td>
                <td class="n">
NULL</td>
                <td class="n">
NULL</td>
              </tr>
              <tr>
                <td class="n">
102</td>
                <td class="n">
101</td>
                <td class="n">
1003</td>
                <td class="n">
NULL</td>
                <td class="n">
NULL</td>
              </tr>
              <tr>
                <td class="n">
103</td>
                <td class="n">
102</td>
                <td class="n">
1004</td>
                <td class="n">
NULL</td>
                <td class="n">
NULL</td>
              </tr>
              <tr>
                <td class="n">
104</td>
                <td class="n">
102</td>
                <td class="n">
1005</td>
                <td class="n">
NULL</td>
                <td class="n">
NULL</td>
              </tr>
              <tr>
                <td class="n">
105</td>
                <td class="n">
102</td>
                <td class="n">
1006</td>
                <td class="n">
NULL</td>
                <td class="n">
NULL</td>
              </tr>
            </tbody>
          </table>
        </p>
        <p>
The next part is to find the root node of the tree, also known as the top-level, etc. 
<br />
That's the node that has no parent (Null), so we will start there and set the Lineage
column as the root: 
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">UPDATE</span> Tree <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SET</span> Lineage=<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'/'</span>,
Depth=0 <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> ParentNode <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Is</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Null</span></span>
        </p>
        <p>
Once we did that, 
<br />
we can then update the rows who are the descendant of the root node:
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHILE</span>
            <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">EXISTS</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span> * <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> Tree <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> Depth <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Is</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Null</span>) <br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">   UPDATE</span> T <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SET</span> T.depth
= P.Depth + 1, <br />
   T.Lineage = P.Lineage + <span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Ltrim</span>(<span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Str</span>(T.ParentNode,6,0))
+ <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'/'</span> <br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">   FROM</span> Tree <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AS</span> T <br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">   INNER</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">JOIN</span> Tree <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AS</span> P <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> (T.ParentNode=P.Node) <br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">   WHERE</span> P.Depth&gt;=0 <br /><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">   AND</span> P.Lineage <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Is</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Not</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Null</span> <br /><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">   AND</span> T.Depth <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Is</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Null</span></span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <font face="Verdana" color="#003300" size="2">
              </font>
            </span>
          </span> 
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <font face="Verdana" color="#003300" size="2">this
loop will run once for each level of the hierarchy (<strong>not for each node as the
recursion method</strong>.) 
<br />
so, with data representation of 10,000 records with 8 levels of hierarchy, 
<br />
this code will run only 8 times to populate the needed data of the "lineage" field
and the "depth" field, and this "heavy" procedure will happen only once at the setup.<br />
the table should look like this after the given operation : 
<br /><br /><br /></font>
            </span>
          </span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <font face="Verdana" color="#003300" size="2">
                <br />
              </font>
            </span>
          </span>
        </p>
        <table cellspacing="0" cellpadding="2" border="1">
          <tbody>
            <tr>
              <td class="head">
Node</td>
              <td class="head">
ParentNode</td>
              <td class="head">
EmployeeID</td>
              <td class="head">
Depth</td>
              <td class="head">
Lineage</td>
            </tr>
            <tr>
              <td class="n">
100</td>
              <td class="n">
NULL</td>
              <td class="n">
1001</td>
              <td class="n">
0</td>
              <td class="n">
/</td>
            </tr>
            <tr>
              <td class="n">
101</td>
              <td class="n">
100</td>
              <td class="n">
1002</td>
              <td class="n">
1</td>
              <td class="n">
/100/</td>
            </tr>
            <tr>
              <td class="n">
102</td>
              <td class="n">
101</td>
              <td class="n">
1003</td>
              <td class="n">
2</td>
              <td class="n">
/100/101/</td>
            </tr>
            <tr>
              <td class="n">
103</td>
              <td class="n">
102</td>
              <td class="n">
1004</td>
              <td class="n">
3</td>
              <td class="n">
/100/101/102/</td>
            </tr>
            <tr>
              <td class="n">
104</td>
              <td class="n">
102</td>
              <td class="n">
1005</td>
              <td class="n">
3</td>
              <td class="n">
/100/101/102/</td>
            </tr>
            <tr>
              <td class="n">
105</td>
              <td class="n">
102</td>
              <td class="n">
1006</td>
              <td class="n">
3</td>
              <td class="n">
/100/101/102/</td>
            </tr>
          </tbody>
        </table>
        <p>
 
</p>
        <p>
You'll notice that for each node, the entire lineage back to the root is stored. This
means that finding someone's boss, or their boss' boss, doesn't require any self-joins
or recursion to create an indented list. In fact, it can be accomplished with a single
SELECT.
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span>
            <span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Space</span>(T.Depth*2)
+ E.Name <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AS</span> Name<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> Employees
E 
<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">INNER</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">JOIN</span> Tree
T <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> E.EmployeeID=T.EmployeeID<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ORDER</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">BY</span> T.Lineage
+ <span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Ltrim</span>(<span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Str</span>(T.Node,6,0))</span>
        </p>
        <p>
 
</p>
        <p>
maintaining the table is really not a big deal if we will use triggers.<br />
think about the new inserted record as the row that has not been filled in the setup
process.<br />
so the insert trigger should be : 
<br /><br /></p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">UPDATE</span> T <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SET</span> T.depth
= P.Depth + 1, 
<br />
T.Lineage = P.Lineage + <span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Ltrim</span>(<span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Str</span>(T.ParentNode,6,0))
+ <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'/'</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> Tree <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AS</span> T 
<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">INNER</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">JOIN</span> Tree <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AS</span> P <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> (T.ParentNode=P.Node) 
<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> P.Depth&gt;=0 
<br /><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AND</span> P.Lineage <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Is</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Not</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Null</span><br /><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AND</span> T.Depth <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Is</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Null</span><br /></span>
        </p>
        <p>
 
</p>
        <p>
the update trigger should do pretty much the same : building the 2 extra field all
over again.
</p>
        <p>
suggestions and request will be repplied :)
</p>
        <img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=8d51eb7c-3975-4fcf-a2e8-75c8b1443661" />
        <br />
        <hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>hierarchy implementation in SQL server 2000</title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,8d51eb7c-3975-4fcf-a2e8-75c8b1443661.aspx</guid>
      <link>http://www.krokhmal.com/2006/08/16/hierarchyImplementationInSQLServer2000.aspx</link>
      <pubDate>Wed, 16 Aug 2006 22:47:18 GMT</pubDate>
      <description>&lt;p&gt;
remember this neat syntax that exists in oracle database for hierarchy selects ?&lt;br&gt;
actually , its pretty simple :&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; last_name,
employee_id, manager_id, LEVEL&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; employees&lt;br&gt;
&lt;font color=#0000ff&gt;START&lt;/font&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WITH&lt;/span&gt; employee_id
= 100&lt;br&gt;
&lt;font color=#0000ff&gt;CONNECT &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;BY&lt;/span&gt; PRIOR&lt;/font&gt; employee_id
= manager_id;&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
ever tried to do the same in sql server ?&lt;br&gt;
well, these kind of syntax just does not exist,&lt;br&gt;
we need to work very hard to create such a feature in our database.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
so, what do we have:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
a table that contains entities 
&lt;li&gt;
each entity connected to some father entity 
&lt;li&gt;
each connection describes a "father - son" relation between the two entities&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
lets not forget the things we need to relay on , when implementing :
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
what will happen when we will delete the father of some sub tree ? 
&lt;li&gt;
what should we do when we update/add a record ? 
&lt;li&gt;
how will we select the data ? 
&lt;li&gt;
algorithm efficiency is crucial, if we will need to wait 5 minutes for the data, it's
not worth it&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
the first (but apparently the worst) idea that came to my mind is recursion&lt;br&gt;
lets look at this table :
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;table border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;EmployeeID&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;BossID&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p align=center&gt;
1
&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p align=center&gt;
shimon
&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p align=center&gt;
NULL
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p align=center&gt;
2
&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p align=center&gt;
yossi
&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p align=center&gt;
1
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p align=center&gt;
3
&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p align=center&gt;
Gaby
&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p align=center&gt;
1
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p align=center&gt;
4
&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p align=center&gt;
koby
&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p align=center&gt;
3
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p align=center&gt;
5
&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p align=center&gt;
jack
&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p align=center&gt;
3
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
we have we that the employee shimon is the "big boss" (because there is no other boss
above him),&lt;br&gt;
under shimon we have the employees Gaby and yossi,&lt;br&gt;
and under Gaby, we have another 2 employees : Koby and jack
&lt;/p&gt;
&lt;p&gt;
the recursive solution is to write some stored procedure that will receive the employeeID
and return as a data-Table the results&lt;br&gt;
i will not add the code for this solution and surely will not recommend it because
it was many problems :
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
for each record we received as a descendant , 
&lt;br&gt;
we need to run with the function and get her descendants, 
&lt;br&gt;
and so on, until there are no descendants for the node 
&lt;li&gt;
we are limited to 32 levels of hierarchy 
&lt;li&gt;
the runtime will depend on the row count that is in the table (we will need to run
on each of the rows one at the time) 
&lt;li&gt;
the run on the node will look like this :&lt;br&gt;
&lt;img height=200 alt=heirarchy.jpg src="http://www.krokhmal.com/Blog/content/binary/heirarchy.jpg" width=300 border=0&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
by the way, the most common way that I've seen to select hierarchical structure,&lt;br&gt;
is simply by setting a join between the levels in the select query.&lt;br&gt;
for example :
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; TopBoss.Name
TopBoss, Boss.Name Boss, Employees.Name Employee&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; Employees 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;INNER&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;JOIN&lt;/span&gt; Employees &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AS&lt;/span&gt; Boss &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; Employees.BossID=Boss.EmployeeID&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;INNER&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;JOIN&lt;/span&gt; Employees
TopBoss &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; Boss.BossID=TopBoss.EmployeeID&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
this apply to the selection of three levels
&lt;/p&gt;
&lt;p&gt;
For each level, you'd need to join the table to itself...not an attractive option
if you have 5 or more levels ,&lt;br&gt;
you don't know how many levels you will have to select, there is no way can control
it!&lt;br&gt;
It would be great if it could join itself as many times as needed. This is called
a recursive join, and though some database products support it (Oracle has the CONNECT
BY syntax) SQL Server is not one of them.
&lt;/p&gt;
&lt;p&gt;
the other way is based on a thread that i read &lt;a href="http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=2828"&gt;here&lt;/a&gt;&amp;nbsp;about
hierarchies,&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
lets create a table :&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;CREATE&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;TABLE&lt;/span&gt; Tree
(&lt;br&gt;
Node &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NOT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NULL&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;IDENTITY&lt;/span&gt;(100,
1),&lt;br&gt;
ParentNode &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;, 
&lt;br&gt;
EmployeeID &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NOT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NULL&lt;/span&gt;, 
&lt;br&gt;
Depth &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;tinyint&lt;/span&gt;,&lt;br&gt;
Lineage &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;varchar&lt;/span&gt;(255)
)&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
the extra fields that has been added are the "&lt;strong&gt;lineage&lt;/strong&gt;" and the "&lt;strong&gt;depth&lt;/strong&gt;"
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Depth - for saving the current depth of the record in the hierarchy 
&lt;li&gt;
Lineage - for saving all the ancestors of the record as a concatenated string&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
after filling the needed data for relations, the table looks like this :
&lt;/p&gt;
&lt;p&gt;
&lt;table cellspacing=0 cellpadding=2 border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=head&gt;
Node&lt;/td&gt;
&lt;td class=head&gt;
ParentNode&lt;/td&gt;
&lt;td class=head&gt;
EmployeeID&lt;/td&gt;
&lt;td class=head&gt;
Depth&lt;/td&gt;
&lt;td class=head&gt;
Lineage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
100&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;td class=n&gt;
1001&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
101&lt;/td&gt;
&lt;td class=n&gt;
100&lt;/td&gt;
&lt;td class=n&gt;
1002&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
102&lt;/td&gt;
&lt;td class=n&gt;
101&lt;/td&gt;
&lt;td class=n&gt;
1003&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
103&lt;/td&gt;
&lt;td class=n&gt;
102&lt;/td&gt;
&lt;td class=n&gt;
1004&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
104&lt;/td&gt;
&lt;td class=n&gt;
102&lt;/td&gt;
&lt;td class=n&gt;
1005&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
105&lt;/td&gt;
&lt;td class=n&gt;
102&lt;/td&gt;
&lt;td class=n&gt;
1006&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;
The next part is to find the root node of the tree, also known as the top-level, etc. 
&lt;br&gt;
That's the node that has no parent (Null), so we will start there and set the Lineage
column as the root: 
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;UPDATE&lt;/span&gt; Tree &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SET&lt;/span&gt; Lineage=&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'/'&lt;/span&gt;,
Depth=0 &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; ParentNode &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Is&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Null&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
Once we did that, 
&lt;br&gt;
we can then update the rows who are the&amp;nbsp;descendant of the root node:
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHILE&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;EXISTS&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; * &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; Tree &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; Depth &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Is&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Null&lt;/span&gt;)&amp;nbsp;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;UPDATE&lt;/span&gt; T &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SET&lt;/span&gt; T.depth
= P.Depth + 1,&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;T.Lineage = P.Lineage + &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Ltrim&lt;/span&gt;(&lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Str&lt;/span&gt;(T.ParentNode,6,0))
+ &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'/'&lt;/span&gt;&amp;nbsp;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;FROM&lt;/span&gt; Tree &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AS&lt;/span&gt; T&amp;nbsp;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;INNER&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;JOIN&lt;/span&gt; Tree &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AS&lt;/span&gt; P &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; (T.ParentNode=P.Node)&amp;nbsp;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE&lt;/span&gt; P.Depth&amp;gt;=0&amp;nbsp;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND&lt;/span&gt; P.Lineage &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Is&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Not&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Null&lt;/span&gt;&amp;nbsp;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND&lt;/span&gt; T.Depth &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Is&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Null&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font face=Verdana color=#003300 size=2&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font face=Verdana color=#003300 size=2&gt;this
loop will run once for each level of the hierarchy (&lt;strong&gt;not for each node as the
recursion method&lt;/strong&gt;.) 
&lt;br&gt;
so, with data representation of 10,000 records with 8 levels of hierarchy, 
&lt;br&gt;
this code will run only 8 times to populate the needed data of the "lineage" field
and the "depth" field, and this "heavy" procedure will happen only once at the setup.&lt;br&gt;
the table should look like this after the given operation : 
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font face=Verdana color=#003300 size=2&gt;
&lt;br&gt;
&lt;/p&gt;
&gt;&gt;&gt; 
&lt;table cellspacing=0 cellpadding=2 border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=head&gt;
Node&lt;/td&gt;
&lt;td class=head&gt;
ParentNode&lt;/td&gt;
&lt;td class=head&gt;
EmployeeID&lt;/td&gt;
&lt;td class=head&gt;
Depth&lt;/td&gt;
&lt;td class=head&gt;
Lineage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
100&lt;/td&gt;
&lt;td class=n&gt;
NULL&lt;/td&gt;
&lt;td class=n&gt;
1001&lt;/td&gt;
&lt;td class=n&gt;
0&lt;/td&gt;
&lt;td class=n&gt;
/&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
101&lt;/td&gt;
&lt;td class=n&gt;
100&lt;/td&gt;
&lt;td class=n&gt;
1002&lt;/td&gt;
&lt;td class=n&gt;
1&lt;/td&gt;
&lt;td class=n&gt;
/100/&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
102&lt;/td&gt;
&lt;td class=n&gt;
101&lt;/td&gt;
&lt;td class=n&gt;
1003&lt;/td&gt;
&lt;td class=n&gt;
2&lt;/td&gt;
&lt;td class=n&gt;
/100/101/&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
103&lt;/td&gt;
&lt;td class=n&gt;
102&lt;/td&gt;
&lt;td class=n&gt;
1004&lt;/td&gt;
&lt;td class=n&gt;
3&lt;/td&gt;
&lt;td class=n&gt;
/100/101/102/&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
104&lt;/td&gt;
&lt;td class=n&gt;
102&lt;/td&gt;
&lt;td class=n&gt;
1005&lt;/td&gt;
&lt;td class=n&gt;
3&lt;/td&gt;
&lt;td class=n&gt;
/100/101/102/&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=n&gt;
105&lt;/td&gt;
&lt;td class=n&gt;
102&lt;/td&gt;
&lt;td class=n&gt;
1006&lt;/td&gt;
&lt;td class=n&gt;
3&lt;/td&gt;
&lt;td class=n&gt;
/100/101/102/&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
You'll notice that for each node, the entire lineage back to the root is stored. This
means that finding someone's boss, or their boss' boss, doesn't require any self-joins
or recursion to create an indented list. In fact, it can be accomplished with a single
SELECT.
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Space&lt;/span&gt;(T.Depth*2)
+ E.Name &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AS&lt;/span&gt; Name&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; Employees
E 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;INNER&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;JOIN&lt;/span&gt; Tree
T &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; E.EmployeeID=T.EmployeeID&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ORDER&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;BY&lt;/span&gt; T.Lineage
+ &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Ltrim&lt;/span&gt;(&lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Str&lt;/span&gt;(T.Node,6,0))&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
maintaining the table is really not a big deal if we will use triggers.&lt;br&gt;
think about the new inserted record as the row that has not been filled in the setup
process.&lt;br&gt;
so the insert trigger should be : 
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;UPDATE&lt;/span&gt; T &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SET&lt;/span&gt; T.depth
= P.Depth + 1, 
&lt;br&gt;
T.Lineage = P.Lineage + &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Ltrim&lt;/span&gt;(&lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Str&lt;/span&gt;(T.ParentNode,6,0))
+ &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'/'&lt;/span&gt; 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; Tree &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AS&lt;/span&gt; T 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;INNER&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;JOIN&lt;/span&gt; Tree &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AS&lt;/span&gt; P &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; (T.ParentNode=P.Node) 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; P.Depth&amp;gt;=0 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AND&lt;/span&gt; P.Lineage &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Is&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Not&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Null&lt;/span&gt; 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AND&lt;/span&gt; T.Depth &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Is&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Null&lt;/span&gt;
&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
the update trigger should do pretty much the same : building the 2 extra field all
over again.
&lt;/p&gt;
&lt;p&gt;
suggestions and request will be repplied :)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=8d51eb7c-3975-4fcf-a2e8-75c8b1443661" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,8d51eb7c-3975-4fcf-a2e8-75c8b1443661.aspx</comments>
      <category> Sql Server </category>
      <category>Architectural solutions</category>
      <category>database</category>
      <category>Sql Server 2000</category>
    </item>
  </channel>
</rss>