<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pearl Tech &#187; .NET</title>
	<atom:link href="http://blog.pearltechnology.com/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pearltechnology.com</link>
	<description></description>
	<lastBuildDate>Thu, 05 Jan 2012 14:47:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting out of a bind &#8212; with bindings</title>
		<link>http://blog.pearltechnology.com/getting-out-of-a-bind-with-bindings/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://blog.pearltechnology.com/getting-out-of-a-bind-with-bindings/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 12:12:52 +0000</pubDate>
		<dc:creator>RobB</dc:creator>
				<category><![CDATA[Application Development]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Bindings]]></category>
		<category><![CDATA[Converters]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://blog.pearltechnology.com/?p=1152</guid>
		<description><![CDATA[Converters in WPF are quite possibly the most useful tool that I&#8217;ve used in the relatively short time I&#8217;ve spent developing WPF applications.  They give the ability to format data, style controls, and more, based on the value supplied from a binding.  And it doesn&#8217;t even have to be a binding to a data source.  [...]]]></description>
			<content:encoded><![CDATA[<p>Converters in WPF are quite possibly the most useful tool that I&#8217;ve used in the relatively short time I&#8217;ve spent developing WPF applications.  They give the ability to format data, style controls, and more, based on the value supplied from a binding.  And it doesn&#8217;t even have to be a binding to a data source.  You can bind to a property of another control on the page.  Lets take a look at a couple of examples.</p>
<p><span style="text-decoration: underline;">Setting a property based on a data value:</span></p>
<p>In this example, we have a label on a form that will display a dollar amount from our data source.  If the dollar amount is negative we want the label&#8217;s background to be red, and green if the value is positive.  To do this, we simply create a converter class in our code behind page.  This will use the numeric value and return a Brush object which we will use in our label&#8217;s Background property.  Here&#8217;s the code for the converter:</p>
<p style="text-align: center;"><img class="size-large wp-image-1153 aligncenter" title="cellbackgroundconverter" src="http://blog.pearltechnology.com/wp-content/uploads/2011/04/cellbackgroundconverter-1024x312.jpg" alt="cellbackgroundconverter" width="1024" height="312" /></p>
<p>We then have to create a simple reference to our converter class in our ResourceDictionary in the Window.Resources section of our XAML:</p>
<p><img class="alignnone size-full wp-image-1154" title="converterdefinition" src="http://blog.pearltechnology.com/wp-content/uploads/2011/04/converterdefinition.JPG" alt="converterdefinition" width="397" height="30" /></p>
<p>So far so good.  Now all we have to do is bind the label&#8217;s background property to the converter and data value.  Notice that we have to bind both the label&#8217;s content (text) and background to the Amount value from our data context.  We also can set the ContentStringFormat property to automatically format the value as currency:</p>
<p style="text-align: center;"><img class="size-full wp-image-1155 aligncenter" title="labelwithconverterbinding" src="http://blog.pearltechnology.com/wp-content/uploads/2011/04/labelwithconverterbinding.JPG" alt="labelwithconverterbinding" width="709" height="39" /></p>
<p><span style="text-decoration: underline;">Setting a property based on another control&#8217;s property:</span></p>
<p>This method is useful when there isn&#8217;t a data field to bind to, but there is a need to conditionally set a control&#8217;s property.  Let&#8217;s assume we need to set a button control&#8217;s IsEnabled property based on a check box&#8217;s IsChecked property:</p>
<p style="text-align: center;"><img class="size-full wp-image-1158 aligncenter" title="bindingtocontrolproperties" src="http://blog.pearltechnology.com/wp-content/uploads/2011/04/bindingtocontrolproperties1.JPG" alt="bindingtocontrolproperties" width="754" height="144" /></p>
<p>As you can see, we&#8217;ve bound the IsEnabled property of the buttons to the IsChecked property of  the check box by specifying the ElementName (control&#8217;s name) and Path (control&#8217;s property).  We also bound the Content property of the button to the same IsChecked property using the same method, to show the current state of the button (which the framework automatically converts from a boolean to a string).  The best part about this method?  No code behind!  Here&#8217;s the result:</p>
<p style="text-align: center;"><img class="size-full wp-image-1159 aligncenter" title="bindingtocontrolpropertiesresult" src="http://blog.pearltechnology.com/wp-content/uploads/2011/04/bindingtocontrolpropertiesresult.JPG" alt="bindingtocontrolpropertiesresult" width="115" height="101" /></p>
<p style="text-align: left;">This is a very simple overview of the power of converters and bindings, and how they can be used to help you do more than just display the data on your page.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pearltechnology.com/getting-out-of-a-bind-with-bindings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Jquery with Custom Html Helpers in MVC2</title>
		<link>http://blog.pearltechnology.com/using-jquery-with-custom-html-helpers-in-mvc2/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://blog.pearltechnology.com/using-jquery-with-custom-html-helpers-in-mvc2/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 15:58:28 +0000</pubDate>
		<dc:creator>Chad Ferguson</dc:creator>
				<category><![CDATA[Application Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Html Helpers]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[MVC2]]></category>

		<guid isPermaLink="false">http://blog.pearltechnology.com/?p=896</guid>
		<description><![CDATA[While preparing for an upcoming exam I was looking into the usage of Html Helpers with MVC2 and thought it may be simple to easily add extension methods for Jquery UI controls. I have had several sites that have utilized multiple Jquery UI DatePicker controls and it would be a great time saver to simply [...]]]></description>
			<content:encoded><![CDATA[<p>While preparing for an upcoming exam I was looking into the usage of Html Helpers with MVC2 and thought it may be simple to easily add extension methods for Jquery UI controls. I have had several sites that have utilized multiple Jquery UI DatePicker controls and it would be a great time saver to simply call a method to generate the control and its script tags. </p>
<p>First we will need to create a class for the Html Helper, I usually prefer to add a folder to my solution called Helpers. Both the class and the method will need to be static. In addition you will need to add a HtmlHelper parameter to the method that is preceded by the keyword <em>this</em>. This first parameter of the extension method indicates the class that the extension method extends, which in our case would be a input control.</p>
<p><code><br />
using System;<br />
using System.Web.Mvc;</p>
<p>public static class DatePickerExtensions<br />
    {<br />
        public static string DatePicker(this HtmlHelper helper, string id)<br />
        {</p>
<p>        }<br />
    }<br />
</code></p>
<p>Next within our DatePicker Method we will want to add the input control and the Jquery, and I found this easiest way to do this was to use TagBuilders. The TagBuilder Class is a utility that easily allows you to build html tags. We will be using two TagBuilders, one for generating our input box, and a second TagBuilder for the script tag. An overview of the TagBuilder class can be found <a href="http://www.asp.net/mvc/tutorials/using-the-tagbuilder-class-to-build-html-helpers-cs">here</a>. Any script tag will need to be written with the TagBuilders InnerHtml property, or it will be interpreted as text on the page. The resulting code is below:</p>
<p><code><br />
using System;<br />
using System.Web.Mvc;</p>
<p>namespace MvcApplication1.Helpers<br />
{<br />
    public static class DatePickerExtensions<br />
    {<br />
        public static string DatePicker(this HtmlHelper helper, string id)<br />
        {<br />
            var builder = new TagBuilder("input");<br />
            builder.GenerateId(id);<br />
            builder.MergeAttribute("type", "text");</p>
<p>            var scriptBuilder = new TagBuilder("script");<br />
            scriptBuilder.InnerHtml = "$(function() { $('#" + id + "').datepicker();});";<br />
            return builder.ToString(TagRenderMode.Normal) + scriptBuilder.ToString(TagRenderMode.Normal);<br />
        }<br />
    }<br />
}<br />
</code></p>
<p>Next to be able add the control easily on any page we will need to add the Namespace for our Helpers to the web.config file as shown below.</p>
<p><code><br />
&lt;pages&gt;<br />
      &lt;namespaces&gt;<br />
        &lt;add namespace="System.Web.Mvc" /&gt;<br />
        &lt;add namespace="System.Web.Mvc.Ajax" /&gt;<br />
        &lt;add namespace="System.Web.Mvc.Html" /&gt;<br />
        &lt;add namespace="System.Web.Routing" /&gt;<br />
        <strong>&lt;add namespace="MvcApplication1.Helpers"/&gt;</strong><br />
      &lt;/namespaces&gt;<br />
    &lt;/pages&gt;<br />
</code></p>
<p>Finally we can easily add our DatePicker control anywhere to our site with our new Extension.<br />
<code><br />
Date: &lt;%= Html.DatePicker("myDatePicker") %&gt;<br />
</code></p>
<p>This short tutorial assumes that your site is already set up for using JQuery, and the UI controls. Details for using Jquery can be found <a href="http://jqueryui.com/demos/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pearltechnology.com/using-jquery-with-custom-html-helpers-in-mvc2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework 4 GUID as an Entity Key</title>
		<link>http://blog.pearltechnology.com/entity-framework-4-guid-as-an-entity-key/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://blog.pearltechnology.com/entity-framework-4-guid-as-an-entity-key/#comments</comments>
		<pubDate>Wed, 13 Oct 2010 21:39:36 +0000</pubDate>
		<dc:creator>Chad Ferguson</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[EF4]]></category>
		<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://blog.pearltechnology.com/?p=777</guid>
		<description><![CDATA[Recently for a client we decided to utilize SQL 2008 Filestreams for handling their library of documents. A requirement with Filestreams is that SQL needs to maintain the creation of the GUID for each item. Since we were using the Entity Framework 4 (EF4) for the data model we ran into a rather common problem [...]]]></description>
			<content:encoded><![CDATA[<p>Recently for a client we decided to utilize SQL 2008 Filestreams for handling their library of documents. A requirement with Filestreams is that SQL needs to maintain the creation of the GUID for each item. Since we were using the <a href="http://msdn.microsoft.com/en-us/data/aa937723.aspx">Entity Framework 4 (EF4)</a> for the data model we ran into a rather common problem in EF4, that it does not support using a GUID’s as an entity key. A <a href="https://connect.microsoft.com/VisualStudio/feedback/details/588796/using-a-guid-as-an-entitykey-in-entity-framework-4">known workaround</a> is to manually edit the edmx as XML and add StoreGeneratedPattern=&#8221;Identity&#8221; to the affected columns.</p>
<p><img class="size-full wp-image-778" src="http://blog.pearltechnology.com/wp-content/uploads/2010/10/EF4_1.JPG" alt="EF4_1" width="457" height="63" /></p>
<p>Unfortunately this is a step that will need to be reproduced each and every time a database refresh is done. So an approach that worked for us was to create a new EF4 model at runtime, injecting the required code into it.</p>
<p>To do this first you need to create a MetadataWorkspace (System.Data.Entity), which allows you to interact with model metadata.</p>
<pre name="code" language="csharp">MetadataWorkspace workspace = new MetadataWorkspace();</pre>
<p>Next you will need to read the existing metadata from your current model and replace the text with the change we manually added before. To do this we will query the executing assembly for the Store Schema Definition Language (SSDL) file which is the file we initially had to manually edit.  <em>You can locate the names of your SSDL, MSL, and CSDL in the web.config</em>.</p>
<pre name="code" language="csharp">Assembly edmAssembly = Assembly.GetExecutingAssembly();
StreamReader metaReader = new StreamReader(edmAssembly.GetManifestResourceStream("Models.ModelName.ssdl"));
var text = metaReader.ReadToEnd();
text = text.Replace("&lt;Property Name=\"RowGUID\" Type=\"uniqueidentifier\" Nullable=\"false\" /&gt;", "&lt;Property Name=\"RowGUID\" Type=\"uniqueidentifier\" Nullable=\"false\"  StoreGeneratedPattern=\"Identity\" /&gt;");</pre>
<p>Once we have a modified SSDL we can began to build a new EF4 model. This will require the other two components, the Conceptual Schema Definition Language (CSDL) and the Mapping Specification Language (MSL).</p>
<p>The MSL, SSDL, and CSDL did Snoop Dogg Come up with these names? <img src='http://blog.pearltechnology.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To get the MSL and CSDL we can just read them in with MemoryStreams (System.IO) like so.</p>
<pre name="code" language="csharp">var ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(text));
var sReaders = new[] { XmlReader.Create(ms) };
var mReaders = new[] { XmlReader.Create(edmAssembly.GetManifestResourceStream("Models.ModelName.msl")) };

var cReaders = new[] { XmlReader.Create(edmAssembly.GetManifestResourceStream("Models.ModelName.csdl")) };</pre>
<p>Now that we have streams of the 3 necessary files we can assemble them into the model. For the SSDL we use a StoreItemCollection (System.Data.Entity) to load the xml file. For the CSDL we use the EdmItemCollection (System.Data.Entity) and then we map those to files to the MSL using the StorageMappingItemCollection (System.Data.Mapping).</p>
<pre name="code" language="csharp">var sCollection = new StoreItemCollection(sReaders);
var cCollection = new EdmItemCollection(cReaders);
var csCollection = new StorageMappingItemCollection(cCollection, sCollection, mReaders);</pre>
<p>Now that we have our new EF4 model built we simply register it in the MetadataWorkspace.</p>
<pre name="code" language="csharp">workspace.RegisterItemCollection(sCollection);
workspace.RegisterItemCollection(csCollection);
workspace.RegisterItemCollection(cCollection);</pre>
<p>Finally we declare are data model context with the new workspace and our connection string.</p>
<pre name="code" language="csharp">DBModel _db = new DBModel(new EntityConnection(workspace, (SqlConnection) connectionString));</pre>
<p>We then have access to a new EF4 model at runtime that now has a GUID that can be an entity key. To be safe we then clean up any loose ends after committing the database changes.</p>
<pre name="code" language="csharp">ms.Close();
_db.Dispose();</pre>
<div>-Chad Ferguson</div>
<p><em><br />
</em><em> </em><em></em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pearltechnology.com/entity-framework-4-guid-as-an-entity-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enterprise Library Logger.Write Not Working?</title>
		<link>http://blog.pearltechnology.com/enterprise-library-logger-write-not-working/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://blog.pearltechnology.com/enterprise-library-logger-write-not-working/#comments</comments>
		<pubDate>Mon, 17 May 2010 21:29:50 +0000</pubDate>
		<dc:creator>Geer</dc:creator>
				<category><![CDATA[Application Development]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Enterprise Library]]></category>
		<category><![CDATA[Logger]]></category>

		<guid isPermaLink="false">http://blog.pearltechnology.com/?p=697</guid>
		<description><![CDATA[Using Logging Application Block provided by Enterprise Library is a common way of logging exceptions in .NET applications.  It&#8217;s easy to setup and to use.  One problem I had a while ago was that it worked while on my development PC, but stopped working once it&#8217;s deployed.
What I was doing was to use it to [...]]]></description>
			<content:encoded><![CDATA[<p>Using Logging Application Block provided by Enterprise Library is a common way of logging exceptions in .NET applications.  It&#8217;s easy to setup and to use.  One problem I had a while ago was that it worked while on my development PC, but stopped working once it&#8217;s deployed.</p>
<p>What I was doing was to use it to write event entries in windows event log as part of the exception handling in an ASP.NET web application.  Nothing fancy.  It worked fine on my PC, but it didn&#8217;t write anything to the event log when Logger.Write is executed. No exception was thrown from that line of code either.  The program just went through as if that line of code was skipped.</p>
<p>It turned out to be a permission issue. When I debug the application in Visual Studio, it was running under my windows account credential which had permission to create Event Source in the system.  However, when the application was deployed to the server, it was running under the default account which was &#8220;NT Authority\Network Service&#8221; in my case.  By default, that account doesn&#8217;t have permission to create new Event Source in the system.  It does have permission to write event entries though.  So, when Logger.Write was executed, it couldn&#8217;t proceed because the Event Source wasn&#8217;t there, and the program didn&#8217;t have permission to create one.  Actually, it makes sense to not throw any exception on this line, because people like me usually use this to log exceptions, and if the logging process itself throws exceptions, there is probably nowhere else we can handle and/or log them.</p>
<p>Now, how this can be resolve?  The solution is quite simple.  As long as the Event Source is created ahead of time, it will be no problem.  What I did was to create required Event Source(s) on server through CMD.exe.  Here is a sample command:</p>
<p><span style="color: #0000ff"><code>eventcreate /ID 1 /L Application /T information /SO "My Event Source" /D "My Event Source is created."</code></span></p>
<p>This command will create an event entry in the Application log.  If the Event Source doesn&#8217;t exist, it will be created during the process.  This is only required when the application is deployed to the server first time.  So, I usually do it manually on server.  However, if that&#8217;s not the case, you can build it into the installation package, and when the application is installed, the Event Source is created as well.  Of course, you can use .NET to create Event Source in your installer too.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pearltechnology.com/enterprise-library-logger-write-not-working/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use of TransactionScope in .NET</title>
		<link>http://blog.pearltechnology.com/use-of-transactionscope-in-net-2/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://blog.pearltechnology.com/use-of-transactionscope-in-net-2/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 12:53:50 +0000</pubDate>
		<dc:creator>Geer</dc:creator>
				<category><![CDATA[Application Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MSDTC]]></category>
		<category><![CDATA[TransactionScope]]></category>

		<guid isPermaLink="false">http://blog.pearltechnology.com/?p=79</guid>
		<description><![CDATA[In one of the projects I’m currently working on, I have to handle multiple SQL operations in one transaction, so if anything goes wrong in a particular operation, things done in previous operations can be rolled back automatically.  This can be done by using Transaction in a Stored Procedure directly, but you might lose some [...]]]></description>
			<content:encoded><![CDATA[<p>In one of the projects I’m currently working on, I have to handle multiple SQL operations in one transaction, so if anything goes wrong in a particular operation, things done in previous operations can be rolled back automatically.  This can be done by using Transaction in a Stored Procedure directly, but you might lose some flexibility that way.  For example, when you want to insert an object with multiple children in database, it’s not easy to pass an array or a list of items (children object) to the Stored Procedure.  So, I considered using TransactionScope provided by .NET to handle this.</p>
<p>In order to get away from code being escalated to MSDTC (Microsoft Distributed Transaction Coordinator), I decided to use single SqlConnection per TransactionScope.  Also, to maximize code reusability, we ended up with the following development pattern.  For simplicity reason, error handling is not added in the example below.</p>
<pre style="font-family:Courier New;line-height:14px"><span style="color:blue">public</span> <span style="color:blue">static</span> <span style="color:blue">void</span> CreateOrder(<span style="color:#006080">Order</span> myOrder)
{
    <span style="color:blue">using</span> (<span style="color:#006080">TransactionScope</span> scope =
            <span style="color:blue">new</span> <span style="color:#006080">TransactionScope</span>(<span style="color:#006080">TransactionScopeOption</span>.RequiresNew))
    {
        <span style="color:blue">using</span> (<span style="color:#006080">SqlConnection</span> cn = <span style="color:#006080">DB</span>.GetConnection())
        {
            cn.Open();
            <span style="color:#006080">SqlCommand</span> cmd = <span style="color:blue">new</span> <span style="color:#006080">SqlCommand</span>();
            cmd.Connection = cn;

            InsertOrderTransaction(myOrder, cmd);
        }
        scope.Complete();
    }
}

<span style="color:blue">internal</span> <span style="color:blue">static</span> <span style="color:blue">void</span> InsertOrderTransaction(<span style="color:#006080">Order</span> myOrder, <span style="color:#006080">SqlCommand</span> cmd)
{
    cmd.CommandText = "dbo.spCOrder";
    cmd.<span style="color:#006080">CommandType</span> = <span style="color:#006080">CommandType</span>.StoredProcedure;

    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@CustomerID", myOrder.CustomerID);
    <span style="color:green">// ...</span>

    myOrder.ID = <span style="color:#006080">Convert</span>.ToInt64(cmd.ExecuteScalar());

    InsertOrderTransactionPostProcessing(myOrder, cmd);
}</pre>
<p>As you can see, the method InsertOrderTransaction takes the Order object and a SqlCommand instance. It assumes that the SqlConnection has been established for the SqlCommand in the calling method. It doesn’t care if the SqlCommand needs to be executed within a transaction or not. It simply executes a SQL statement or a Stored Procedure. In its calling method CreateOrder, it defines the TransactionScope and defines and opens a SqlConnection. It can call multiple transactional methods (in this case InsertTransaction) and keep them all in one transaction. However, you don’t have to define a TranscationScope in the calling method if you are not dealing with multiple SQL operations. You can also add pre-processing and post-processing functions if needed.</p>
<pre style="font-family:Courier New;line-height:14px"><span style="color:blue">internal</span> <span style="color:blue">static</span> <span style="color:blue">void</span> InsertOrderTransactionPostProcessing(<span style="color:#006080">Order</span> myOrder, <span style="color:#006080">SqlCommand</span> cmd)
{
    <span style="color: #0000ff">if</span> (myOrder.OrderItems != <span style="color:blue">null</span>)
    {
        <span style="color: #0000ff">foreach </span>(<span style="color:#006080">OrderItem</span> myItem<span style="color:blue"> in </span>myOrder.OrderItems)
        {
            myItem.OrderID = myOrder.ID;
            InsertOrderItemTransaction(myItem, cmd);
        }
    }
}

<span style="color:blue">internal</span> <span style="color:blue">static</span> <span style="color:blue">void</span> InsertOrderItemTransaction(<span style="color:#006080">OrderItem</span> myItem, <span style="color:#006080">SqlCommand</span> cmd)
{
    cmd.CommandText = "dbo.spCOrderItem";
    cmd.<span style="color:#006080">CommandType</span> = <span style="color:#006080">CommandType</span>.StoredProcedure;

    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@ItemID", myItem.ItemID);
    <span style="color:green">// ...</span>
    myItem.OrderItemID = <span style="color:#006080">Convert</span>.ToInt64(cmd.ExecuteScalar());
}</pre>
<p>One thing you have to be aware of is that TransactionScope may not be a good choice if these operations take too long to complete. The reason is obvious. Transaction may result in temporary locking on certain objects (such as tables) in your database. If the transaction needs 5 minutes to complete, it may lock a table for that long and your users may be unable to access data in the table for that long. If anything goes wrong at the very last second in the transaction, it may take another 5 minutes for the database to roll back previous operations in this transaction, and make the table inaccessible to your users for even longer. So in cases like this, one transaction may need to be broken into multiple segments.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pearltechnology.com/use-of-transactionscope-in-net-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

