<?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>Adventures In Development &#187; C#</title>
	<atom:link href="http://www.adventuresindevelopment.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adventuresindevelopment.com</link>
	<description>Web Development Tools, Ideas, Techniques and Resources</description>
	<lastBuildDate>Fri, 27 Aug 2010 04:11:04 +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>How to Make Use of Namespaces in C# and Visual Basic .NET</title>
		<link>http://www.adventuresindevelopment.com/2010/03/23/how-to-make-use-of-namespaces-in-c-and-visual-basic-net/</link>
		<comments>http://www.adventuresindevelopment.com/2010/03/23/how-to-make-use-of-namespaces-in-c-and-visual-basic-net/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 16:19:16 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=194</guid>
		<description><![CDATA[If you&#8217;ve written any sort of software application of decent size, you&#8217;ll know that you need to structure your code, most often using object oriented design techniques, to keep your code-base manageable. Some languages such as Java and C# enable developers to write object-oriented applications relatively well out of the box without much extra work. [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve written any sort of software application of decent size, you&#8217;ll know that you need to structure your code, most often using object oriented design techniques, to keep your code-base manageable. Some languages such as Java and C# enable developers to write object-oriented applications relatively well out of the box without much extra work. Others, such as JavaScript, Classic ASP and PHP (without any frameworks attached), make users do a lot more work to keep their code base manageable.</p>
<p>One of the features that was included with C#/VB.NET is the idea of name-spaces, which are not necessarily a requirement to write object-oriented code, however do a a great job of creating a hierarchical class structure and generally keeping your classes, methods, enumerations, etc in non-spaghetti-code state.</p>
<p><span style="text-decoration: underline;"><strong>What is a namespace?</strong></span></p>
<p>A namespace can best be described as a collection of classes and enumerations.</p>
<p><span style="text-decoration: underline;"><strong>An Example</strong></span></p>
<p>Typically, you would want to put classes and enums together in a namespace that are related. For example, if you were considering making some C# code to represent the organization structure of a library, you might have a namespace for the library as a whole, then have sub-namespaces for different aspects of the library, such as the materials that can be rented out in a library, the library&#8217;s staff members, and its facilities.</p>
<p>Here&#8217;s what a hierarchical structure of namespaces might look like in a Library in C#</p>
<p><a href="http://www.adventuresindevelopment.com/wp-content/uploads/2010/03/namespaces.jpg"><img class="alignnone size-full wp-image-195" title="namespaces" src="http://www.adventuresindevelopment.com/wp-content/uploads/2010/03/namespaces.jpg" alt="" width="383" height="519" /></a></p>
<p>You&#8217;ll notice that name spaces can be hierarchically organized and that name spaces can contain both classes and enumerations. They can also hold delegates, interfaces and structs. They cannot directly include methods, which must be contained within a class.</p>
<p><span style="text-decoration: underline;"><strong>Referencing a Class in a Namespace</strong></span></p>
<p>If you were to create the class that we just made above and put it into a C# application (most likely in the app_code folder), this is how you would create some of the classes above:</p>
<p><strong>AdministrationBuilding</strong> -  Library.Facilities.AdministrationBuilding thisBuilding = new Library.Facilities.AdministrationBuilding();</p>
<p><strong>Journal</strong> &#8211; Library.Materials.Periodicals.Journal thisJournal = new Library.Materials.Periodicals.Journal();</p>
<p><strong>BookType </strong>- Library.Materials.BookType thisType = new Library.Materials.BookType();</p>
<p><span style="text-decoration: underline;"><strong>Making Good use of the Using Declaration</strong></span></p>
<p>If you plan to make use of a namespace a lot on a particular form, web-form, or any other C#/VB file, you can import the namespace directly on the page much in the way that you might import one of the inherited namespaces under the &#8220;System&#8221; class.</p>
<p>When doing development, if you wanted to create a DataTable object, you would probably want to add &#8220;using System.Data;&#8221; to the top of your page so that you can reference the variable type by just using &#8220;DataTable ThisTable&#8221; rather than &#8220;System.Data.DataTable ThisTable&#8221;.</p>
<p>You can also do this with your own namespaces.</p>
<p><strong>Here&#8217;s how you would import the &#8220;periodicals&#8221; namespace in C#</strong> &#8211;   using Library.Materials.Periodicals;</p>
<p><strong>Here&#8217;s how you would import the &#8220;periodicals&#8221; namespace in VB.NET</strong> &#8212; Imports Library.Materials.Periodicals</p>
<p>Now, instead of creating a journal like we did above, we could simply write &#8220;Journal thisJournal = new Journal();&#8221; to create a new object of type Journal.</p>
<p><span style="text-decoration: underline;"><strong>Here are a few other good resources on Namespaces:</strong></span></p>
<ul>
<li><a href="http://www.vbdotnetheaven.com/UploadFile/ssivkumar/NamespacesInVBdotNETCsharp04072005054728AM/NamespacesInVBdotNETCsharp.aspx">Creating and using Namespaces in VB.NET and C#</a> (VB.NET Heaven)</li>
<li><a href="http://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx">Using Namespaces</a> (MSDN)</li>
<li><a href="http://www.csharphelp.com/2006/02/namespaces-in-c/">Namespaces in C#</a> (C Sharp Help)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2010/03/23/how-to-make-use-of-namespaces-in-c-and-visual-basic-net/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fast and Efficient C# and Visual Basic String Concatenation</title>
		<link>http://www.adventuresindevelopment.com/2009/06/25/fast-and-efficient-c-and-visual-basic-string-concatenation/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/25/fast-and-efficient-c-and-visual-basic-string-concatenation/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 18:15:12 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=150</guid>
		<description><![CDATA[If you do any sort of web development work on the .NET platform, you are going to find yourself concatenating (connecting) strings together on a very regular basis. There are two ways to do this. The first is with a traditional string concatenation, which would look something like this: string MyString = String.Empty; MyString = [...]]]></description>
			<content:encoded><![CDATA[<p>If you do any sort of web development work on the .NET platform, you are going to find yourself concatenating (connecting) strings together on a very regular basis. There are two ways to do this.</p>
<p><strong>The first is with a traditional string concatenation, which would look something like this:</strong></p>
<p>string MyString = String.Empty;<br />
MyString = &#8220;Hello &#8221; + &#8220;World&#8221;;</p>
<p><strong>The second way, is using the StringBuilder library, which would look something like this:</strong></p>
<p>StringBuilder MyString = new StringBuilder();<br />
MyString.Append(&#8220;Hello &#8220;);<br />
MyString.Append(&#8220;World&#8221;);</p>
<p>These two sets of code do basically the same thing, but is it preferable to use one over the other? It turns out, that the StringBuilder class is much, much more efficient when dealing with large sets of strings. For short strings, with fewer than five concatenations, chances are you&#8217;ll be better off with traditional string concatenation because you don&#8217;t have to instantiate a copy of the StringBuilder library, but for situations that you want to do a lot of string manipulation, you definitely want to use the StringBuilder library.</p>
<p>Here&#8217;s a synthetic test using C# and ASP.NET comparing the two:</p>
<p>        Trace.IsEnabled = true;</p>
<p>        StringBuilder StBuilder = new StringBuilder();<br />
        Trace.Write(&#8220;String Builder Append Begin&#8221;);<br />
        for (int x = 0; x &lt; 10000; x++)<br />
        {<br />
            StBuilder.Append(&#8220;Testing123&#8243;);<br />
        }<br />
        Trace.Write(&#8220;String Builder Complete&#8221;);</p>
<p>        string stString = String.Empty;<br />
        Trace.Write(&#8220;String Concatenation Beginning&#8221;);<br />
        for (int x = 0; x &lt; 10000; x++)<br />
        {<br />
            stString += &#8220;Testing123&#8243;;<br />
        }<br />
        Trace.Write(&#8220;String Concatenation Complete&#8221;);</p>
<p>The test concatenated the string 10,000 times. Although it&#8217;s a synthetic test, it shows that the StringBuilder class is substantially more efficient than the string class for concatenation. The string class concatenated the text in 5.292587 seconds. The StringBuilder class performed the same task in just 0.006142 seconds. In other words, the StringBuilder was over 850 times faster at concatenating the string than the string class was.</p>
<p>Now that&#8217;s a performance benefit!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/25/fast-and-efficient-c-and-visual-basic-string-concatenation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET Performance Tip: Remove Unnecessary Library References</title>
		<link>http://www.adventuresindevelopment.com/2009/06/22/aspnet-performance-tip-remove-unnecessary-library-references/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/22/aspnet-performance-tip-remove-unnecessary-library-references/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 16:12:17 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=148</guid>
		<description><![CDATA[Over the last few days, I&#8217;ve been scouring the web for techniques and strategies to optimize ASP.NET code so that it runs faster and more efficient, resulting in quicker load times. A lot of what I found was pretty standard advice, disable viewstate, use the StringBuilder for concatenation, disable tracing and use AJAX. One piece [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last few days, I&#8217;ve been scouring the web for techniques and strategies to optimize ASP.NET code so that it runs faster and more efficient, resulting in quicker load times. A lot of what I found was pretty standard advice, disable viewstate, use the StringBuilder for concatenation, disable tracing and use AJAX. One piece of advice that I didn&#8217;t find when searching was to be selective about the libraries that you reference.</p>
<p><strong>By default, ASP.NET will add the following references to your page:</strong></p>
<p>using System;<br />
using System.Data;<br />
using System.Configuration;<br />
using System.Linq;<br />
using System.Web;<br />
using System.Web.Security;<br />
using System.Web.UI;<br />
using System.Web.UI.HtmlControls;<br />
using System.Web.UI.WebControls;<br />
using System.Web.UI.WebControls.WebParts;<br />
using System.Xml.Linq;<br />
using System.Data.SqlClient;</p>
<p><strong>In a library that I was using, I was able to pair those down to these five libraries:</strong></p>
<p>using System;<br />
using System.Web;<br />
using System.Data;<br />
using System.Data.SqlClient;<br />
using System.Configuration;</p>
<p>After removing the un-necessary libraries, the page load time decreased by nearly half of a second. If you were to do this with all of your libraries and pages, chances are you could see a pretty significant performance increase.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/22/aspnet-performance-tip-remove-unnecessary-library-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Display Your Twitter Feed using ASP.NET</title>
		<link>http://www.adventuresindevelopment.com/2009/06/21/how-to-display-your-twitter-feed-using-aspnet/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/21/how-to-display-your-twitter-feed-using-aspnet/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 00:08:38 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Content Management Systems]]></category>
		<category><![CDATA[Social Media]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=145</guid>
		<description><![CDATA[UPDATE 3/23/2010 - Ricky from Twitterizer commented below noting that basic authentication will soon go away via Twitter and OAUTH will be required. Note that the code below will only work for a few months. We will post an updated code-example soon. As I write this article, It&#8217;s about 75 degrees and Sunny outside. When [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE 3/23/2010 </strong>- Ricky from Twitterizer commented below noting that basic authentication will soon go away via Twitter and OAUTH will be required. Note that the code below will only work for a few months. We will post an updated code-example soon.</p>
<p>As I write this article, It&#8217;s about 75 degrees and Sunny outside. When I should be going out on a bike ride, instead I&#8217;ve opted to play with <a href="http://code.google.com/p/twitterizer/">Twitterizer</a> (an ASP.NET Twitter Library). Twitterizer is an ASP.NET library that lets you interact with the Twitter API using easy to use objects and methods. It will work with any of the .NET variants (C#, VB, J#, Windows Forms, ASP.NET, WPF, etc). I added the functionality into the <a href="http://www.360webcms.com/">360 Web Content Management System</a> and I thought I&#8217;d share with you how I did it.</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to retrieve twitter feeds in ASP.NET</strong></span></p>
<p><strong>(1) Get a copy of the Twitterizer Library </strong></p>
<p>First, you&#8217;ll need to get a copy of the <a href="http://code.google.com/p/twitterizer/">Twitterizer Library from Google&#8217;s Codebase</a>. The download is pretty small and contains only the application library (DLL) you need. Create a new website in ASP.NET and extract the twitterizer library to the /bin/ folder so that you can use it.  Once you have it placed in your /bin/ folder, add a &#8220;using&#8221; reference to the library in the header of your page.</p>
<p>using Twitterizer.Framework;</p>
<p><strong>(2) Create a &#8220;Twitter&#8221; object and Retrieve Your Status Updates.</strong></p>
<p>The library contains a few different objects that you can create. A &#8220;Twitter&#8221; object is the most generic object that you can create. Creating an instance of this object using your username and password gives you all the functionality you would normally have in Twitter, but instead of using the Twitter web interface, you&#8217;re using C# or Visual Basic. First, we&#8217;ll need to instantiate the object, and then get a collection of status updates from your account.</p>
<p>Twitter thisUser = new Twitter(&#8220;UserNameHere&#8221;, &#8220;PasswordHere&#8221;);<br />
TwitterStatusCollection thisCollection = thisUser.Status.UserTimeline();</p>
<p><strong>(3) Loop Through Your Status Updates and Generate Some HTML</strong></p>
<p>The &#8220;TwitterStatusCollection&#8221; object type is a list of &#8220;TwitterStatus&#8221; objects, so you can use a foreach loop and go through your most recent status updates. You&#8217;ll notice in the code below that I also do some basic work with the time of the status update to generate a hyperlink to the page of the status, similar to what Twitter does.</p>
<p>string TwitterCode = &#8220;&#8221;;<br />
foreach (TwitterStatus thisStatus in thisCollection)<br />
{</p>
<p>TimeSpan thisSpan = new TimeSpan();<br />
thisSpan = DateTime.Now.Subtract(thisStatus.Created);</p>
<p>string TimeBetween = &#8220;&#8221;;<br />
if (thisSpan.Days &gt; 0) { TimeBetween = thisSpan.Days.ToString() + &#8221; days ago&#8221;; }<br />
else if (thisSpan.Hours &gt; 0) { TimeBetween = thisSpan.Days.ToString() + &#8221; hours ago&#8221;;}<br />
else if (thisSpan.Minutes &gt; 0) { TimeBetween = thisSpan.Days.ToString() + &#8221; minutes ago&#8221;;}<br />
else if (thisSpan.Seconds &gt; 0) { TimeBetween = thisSpan.Days.ToString() + &#8221; seconds ago&#8221;;}</p>
<p>TwitterCode += &#8220;&lt;div class=&#8217;TwitterStatus&#8217;&gt;&#8221; + thisStatus.Text + &#8221; &lt;a href=&#8217;http://twitter.com/&#8221; + thisStatus.TwitterUser.UserName + &#8220;/status/&#8221; + thisStatus.ID + &#8220;&#8216;&gt;&#8221; + TimeBetween + &#8220;&lt;/a&gt;&lt;/div&gt;&#8221;;<br />
}<br />
<strong>(4) Display Your Tweets </strong></p>
<p>You now have a string with your most recent twitter status updates that you can display on the page using a simple Response.Write() or you can display it in a label. You can see a variation of this code running on the <a href="http://www.360webcms.com/addons/twitter/">&#8220;Twitter&#8221; page for the 360 Web Content Management System</a>.</p>
<p>You can also download a copy of my <a href="http://www.adventuresindevelopment.com/wp-content/uploads/2009/06/twitter.zip">sample code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/21/how-to-display-your-twitter-feed-using-aspnet/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to Implement an ASP.NET Color Picker</title>
		<link>http://www.adventuresindevelopment.com/2009/06/12/how-to-implement-an-aspnet-color-picker/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/12/how-to-implement-an-aspnet-color-picker/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 20:28:18 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[360 WebCMS]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Content Management Systems]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=139</guid>
		<description><![CDATA[One of the components of the 360 Web Content Management System (website in progress) that I wanted to develop was an events calendar that allowed you to post events into color-coded categories. You can see a demo of it here. At first, I had it so that users would manually enter in a 6-character HTML [...]]]></description>
			<content:encoded><![CDATA[<p>One of the components of the <a href="http://www.360webcms.com/">360 Web Content Management System</a> (website in progress) that I wanted to develop was an events calendar that allowed you to post events into color-coded categories. You can see a demo of it <a href="http://cmsdemo.factor360.com/events.aspx">here</a>. At first, I had it so that users would manually enter in a 6-character HTML color code, but it was very non-intuitive for anyone who&#8217;s never worked with HTML before. Eventually I stumbled upon the <a href="http://www.karpach.com/ColorPickerDemo.aspx">ASP.NET Color Picker control</a>. It&#8217;s a custom ASP.NET control that you can add to a page much in the way that you can add a text box, radio buttons, or a drop down list.</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to implement the ASP.NET Color Picker Control</strong></span></p>
<p><strong>(1) Download the library and add it to your project<br />
</strong></p>
<p>First, download the library from the ASP.NET Color Picker Control website. Make sure to download the latest binary release from the website. Currently that version is <a href="http://www.karpach.com/files/WebControls.v.1.4.10423.1-bin.zip">ASP.NET Color Picker v.1.4.10423.1 Binary</a>. Once you get the zip file, it will contain a library that you should extract to the /bin/ folder of your website.</p>
<p><strong>(2) Register the library on your page</strong></p>
<p>ASP.NET provides a set of standard controls that you can add to a page that start with the &#8220;ASP&#8221; prefix, such as &#8220;&lt;ASP:TextBox runat=&#8221;server&#8221; id=&#8221;txtBox&#8221; /&gt;. Any custom controls will have their own prefix that you specify by registering the library on the page. It&#8217;s another line of code that you add to the top of the page next to your page definition. It should look something like this:</p>
<p>&lt;%@ Register Assembly=&#8221;Karpach.WebControls&#8221; Namespace=&#8221;Karpach.WebControls&#8221; TagPrefix=&#8221;cc1&#8243; %&gt;</p>
<p><strong>(3) Add the control to your page</strong></p>
<p>Now that you have the library referenced, you can add the control to your page and make use of it.  For the purpose of this demo, I&#8217;m going to set the AutoPostBack property to true and run a function whenever the color is changed. This will show us the color that we picked inside of a label (also shown below) after we select a new color.</p>
<p>&lt;cc1:ColorPicker ID=&#8221;colorBackgroundColor&#8221; runat=&#8221;server&#8221; AutoPostBack=&#8221;true&#8221; OnColorChanged=&#8221;chngColor&#8221; /&gt;<br />
&lt;br /&gt;&lt;br /&gt;<br />
&lt;asp:Label ID=&#8221;lblResults&#8221; runat=&#8221;server&#8221; Text=&#8221;"&gt;&lt;/asp:Label&gt;</p>
<p><strong>(4) Create Your C# Function</strong></p>
<p>After we choose a color, we have to do something with it. With the ColorPicker control above, I&#8217;m using the OnColorChanged property to call the &#8220;chngColor&#8221; function, which in C# will look something like this. This will also demonstrate how to programmatically read the color chosen with the .Color property of the ASP.NET Color Picker Control</p>
<p>protected void chngColor(object sender, EventArgs e)<br />
{<br />
lblResults.Text = &#8220;&lt;div style=&#8217;background-color:#&#8221; + colorBackgroundColor.Color.Replace(&#8220;#&#8221;, &#8220;&#8221;) + &#8220;;height:50px;width:80px;text-align:center;padding-top:35px;&#8217;&gt;Sample Text&lt;/div&gt;&#8221;;<br />
}</p>
<p><strong>(5) Success</strong></p>
<p>So far, we&#8217;ve added the library to our project, registered the library on the page, added the control to the page, and done something with the color chosen by the user. Your page should look something like this:</p>
<p><a href="http://www.adventuresindevelopment.com/wp-content/uploads/2009/06/color-picker.jpg"><img class="alignnone size-full wp-image-140" title="color-picker" src="http://www.adventuresindevelopment.com/wp-content/uploads/2009/06/color-picker.jpg" alt="color-picker" width="567" height="398" /></a></p>
<p><a href="http://www.adventuresindevelopment.com/wp-content/uploads/2009/06/colorpickerdemo.zip">You can download my sample program here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/12/how-to-implement-an-aspnet-color-picker/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Validate Email Addresses in C#</title>
		<link>http://www.adventuresindevelopment.com/2009/06/08/how-to-validate-email-addresses-in-c/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/08/how-to-validate-email-addresses-in-c/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 14:11:48 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=137</guid>
		<description><![CDATA[I was recently doing doing support for a client that had a newsletter system. The previous employee had neglected to do much in the form of format validation for email addresses from both a user-input standpoint and system-integrity standpoint. Since there were several email addresses in the database that didn&#8217;t meet the basic conventions of [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently doing doing support for a client that had a newsletter system. The previous employee had neglected to do much in the form of format validation for email addresses from both a user-input standpoint and system-integrity standpoint. Since there were several email addresses in the database that didn&#8217;t meet the basic conventions of an email address, the user received the following error whenever she tried to send out a message:</p>
<p><strong>Exception Details: </strong>System.FormatException: The specified string is not in the form required for an e-mail address.</p>
<p>Ouch. To remedy this issue, I added a check to make sure the email address was valid before it attempted to send the message. In the code below, I&#8217;m making use of the System.Text.RegularExpressions library that comes with the .NET framework. The code below is written in C# but the code will be very similar in Visual Basic. It will also work in ASP.NET, WPF or plain old windows forms.</p>
<p><strong>Here&#8217;s a C# function that will determine whether or not an email address is valid:</strong><br />
<code><br />
public static bool IsValidEmail(string strEmailAddress)<br />
{<br />
if (strEmailAddress == null)<br />
{<br />
return false;<br />
}<br />
else<br />
{<br />
return System.Text.RegularExpressions.Regex.IsMatch(strEmailAddress, @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|[a-zA-Z]{2})$", RegexOptions.IgnorePatternWhitespace);<br />
}<br />
}<br />
</code></p>
<p>I also made some modifications to the system on the front-end, so when a user registered from then on, that it would validate that they have entered an email address and that the email address matched the format of an email address using a RequiredFieldValidator and a RegularExpressionValidator.&lt;&#8211;&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/08/how-to-validate-email-addresses-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hash Passwords in C# and Visual Basic Using SHA-512</title>
		<link>http://www.adventuresindevelopment.com/2009/06/02/hash-passwords-in-c-and-visual-basic-using-sha-512/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/02/hash-passwords-in-c-and-visual-basic-using-sha-512/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 20:26:31 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=126</guid>
		<description><![CDATA[We recently covered an easy way to hash passwords using SHA-1 in .NET using either Visual Basic or C#. In most cases, SHA-1 encryption is &#8220;secure enough&#8221;, but there are some mathematical weaknesses. Microsoft&#8217;s .NET platform (specifically the System.Security class) allows you to encrypt passwords with a number of differnet algorithms without having to know the [...]]]></description>
			<content:encoded><![CDATA[<p>We recently covered an <a href="http://www.adventuresindevelopment.com/2009/05/23/a-simple-way-to-hash-passwords-in-aspnet/">easy way to hash passwords using SHA-1</a> in .NET using either Visual Basic or C#. In most cases, SHA-1 encryption is &#8220;secure enough&#8221;, but there are some <a href="http://www.schneier.com/blog/archives/2005/02/cryptanalysis_o.html">mathematical weaknesses</a>. Microsoft&#8217;s .NET platform (specifically the System.Security class) allows you to encrypt passwords with a number of differnet algorithms without having to know the mathematics behind them.</p>
<p>Today, we&#8217;re going to encrypt a string with SHA-2, specifically the SHA-512 derivation of SHA-2, which should hypothetically be more secure than SHA-1 because it has a longer message digest than SHA-1. The example code I&#8217;m going to show off today also uses a &#8220;<a href="http://en.wikipedia.org/wiki/Salt_(cryptography)">salt</a>&#8220;, whereas the previous function I showed off didn&#8217;t. This will make your hashed-passwords more immume to dictionary attacts because not only would the hacker have to develop a hash for every commonly known password, but as well as every commonly known password multiplied by the nearly infinite number of possible salts.</p>
<p><strong>Here&#8217;s the function:</strong></p>
<p>    public static string CreateSHAHash(string Password, string Salt)<br />
    {<br />
        System.Security.Cryptography.SHA512Managed HashTool = new System.Security.Cryptography.SHA512Managed();<br />
        Byte[] PasswordAsByte = System.Text.Encoding.UTF8.GetBytes(string.Concat(Password, Salt));<br />
        Byte[] EncryptedBytes = HashTool.ComputeHash(PasswordAsByte);<br />
        HashTool.Clear();<br />
        return Convert.ToBase64String(EncryptedBytes);<br />
    }</p>
<p><strong>How it works:</strong></p>
<p>This method makes use of the System.Security.Cryptography class. It combines your password and the salt that you provide and  turns it into a byte-array. It runs those bytes through the has computation function provided by the class and returns an 88-bit string of the message-digest/hash that&#8217;s created.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/02/hash-passwords-in-c-and-visual-basic-using-sha-512/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Authenticate a User in Active Directory using ASP.NET</title>
		<link>http://www.adventuresindevelopment.com/2009/06/02/how-to-authenticate-a-user-in-active-directory-using-aspnet/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/02/how-to-authenticate-a-user-in-active-directory-using-aspnet/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 15:58:41 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=123</guid>
		<description><![CDATA[If you&#8217;re working in an academic or large corporate or government setting, changes are you&#8217;re going to have a network in place using Active Directory or an open-source equivalent. Every user in the organization will have some sort of an account to use. If you&#8217;re building an internal web-application or desktop-application, it doesn&#8217;t make a [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working in an academic or large corporate or government setting, changes are you&#8217;re going to have a network in place using Active Directory or an open-source equivalent. Every user in the organization will have some sort of an account to use. If you&#8217;re building an internal web-application or desktop-application, it doesn&#8217;t make a lot of sense to give the user another set of credentials. Instead, you can validate users by checking the permissions existing Active Directory accounts.</p>
<p>The source code to check a user&#8217;s credentials in Active Directory using C# or Visual Basic is actually fairly minimal. This works with both ASP.NET and with Windows Forms  (or WPF for that matter) if you&#8217;re building a desktop application.</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to do it:</strong></span></p>
<p><strong>(1) Reference the appropriate library</strong></p>
<p>You&#8217;ll need to make use of the System.DirectoryServices library that comes with Visual Studio. You can add this to your ASP.NET code-behind page or your C# class for your Windows forms like this.</p>
<p><em>using System.DirectoryServices;</em></p>
<p><strong>(2) Create</strong><em> <strong>An Authentication Function.</strong></em></p>
<p>Here&#8217;s a basic function that will check a user&#8217;s permissions on a given domain. Essentially, it will try to create an Active Directory entry using the provided credentials, and it can successfully create a valid entry, we know that the user is authenticated. Otherwise, it&#8217;ll return false.</p>
<p>public bool AuthenticateActiveDirectory(string Domain, string UserName, string Password)<br />
{<br />
try<br />
{<br />
DirectoryEntry entry = new DirectoryEntry(&#8220;LDAP://&#8221; + Domain, UserName, Password);<br />
object nativeObject = entry.NativeObject;<br />
return true;<br />
}<br />
catch (DirectoryServicesCOMException) { return false; }<br />
}</p>
<p>That&#8217;s really all there is to it. Microsoft has an <a href="http://msdn.microsoft.com/en-us/library/ms180890(VS.80).aspx" target="_blank">extensive aritcle</a> on MSDN that covers active directory authentication in .NET that you might want to check out as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/02/how-to-authenticate-a-user-in-active-directory-using-aspnet/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to Resize Photos in ASP.NET</title>
		<link>http://www.adventuresindevelopment.com/2009/06/01/how-to-resize-photos-in-aspnet/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/01/how-to-resize-photos-in-aspnet/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 18:28:23 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=121</guid>
		<description><![CDATA[If you&#8217;re developing a website that allows users to upload any sort of photos or images, you have to expect that they aren&#8217;t going to bother doing any sort of basic image manipulation, such as resizing an 8 megapixel image down to something that&#8217;s appropriate for the web. Fortunately, there are a number of good [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re developing a website that allows users to upload any sort of photos or images, you have to expect that they aren&#8217;t going to bother doing any sort of basic image manipulation, such as resizing an 8 megapixel image down to something that&#8217;s appropriate for the web. Fortunately, there are a number of good cropping and image resizing tools that are available for most server-side programming languages.</p>
<p>ASP.NET does a particularly good job of offering the ability to resize photos. The System.Drawing library that comes with .NET allows you to resize photos extremely well with just a few lines of code, and place it in the format that you&#8217;d like.</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to resize photos in the .NET Framework:</strong></span></p>
<p><strong>(1) Reference the appropriate libraries.</strong></p>
<p>The example code below will be in C#, but it will work basically the same way for Visual Basic or J#. You&#8217;ll need to reference the System.IO library to read and write the files, as well as the System.Drawing library and the System.Drawing.Imaging library. To do this, simply add the following lines to the top of your code-behind page.</p>
<p>using System.IO;<br />
using System.Drawing;<br />
using System.Drawing.Imaging;</p>
<p><strong>(2) Load the OriginalImage</strong></p>
<p>The following three lines of code will take an existing image that you can set programmatically, say if you want to connect it to a photo gallery module in a database, and create a bitmap/image object that you can use to resize the image:</p>
<p>string UploadPath = Server.MapPath(&#8220;/images/&#8221;);<br />
string FileName = &#8220;OriginalImage.PNG&#8221;;<br />
Bitmap OriginalBM = new Bitmap(UploadPath + FileName);</p>
<p><strong>(3) Determine The New Size</strong></p>
<p>Typically you&#8217;ll want to keep the aspect ratio the same, but you can set them statically if you&#8217;d like.  Here&#8217;s some code that will create two variables for the height and the width. We set the width statically based on what we&#8217;d like to use, then the height is based on the aspect ratio of the original image so that the image is not stretched.Finally, we&#8217;ll create a &#8220;Size&#8221; object that will be used for the resizing</p>
<p>float AspectRatio = (float)OriginalBM.Size.Width / (float)OriginalBM.Size.Height;<br />
int NewWidth = 800;<br />
int newHeight = NewWidth / AspectRatio;<br />
Size newSize = new Size(NewWidth, newHeight);<strong><br />
</strong></p>
<p><strong>(4) Resize and Save the Photo</strong></p>
<p>Once you have a new &#8220;Size&#8221; object created, we can create a new Bitmap/image object with the new size that we specify and save it in the format of image that we&#8217;d like. In this case, I&#8217;m saving it as a Jpeg file, but you can also save it as a PNG, GIF, or Bitmap file if you&#8217;d like.</p>
<p>Bitmap ResizedBM = new Bitmap(OriginalBM, newSize);<br />
string newfilepath = UploadPath + &#8220;resized_&#8221; + FileName.Replace(@&#8221;\&#8221;, @&#8221;");<br />
ResizedBM.Save(newfilepath, ImageFormat.Jpeg);</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/01/how-to-resize-photos-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Export Data to Excel in C# and ASP.NET</title>
		<link>http://www.adventuresindevelopment.com/2009/05/27/how-to-export-data-to-excel-in-aspnet/</link>
		<comments>http://www.adventuresindevelopment.com/2009/05/27/how-to-export-data-to-excel-in-aspnet/#comments</comments>
		<pubDate>Wed, 27 May 2009 19:14:25 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=107</guid>
		<description><![CDATA[If you develop a web-application for a client that involves creating and viewing reports, it&#8217;s very likely that they&#8217;re going to want to be able to export that data to excel very readily. Fortunately, you won&#8217;t need any special libraries or hundreds of lines of code to make that happen in ASP.NET. All you need [...]]]></description>
			<content:encoded><![CDATA[<p>If you develop a web-application for a client that involves creating and viewing reports, it&#8217;s very likely that they&#8217;re going to want to be able to export that data to excel very readily. Fortunately, you won&#8217;t need any special libraries or hundreds of lines of code to make that happen in ASP.NET. All you need is a GridView with some data in it and a few lines of code to make that happen.</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to export data from a GridView in ASP.NET:</strong><br />
</span></p>
<p><strong>(1) Create a Grid View</strong></p>
<p>First, we&#8217;ll need a GridView control on the page that we can load our report or table into.</p>
<p>&lt;asp:GridView ID=&#8221;GridView1&#8243; runat=&#8221;server&#8221;&gt;&lt;/asp:GridView&gt;</p>
<p><strong>(2) Disable Event Validation</strong></p>
<p>Because we will be creating a fresh HTTP response, ASP.NET&#8217;s event validator will likely run into an error. Specifically, the message you would receive is &#8220;Invalid postback or callback argument.&#8221; In order to get around this, we need to disable event validation for the page that is used to export to excel. To do that, add the following tag to the top of your .ASPX page:</p>
<p>&lt;%@Page EnableEventValidation=&#8221;true&#8221; %&gt;</p>
<p><strong>(3) Add data to your GridView</strong></p>
<p>At this point, you&#8217;ve got everything you need done on the HTML side of things, and it&#8217;s time to write some C# (or VB or J#) if you prefer. The first thing we need to do in our server-side code is to add data to the gridview. In the code below, I&#8217;m using a function from the database utilities library mentioned earlier this month to get the data and return it as a DataTable. You&#8217;re more than welcome to use whatever data-access code you would like to pull the resultset you would like to get from the database. This code will most likely end up in your Page_Load() function.</p>
<p>DataTable dtResults= SQLSelect(&#8220;Select * from Users&#8221;);<br />
GridView1.DataSource = dtResults;<br />
GridView1.DataBind();</p>
<p><strong>(4) Building your export-to-excel function</strong></p>
<p>You&#8217;ll need about 10 lines of code to make the actual export happen. Essentially, it creates a new HTTP response, re-renders the gridview and puts it in a downloadable XLS format. You can either put this in your Page_Load() function or in the event handle for a button if you would like to preview the gridview before exporting it.</p>
<p>Here&#8217;s the export code:</p>
<p>Response.Clear();<br />
Response.ContentType = &#8220;application/ms-excel&#8221;;<br />
Response.Charset = &#8220;&#8221;;<br />
Page.EnableViewState = false;<br />
Response.AddHeader(&#8220;Content-Disposition&#8221;, &#8220;inline;filename=report.xls&#8221;);</p>
<p>System.IO.StringWriter tw = new System.IO.StringWriter();<br />
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);</p>
<p>GridView1.RenderControl(hw);</p>
<p>Response.Write(tw.ToString());<br />
Response.End();</p>
<p><strong>(5) Override the VerifyRenderingInServerForm Method</strong></p>
<p>Add this function to your C# class for the page to prevent a rendering error:</p>
<p>public override void VerifyRenderingInServerForm(System.Web.UI.Control control)<br />
{<br />
//confirms that an HtmlForm control is rendered for the<br />
//specified ASP.NET server control at run time.<br />
}</p>
<p><strong>(6) You&#8217;re done!</strong></p>
<p>That&#8217;s really all it takes. If you&#8217;d like a good example file, you can <a href="http://www.gridviewguy.com/GridViewExportToExcel.zip">download one from ASPAlliance</a>.</p>
<p><span style="text-decoration: underline;"><strong>Other Resources:</strong></span></p>
<p>You might also be interested in reading these articles about exporting files to excel:</p>
<ul>
<li><a href="http://blogs.msdn.com/erikaehrli/archive/2009/01/30/how-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx">How to Export Data to Excel from an ASP.NET Application + Avoid the File Format Differ Prompt (MSDN Blogs)</a></li>
<li><a href="http://aspalliance.com/771">CodeSnip: Exporting GridView to Excel (CodeSnip)</a></li>
<li><a href="http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx">ASP.Net 2.0: Export GridView to Excel (C-Sharp Corner)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/05/27/how-to-export-data-to-excel-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
