<?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>Geek Scrapbook &#187; Code Samples</title>
	<atom:link href="http://www.geekscrapbook.com/category/code-samples/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekscrapbook.com</link>
	<description>The .Net developer&#039;s everyday how-to guide.</description>
	<lastBuildDate>Fri, 03 Sep 2010 19:58:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Connection Timeout Using LINQ DataContext</title>
		<link>http://www.geekscrapbook.com/2010/08/13/connection-timeout-using-linq-datacontext/</link>
		<comments>http://www.geekscrapbook.com/2010/08/13/connection-timeout-using-linq-datacontext/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 16:23:48 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.geekscrapbook.com/2010/08/13/connection-timeout-using-linq-datacontext/</guid>
		<description><![CDATA[For a while now, I’ve had a website that was sporadically encountering the following error: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached. My Research On the website in question, I use [...]]]></description>
			<content:encoded><![CDATA[<p>For a while now, I’ve had a website that was sporadically encountering the following error:</p>
<blockquote><p>Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.</p>
</blockquote>
<p><strong>My Research      <br /></strong>On the website in question, I use LINQ-to-SQL heavily to perform data operations. Naturally, I suspected that I wasn’t disposing of my DataContext objects correctly, causing me to exceed the number of connections available in the pool. After doing a little research on the subject, I read in several articles (some by folks who are much more knowledgeable than myself) that I shouldn’t have to dispose of them. They all said the following code is just fine:</p>
<blockquote><pre class="code"><span style="color: #2b91af">CORDataContext </span>mcon = <span style="color: blue">new </span><span style="color: #2b91af">CORDataContext</span>();
<span style="color: #2b91af">COR_Employee </span>emp = mcon.COR_Employees.FirstOrDefault();</pre>
</blockquote>
<p>In fact, I should be able to put that into an infinite loop and never get the exception I was encountering. According to the experts, LINQ is not only smart enough to automatically open and close the connection for me, but also smart enough to dispose of the object when I’m done. No need for a “using” around my code or even a Connection.Close().</p>
<p><strong>An Exception to the Rule</strong></p>
<p>I noticed, though, that my code was slightly different. In my specific setup, I have a central database and a LOT of other databases I might want to connect to. So, to take advantage of connection pooling more, I always connect to the central database first and change to whatever catalog I need to access. In doing so, it is necessary to manually open the connection, like the following:</p>
<blockquote>
<pre class="code"><span style="color: #2b91af">CORDataContext </span>mcon = <span style="color: blue">new </span><span style="color: #2b91af">CORDataContext</span>();mcon.Connection.Open(); mcon.Connection.ChangeDatabase(<span style="color: #a31515">&quot;OtherDB&quot;</span>);
<span style="color: #2b91af">COR_Employee </span>emp = mcon.COR_Employees.FirstOrDefault();</pre>
</blockquote>
<p><strong>My Testing</p>
<p></strong>I began to theorize that manually opening the connection in this way <strong>keeps the connection alive</strong> after I’ve run my query. I confirmed the belief by trying to run the following code:</p>
<blockquote>
<pre class="code"><span style="color: blue">for </span>(<span style="color: blue">int </span>i = 0; i &lt; 1000; i++) {

    <span style="color: #2b91af">CORDataContext </span>mcon = <span style="color: blue">new </span><span style="color: #2b91af">CORDataContext</span>();
    <span style="color: #2b91af">COR_Employee </span>emp = mcon.COR_Employees.FirstOrDefault();
    mcon.Connection.Open();
    mcon.Connection.ChangeDatabase(<span style="color: #a31515">&quot;OtherDB&quot;</span>);

    <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;&quot; </span>+ (i + 1) + <span style="color: #a31515">&quot; / 1000&quot;</span>);
}</pre>
</blockquote>
<p>I got the exception after 230 or so connections. By default there are 100 connections available in the pool, but the garbage collector kicked in and helped give me a buffer before I ran out. So, from that I can conclude that manually opened DataContext connections will stay open until the connection is closed or the DataContext is disposed.</p>
<p><strong>The Solution</strong></p>
<p>Once I fully understood the problem, the fix was common sense:</p>
<blockquote>
<pre class="code"><span style="color: blue">for </span>(<span style="color: blue">int </span>i = 0; i &lt; 1000; i++) {

    <span style="color: blue">using </span>(<span style="color: #2b91af">CORDataContext </span>mcon = <span style="color: blue">new </span><span style="color: #2b91af">CORDataContext</span>()) {
        <span style="color: #2b91af">COR_Employee </span>emp = mcon.COR_Employees.FirstOrDefault();
        mcon.Connection.Open();
        mcon.Connection.ChangeDatabase(<span style="color: #a31515">&quot;OtherDB&quot;</span>);

        <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;&quot; </span>+ (i + 1) + <span style="color: #a31515">&quot; / 1000&quot;</span>);
    }
}</pre>
</blockquote>
<p>Adding a “using” to automatically dispose of my DataContext did the trick, and this loop now finishes in about 3 seconds (even with the overhead of writing to the console). I hope this helps someone else, because it’s been plaguing me for weeks!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekscrapbook.com/2010/08/13/connection-timeout-using-linq-datacontext/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Serializing Data with System.Runtime.Serialization.DataContractSerializer</title>
		<link>http://www.geekscrapbook.com/2010/03/06/serializing-data-with-system-runtime-serialization-datacontractserializer/</link>
		<comments>http://www.geekscrapbook.com/2010/03/06/serializing-data-with-system-runtime-serialization-datacontractserializer/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 15:16:57 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Cash Tracker]]></category>
		<category><![CDATA[Code Samples]]></category>

		<guid isPermaLink="false">http://www.geekscrapbook.com/2010/03/06/serializing-data-with-system-runtime-serialization-datacontractserializer/</guid>
		<description><![CDATA[Last time we discussed Serializing Data with System.Xml.Serialization.XmlSerializer. We will be using the same sample solution for this article. With .Net 3.5, Microsoft released the DataContractSerializer. It has a variety of uses, but at the very least it’s an improvement over the XmlSerializer. In Cash Tracker we started out by using the XmlSerializer to save [...]]]></description>
			<content:encoded><![CDATA[<p>Last time we discussed <a href="http://www.geekscrapbook.com/2010/03/06/serializing-data-with-system-xml-serialization-xmlserializer/" target="_blank">Serializing Data with System.Xml.Serialization.XmlSerializer</a>. We will be using the same sample solution for this article.</p>
<p>With .Net 3.5, Microsoft released the DataContractSerializer. It has a variety of uses, but at the very least it’s an improvement over the XmlSerializer. In <a href="http://www.geekscrapbook.com/cash-tracker" target="_blank">Cash Tracker</a> we started out by using the XmlSerializer to save files, but quickly found it inadequate, the reason being XmlSerializer does not play well with <strong>mixed access properties</strong>. </p>
<p>For an example, let’s look again at our Person class:</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">Person </span>{
    <span style="color: blue">public string </span>FirstName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    <span style="color: blue">public string </span>LastName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    <span style="color: blue">public string </span>Initials { <span style="color: blue">get</span>; <span style="color: blue">private set</span>; }

    <span style="color: blue">private </span>Person() {
    }
    <span style="color: blue">public </span>Person(<span style="color: blue">string </span>first, <span style="color: blue">string </span>last) {
        <span style="color: blue">this</span>.FirstName = first;
        <span style="color: blue">this</span>.LastName = last;

        <span style="color: blue">this</span>.Initials = <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0}.{1}.&quot;</span>, FirstName[0], LastName[0]);
    }
}</pre>
<pre class="code"><font face="Tahoma">Notice I’ve added a new property with a private mutator. Also, I added a constructor to take advantage of the new property and made my default constructor private. Now when we try to serialize using the XmlSerializer we get an error:</font></pre>
<blockquote>
<p>Unable to generate a temporary class (result=1).<br />
    <br />error CS0200: Property or indexer &#8216;Person.Initials&#8217; cannot be assigned to &#8212; it is read only</p>
</blockquote>
<p><strong>What to do?</strong></p>
<p>In .Net 3.5, to get around this problem, <a href="http://connect.microsoft.com/VisualStudio/feedback/details/96882/xmlserializer-does-not-handle-properties-with-mixed-access-modifiers" target="_blank">Microsoft suggests using the DataContractSerializer</a>, so we will investigate switching to the new technology here. First, we need a reference to System.Runtime.Serialization:</p>
<p><a href="http://www.geekscrapbook.com/wp-content/uploads/2010/03/image2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.geekscrapbook.com/wp-content/uploads/2010/03/image_thumb2.png" width="400" height="451" /></a> </p>
<p>Once that’s done it’s actually pretty simply converting our old code. The new code looks like this:</p>
<pre class="code"><span style="color: blue">static void </span>XmlSerializePerson(<span style="color: #2b91af">Person </span>myPerson, <span style="color: blue">string </span>fileName) {
    <span style="color: #2b91af">DataContractSerializer </span>xs = <span style="color: blue">new </span><span style="color: #2b91af">DataContractSerializer</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">Person</span>));

    <span style="color: blue">using </span>(<span style="color: #2b91af">FileStream </span>fs = <span style="color: blue">new </span><span style="color: #2b91af">FileStream</span>(fileName, <span style="color: #2b91af">FileMode</span>.Create, <span style="color: #2b91af">FileAccess</span>.Write)) {
        xs.WriteObject(fs, myPerson);
    }
}

<span style="color: blue">static </span><span style="color: #2b91af">Person </span>XmlDeserializePerson(<span style="color: blue">string </span>fileName) {
    <span style="color: #2b91af">DataContractSerializer </span>xs = <span style="color: blue">new </span><span style="color: #2b91af">DataContractSerializer</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">Person</span>));

    <span style="color: blue">using </span>(<span style="color: #2b91af">FileStream </span>fs = <span style="color: blue">new </span><span style="color: #2b91af">FileStream</span>(fileName, <span style="color: #2b91af">FileMode</span>.Open, <span style="color: #2b91af">FileAccess</span>.Read)) {
        <span style="color: blue">return </span>(<span style="color: #2b91af">Person</span>)xs.ReadObject(fs);
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>Notice we simply re-used the old methods, changing the object and methods used to serialize our Person. Our code will now execute as expected:</p>
<p><a href="http://www.geekscrapbook.com/wp-content/uploads/2010/03/image3.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.geekscrapbook.com/wp-content/uploads/2010/03/image_thumb3.png" width="400" height="269" /></a> </p>
<p><strong>Download the Solution</strong></p>
<p><strong></strong></p>
<p>You can download a zip file of the solution by clicking the link below.</p>
<p><a href="http://www.geekscrapbook.com/wp-content/uploads/2010/03/DataContractSerializerDemo.zip" target="_blank">DataContractSerializerDemo.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekscrapbook.com/2010/03/06/serializing-data-with-system-runtime-serialization-datacontractserializer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Serializing Data with System.Xml.Serialization.XmlSerializer</title>
		<link>http://www.geekscrapbook.com/2010/03/06/serializing-data-with-system-xml-serialization-xmlserializer/</link>
		<comments>http://www.geekscrapbook.com/2010/03/06/serializing-data-with-system-xml-serialization-xmlserializer/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 14:55:42 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Samples]]></category>

		<guid isPermaLink="false">http://www.geekscrapbook.com/2010/03/06/serializing-data-with-system-xml-serialization-xmlserializer/</guid>
		<description><![CDATA[Serializing objects is often a useful way to save the state of an application. Let’s take a look at doing this using the XmlSerializer object. Class: Person Below is a simple class Person. My person has a name and an age: public class Person { public string FirstName { get; set; } public string LastName [...]]]></description>
			<content:encoded><![CDATA[<p>Serializing objects is often a useful way to save the state of an application. Let’s take a look at doing this using the XmlSerializer object.</p>
<p><strong>Class: Person</strong></p>
<p>Below is a simple class Person. My person has a name and an age:</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">Person </span>{
    <span style="color: blue">public string </span>FirstName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    <span style="color: blue">public string </span>LastName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }

    <span style="color: blue">public </span>Person() {
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>The XmlSerializer requires that I leave an empty constructor in my class. I can make the constructor private if I don’t want it used for other things. Now, let’s create an instance of a Person.</p>
<pre class="code"><span style="color: blue">class </span><span style="color: #2b91af">Program </span>{
    <span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args) {
        <span style="color: #2b91af">Person </span>myPerson = <span style="color: blue">new </span><span style="color: #2b91af">Person</span>();
        myPerson.FirstName = <span style="color: #a31515">&quot;John&quot;</span>;
        myPerson.LastName = <span style="color: #a31515">&quot;Smith&quot;</span>;
    }
}</pre>
<p><strong>How to serialize our class</strong></p>
<p>I will add a couple methods now to handle serializating and deserializing myPerson.</p>
<pre class="code"><span style="color: blue">static void </span>XmlSerializePerson(<span style="color: #2b91af">Person </span>myPerson, <span style="color: blue">string </span>fileName) {
    <span style="color: #2b91af">XmlSerializer </span>xs = <span style="color: blue">new </span><span style="color: #2b91af">XmlSerializer</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">Person</span>));

    <span style="color: blue">using </span>(<span style="color: #2b91af">FileStream </span>fs = <span style="color: blue">new </span><span style="color: #2b91af">FileStream</span>(fileName, <span style="color: #2b91af">FileMode</span>.Create, <span style="color: #2b91af">FileAccess</span>.Write)) {
        xs.Serialize(fs, myPerson);
    }
}

<span style="color: blue">static </span><span style="color: #2b91af">Person </span>XmlDeserializePerson(<span style="color: blue">string </span>fileName) {
    <span style="color: #2b91af">XmlSerializer </span>xs = <span style="color: blue">new </span><span style="color: #2b91af">XmlSerializer</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">Person</span>));

    <span style="color: blue">using </span>(<span style="color: #2b91af">FileStream </span>fs = <span style="color: blue">new </span><span style="color: #2b91af">FileStream</span>(fileName, <span style="color: #2b91af">FileMode</span>.Open, <span style="color: #2b91af">FileAccess</span>.Read)) {
        <span style="color: blue">return </span>(<span style="color: #2b91af">Person</span>)xs.Deserialize(fs);
    }
}</pre>
<p><strong>Code in action</strong></p>
<p>Now, saving myPerson is simple:</p>
<pre class="code"><span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args) {
    <span style="color: #2b91af">Person </span>myPerson = <span style="color: blue">new </span><span style="color: #2b91af">Person</span>();
    myPerson.FirstName = <span style="color: #a31515">&quot;John&quot;</span>;
    myPerson.LastName = <span style="color: #a31515">&quot;Smith&quot;</span>;

    XmlSerializePerson(myPerson, <span style="color: #a31515">@&quot;C:\MyPerson.xml&quot;</span>);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>Let’s take a look at the resulting XML:</p>
<pre class="code"><span style="color: blue">&lt;?</span><span style="color: #a31515">xml </span><span style="color: red">version</span><span style="color: blue">=</span>&quot;<span style="color: blue">1.0</span>&quot;<span style="color: blue">?&gt;
&lt;</span><span style="color: #a31515">Person </span><span style="color: red">xmlns:xsi</span><span style="color: blue">=</span>&quot;<span style="color: blue">http://www.w3.org/2001/XMLSchema-instance</span>&quot; <span style="color: red">xmlns:xsd</span><span style="color: blue">=</span>&quot;<span style="color: blue">http://www.w3.org/2001/XMLSchema</span>&quot;<span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">FirstName</span><span style="color: blue">&gt;</span>John<span style="color: blue">&lt;/</span><span style="color: #a31515">FirstName</span><span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">LastName</span><span style="color: blue">&gt;</span>Smith<span style="color: blue">&lt;/</span><span style="color: #a31515">LastName</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Person</span><span style="color: blue">&gt;</span></pre>
<p>Now, let’s deserialize our person and display its data:</p>
<pre class="code"><span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args) {
    <span style="color: #2b91af">Person </span>myPerson = XmlDeserializePerson(<span style="color: #a31515">@&quot;C:\MyPerson.xml&quot;</span>);

    <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Name: &quot; </span>+ myPerson.LastName + <span style="color: #a31515">&quot;, &quot; </span>+ myPerson.FirstName);
    <span style="color: #2b91af">Console</span>.ReadLine();
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>The result:</p>
<p><a href="http://www.geekscrapbook.com/wp-content/uploads/2010/03/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.geekscrapbook.com/wp-content/uploads/2010/03/image_thumb1.png" width="400" height="269" /></a> </p>
<p>As you can see, the XmlSerializer did its job.</p>
<p><strong>Download the Solution</strong></p>
<p>You can download a zip file of the solution by clicking the link below.</p>
<p><a href="http://www.geekscrapbook.com/wp-content/uploads/2010/03/XmlSerializerDemo.zip" target="_blank">XmlSerializerDemo.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekscrapbook.com/2010/03/06/serializing-data-with-system-xml-serialization-xmlserializer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Version Checking with System.Version</title>
		<link>http://www.geekscrapbook.com/2010/03/04/version-checking-with-system-version/</link>
		<comments>http://www.geekscrapbook.com/2010/03/04/version-checking-with-system-version/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 01:19:02 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Samples]]></category>

		<guid isPermaLink="false">http://www.geekscrapbook.com/2010/03/04/version-checking-with-system-version/</guid>
		<description><![CDATA[The .Net Framework makes version checking extremely simple. The framework provides a Version class that implements IComparable. Comparing two versions is as easy as the code below: using System; namespace VersionChecker { class Program { static void Main(string[] args) { Version v1 = new Version(&#34;1.0.0.0&#34;); Version v2 = new Version(&#34;1.0.0.1&#34;); if (v1 &#62; v2) { [...]]]></description>
			<content:encoded><![CDATA[<p>The .Net Framework makes version checking extremely simple. The framework provides a <a href="http://msdn.microsoft.com/en-us/library/system.version.aspx" target="_blank">Version</a> class that implements IComparable. Comparing two versions is as easy as the code below:</p>
<pre class="csharpcode"><span class="kwrd">using</span> System;

<span class="kwrd">namespace</span> VersionChecker {
    <span class="kwrd">class</span> Program {
        <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args) {
            Version v1 = <span class="kwrd">new</span> Version(<span class="str">&quot;1.0.0.0&quot;</span>);
            Version v2 = <span class="kwrd">new</span> Version(<span class="str">&quot;1.0.0.1&quot;</span>);

            <span class="kwrd">if</span> (v1 &gt; v2) {
                Console.WriteLine(<span class="str">&quot;v1 is higher&quot;</span>);
            } <span class="kwrd">else</span> {
                Console.WriteLine(<span class="str">&quot;v2 is higher&quot;</span>);
            }

            Console.ReadLine();
        }
    }
}</pre>
<p>Here’s the result:</p>
<p><a href="http://www.geekscrapbook.com/wp-content/uploads/2010/03/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.geekscrapbook.com/wp-content/uploads/2010/03/image_thumb.png" width="399" height="268" /></a></p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
]]></content:encoded>
			<wfw:commentRss>http://www.geekscrapbook.com/2010/03/04/version-checking-with-system-version/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Method to Add an Image to a PDF</title>
		<link>http://www.geekscrapbook.com/2009/11/16/c-method-to-add-an-image-to-a-pdf/</link>
		<comments>http://www.geekscrapbook.com/2009/11/16/c-method-to-add-an-image-to-a-pdf/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 23:31:41 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Samples]]></category>

		<guid isPermaLink="false">http://www.geekscrapbook.com/2009/11/16/c-method-to-add-an-image-to-a-pdf/</guid>
		<description><![CDATA[I had to write a method to put images onto PDFs generically, so I wrote this handy little method. Just get your PDF and your image into byte arrays and fire the method below. I used PDFSharp to append the image. This method could be used to do things like build a letterhead generator or [...]]]></description>
			<content:encoded><![CDATA[<p>I had to write a method to put images onto PDFs generically, so I wrote this handy little method. Just get your PDF and your image into byte arrays and fire the method below. I used <a href="http://www.pdfsharp.net" target="_blank">PDFSharp</a> to append the image. This method could be used to do things like build a letterhead generator or any other kind of PDF document generation.</p>
<p>Here’s the method:<a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="color: blue">public static byte</span>[] AppendImageToPdf(<span style="color: blue">byte</span>[] pdf, <span style="color: blue">byte</span>[] img, <span style="color: #2b91af">Point </span>position, <span style="color: blue">double </span>scale)
{
    <span style="color: blue">using </span>(System.IO.<span style="color: #2b91af">MemoryStream </span>msPdf = <span style="color: blue">new </span>System.IO.<span style="color: #2b91af">MemoryStream</span>(pdf))
    {
        <span style="color: blue">using </span>(System.IO.<span style="color: #2b91af">MemoryStream </span>msImg = <span style="color: blue">new </span>System.IO.<span style="color: #2b91af">MemoryStream</span>(img))
        {
            System.Drawing.<span style="color: #2b91af">Image </span>image = System.Drawing.<span style="color: #2b91af">Image</span>.FromStream(msImg);
            PdfSharp.Pdf.<span style="color: #2b91af">PdfDocument </span>document = PdfSharp.Pdf.IO.<span style="color: #2b91af">PdfReader</span>.Open(msPdf);
            PdfSharp.Pdf.<span style="color: #2b91af">PdfPage </span>page = document.Pages[0];
            PdfSharp.Drawing.<span style="color: #2b91af">XGraphics </span>gfx = PdfSharp.Drawing.<span style="color: #2b91af">XGraphics</span>.FromPdfPage(page);
            PdfSharp.Drawing.<span style="color: #2b91af">XImage </span>ximg = PdfSharp.Drawing.<span style="color: #2b91af">XImage</span>.FromGdiPlusImage(image);

            gfx.DrawImage(
                ximg,
                position.X,
                position.Y,
                ximg.Width * scale,
                ximg.Height * scale
            );

            <span style="color: blue">using </span>(System.IO.<span style="color: #2b91af">MemoryStream </span>msFinal = <span style="color: blue">new </span>System.IO.<span style="color: #2b91af">MemoryStream</span>())
            {
                document.Save(msFinal);
                <span style="color: blue">return </span>msFinal.ToArray();
            }

        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekscrapbook.com/2009/11/16/c-method-to-add-an-image-to-a-pdf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Code Sample: ASP.Net Message Control</title>
		<link>http://www.geekscrapbook.com/2009/07/05/code-sample-asp-net-message-control/</link>
		<comments>http://www.geekscrapbook.com/2009/07/05/code-sample-asp-net-message-control/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 02:58:13 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Samples]]></category>

		<guid isPermaLink="false">http://www.geekscrapbook.com/2009/07/05/code-sample-asp-net-message-control/</guid>
		<description><![CDATA[Relaying information snippets and error messages is one of the most common tasks between web application developers. The most basic control this can be done with is the Literal. That’s great for an internal application or a school project, but in the real world information has to be both visually appealing and convey what the [...]]]></description>
			<content:encoded><![CDATA[<p>Relaying information snippets and error messages is one of the most common tasks between web application developers. The most basic control this can be done with is the Literal. That’s great for an internal application or a school project, but in the real world information has to be both visually appealing and convey what the message is about using an icon or other form of illustration.</p>
<p>I will now take you briefly through a pretty simple control that has saved hours of my time. First, you may want to download the sample code, images, and style sheet at the end of this article.</p>
<p><strong>Using MessageControl      <br /></strong>The code here is pretty simple. If you already have a helper or control library you reference from most of your web projects, you may want to add this file to it. Once that is done, just register the assembly on a page (or in your web.config), implement the css classes, add images to your images folder, and start using it. For example:</p>
<pre class="csharpcode"><span class="asp">&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Default.aspx.cs&quot;
    Inherits=&quot;MessageControlTest._Default&quot; %&gt;</span>
<span class="asp">&lt;%@ Register Assembly=&quot;Controls&quot; TagPrefix=&quot;CC&quot; Namespace=&quot;Controls&quot; %&gt;</span>
<span class="kwrd">&lt;!</span><span class="html">DOCTYPE</span> <span class="attr">html</span> <span class="attr">PUBLIC</span> <span class="kwrd">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
    <span class="kwrd">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">html</span> <span class="attr">xmlns</span><span class="kwrd">=&quot;http://www.w3.org/1999/xhtml&quot;</span> <span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">head</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">link</span> <span class="attr">href</span><span class="kwrd">=&quot;style.css&quot;</span> <span class="attr">rel</span><span class="kwrd">=&quot;stylesheet&quot;</span> <span class="attr">type</span><span class="kwrd">=&quot;text/css&quot;</span> <span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">title</span><span class="kwrd">&gt;&lt;/</span><span class="html">title</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">head</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">body</span><span class="kwrd">&gt;&lt;</span><span class="html">form</span> <span class="attr">id</span><span class="kwrd">=&quot;form1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span><span class="kwrd">&gt;&lt;</span><span class="html">div</span><span class="kwrd">&gt;</span>
            Our test MessageControl lives below:
            <span class="kwrd">&lt;</span><span class="html">CC:MessageControl</span> <span class="attr">ID</span><span class="kwrd">=&quot;msgInfo&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">IconType</span><span class="kwrd">=&quot;Info&quot;</span>
                <span class="attr">MessageText</span><span class="kwrd">=&quot;Testing the Message Control&quot;</span> <span class="kwrd">/&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">CC:MessageControl</span> <span class="attr">ID</span><span class="kwrd">=&quot;msgWarn&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">IconType</span><span class="kwrd">=&quot;Warn&quot;</span>
                <span class="attr">MessageText</span><span class="kwrd">=&quot;Testing the Message Control&quot;</span> <span class="kwrd">/&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">CC:MessageControl</span> <span class="attr">ID</span><span class="kwrd">=&quot;msgError&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">IconType</span><span class="kwrd">=&quot;Error&quot;</span>
                <span class="attr">MessageText</span><span class="kwrd">=&quot;Testing the Message Control&quot;</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;&lt;/</span><span class="html">form</span><span class="kwrd">&gt;&lt;/</span><span class="html">body</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">html</span><span class="kwrd">&gt;</span></pre>
<p>
  </p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Yields the following:</p>
<p><a href="http://www.geekscrapbook.com/wp-content/uploads/2009/07/image8.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.geekscrapbook.com/wp-content/uploads/2009/07/image_thumb5.png" width="360" height="432" /></a></p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p><strong>Download the Solution </p>
<p></strong>You can download a zip file of the sample solution by clicking the link below.</p>
<p><a href="http://www.geekscrapbook.com/wp-content/uploads/2009/07/MessageControl.zip">MessageControl.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekscrapbook.com/2009/07/05/code-sample-asp-net-message-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
