<?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; Visual Basic</title>
	<atom:link href="http://www.adventuresindevelopment.com/category/visual-basic/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>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>Making Use of Escape Sequences in C# and Visual Basic</title>
		<link>http://www.adventuresindevelopment.com/2009/05/20/making-use-of-escape-sequences-in-c-and-visual-basic/</link>
		<comments>http://www.adventuresindevelopment.com/2009/05/20/making-use-of-escape-sequences-in-c-and-visual-basic/#comments</comments>
		<pubDate>Wed, 20 May 2009 13:16:40 +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=65</guid>
		<description><![CDATA[When working with strings of text, there are certain characters that you can&#8217;t represent in a normal string variable because the key required on the keyboard simply doesn&#8217;t exist or because of how strings are represented in text. Fortunately, modern programming languages provide &#8220;escape sequences,&#8221; or ways to represent certain characters that otherwise could not [...]]]></description>
			<content:encoded><![CDATA[<p>When working with strings of text, there are certain characters that you can&#8217;t represent in a normal string variable because the key required on the keyboard simply doesn&#8217;t exist or because of how strings are represented in text. Fortunately, modern programming languages provide &#8220;escape sequences,&#8221; or ways to represent certain characters that otherwise could not be represented, in a string.</p>
<p><strong>Let&#8217;s say we wanted to represent the following string of text in a string:</strong></p>
<p>&#8220;Hello, How are you doing today?&#8221;, I asked.</p>
<p><strong>With a string, traditionally you would surround the line of text with quotes, resulting in:</strong></p>
<p>&#8220;&#8221;Hello, How are you doing today?&#8221;, I asked.&#8221;</p>
<p>However, the above will result in a syntax error because as far as a programming language is concerned, the first double quote is representitive of the beginning of a string and the second double quote is representitive of the end of the string. The language would thing the string is over immediately after the double quotes.</p>
<p><strong>Fortunately, we can represent the string like this and get the result we were looking for:</strong></p>
<p>&#8220;\&#8221;Hello, How are you doing today?\&#8221;, I asked.&#8221;</p>
<p>In C# and Visual Basic, using the backslash character indicates that the very next character will be an escape sequence. For example placing &#8220;\n&#8221; in a string would not represent a backslash and then the &#8216;n&#8217; character, but rather a new line. To make use of a backslash, once again, you use an escape sequence. Use the &#8220;\\&#8221; to make a backslash.</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s a full set of escape sequences in C# and Visual Basic</strong></span>:</p>
<ul>
<li><code>\'</code> &#8211; Single quote, needed for character literals</li>
<li><code>\"</code> &#8211; Double quote, needed for string literals</li>
<li><code>\\</code> &#8211; Backslash</li>
<li><code>\0</code> &#8211; Unicode character 0</li>
<li><code>\a</code> &#8211; Alert (character 7)</li>
<li><code>\b</code> &#8211; Backspace (character 8 )</li>
<li><code>\f</code> &#8211; Form feed (character 12)</li>
<li><code>\n</code> &#8211; New line (character 10)</li>
<li><code>\r</code> &#8211; Carriage return (character 13)</li>
<li><code>\t</code> &#8211; Horizontal tab (character 9)</li>
<li><code>\v</code> &#8211; Vertical quote (character 11)</li>
<li><code>\uxxxx</code> &#8211; Unicode escape sequence for character with hex value xxxx</li>
<li><code>\xn[n][n][n]</code> &#8211; Unicode escape sequence for character with hex value nnnn (variable length version of \uxxxx)</li>
<li><code>\Uxxxxxxxx</code> &#8211; Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/05/20/making-use-of-escape-sequences-in-c-and-visual-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using System.IO to Create, Read, Rename and Delete Files and Folders in C# and Visual Basic</title>
		<link>http://www.adventuresindevelopment.com/2009/05/18/using-systemio-to-create-read-rename-and-delete-files-and-folders-in-c-and-visual-basic/</link>
		<comments>http://www.adventuresindevelopment.com/2009/05/18/using-systemio-to-create-read-rename-and-delete-files-and-folders-in-c-and-visual-basic/#comments</comments>
		<pubDate>Tue, 19 May 2009 00:19:45 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=49</guid>
		<description><![CDATA[If you&#8217;re making use of C# or Visual Basic for Windows Forms, ASP.NET Web Forms or ASP.NET MVC, you have access to a very powerful file-system access library that&#8217;s included with Visual Studio. The library, System.IO, will allow you to create, rename, edit and delete files and folders on your system. If you are using [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re making use of C# or Visual Basic for Windows Forms, ASP.NET Web Forms or ASP.NET MVC, you have access to a very powerful file-system access library that&#8217;s included with Visual Studio. The library, System.IO, will allow you to create, rename, edit and delete files and folders on your system. If you are using System.IO in ASP.NET, you will be able to access the file system on the web-server assuming that <a href="http://msdn.microsoft.com/en-us/library/ms998320.aspx">appropriate permissions are set with the Network Service account</a>. For the purposes of this article, all of the code will be in C#, but rest assured, the same functionality is available in Visual Basic, but the syntax might be slightly different.</p>
<p><strong>To get started with the library, add a reference to the System.IO library at the top of your file</strong></p>
<p><code>Using System.IO;</code></p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to manipulate folders/directories using the System.IO Library:</strong></span></p>
<p><strong>Delete a folder:</strong></p>
<p>Directory.Delete(&#8220;c:\\directory\\subdirectory\\&#8221;); //standard delete</p>
<p>Directory.Delete(&#8220;c:\\directory\\subdirectory\\&#8221;, true); //delete folder and all subdirectories</p>
<p><strong>Create a folder:</strong></p>
<p>Directory.CreateDirectory(&#8220;c:\\directory\\subdirectory\\&#8221;)</p>
<p><strong>Check if a folder exists:</strong></p>
<p>Directory.Exists(&#8220;c:\\directory\\subdirectory\\&#8221;); //returns boolean value</p>
<p><strong>Move/Rename a </strong><strong>folder</strong><strong>:</strong></p>
<p>Directory.Move(&#8220;c:\\directory\\oldlocation\\&#8221;, &#8220;c:\\directory\\newlocation\\&#8221;);</p>
<p><strong>Get a list of files in a </strong><strong>folder</strong><strong>:</strong></p>
<p>Directory.GetFiles(&#8220;c:\\directory\\subfolder\\&#8221;); // returns a string array of files in the folder</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to manipulate files using the System.IO Library:</strong></span></p>
<p><strong>Delete a file:</strong></p>
<p>File.Delete(&#8220;c:\\directory\\filename.text&#8221;);</p>
<p><strong>Create a file:</strong></p>
<p>File.Create(&#8220;c:\\directory\\filename.text&#8221;); //note, the file will be empty. There are some overloads available that will let you create a file with content in it</p>
<p><strong>Check if a file exists:<br />
</strong></p>
<p>File.Exists(&#8220;c:\\directory\\subdirectory\\filename.text&#8221;); //returns boolean value</p>
<p><strong>Move/Rename a file:</strong></p>
<p>File.Move(&#8220;c:\\directory\\filename.text&#8221;, &#8220;c:\\directory\\filenamenew.text&#8221;);</p>
<p><strong>Create a copy of a file:</strong></p>
<p>File.Copy(&#8220;c:\\directory\\filename.text&#8221;, &#8220;c:\\directory\\filenamenew.text&#8221;);</p>
<p><strong>Read text from file:</strong></p>
<p>string FileContents = File.ReadAllText(&#8220;c:\\folder\\filename.text&#8221;);</p>
<p><strong>Write text to a file:</strong></p>
<p>File.WriteAllText(&#8220;c:\\filename.text&#8221;, &#8220;contents of file&#8221;);</p>
<p><span style="text-decoration: underline;"><strong>Tips for Using System.IO in ASP.NET</strong></span></p>
<p><strong>Using Server.MapPath to Convert Relative URLS to Physical File Paths</strong></p>
<p>Remember that the System.IO library always requires that you input a physical file path on the web server&#8217;s hard drive. Passing in a file or directory relative to the root of the web server will not work. IE, File.Exists(&#8220;/images/file.jpg&#8221;) will result in an exception. Fortunately, Microsoft has recognized that this would be an issue ahead of time and has given us the &#8220;Server.MapPath&#8221; function which will convert relative URLs on the server into physical file paths.</p>
<p>Here&#8217;s an example of Server.MapPath in action:</p>
<p><code>string RelativeURL = "/media/files/mypicture.jpg";<br />
string AbsolutePath = Server.MapPath(RelativeURL);<br />
bool FileExists = File.Exists(AbsolutePath);<br />
</code></p>
<p><strong>Setting File and Directory Permissions in IIS for ASP.NET<br />
</strong></p>
<p>If you plan on doing any sort of uploading files, modifying files, deleting files, modifying folders or deleting folders from your web-server, you are going to have to assign additional permissions on the server. To do this, you will need to make modifications to the appropriate folders that you want to be able to modify in IIS or have your web host do it for you. Essentially, you need to give the &#8220;network service&#8221; account permissions to do anything that you want ASP.NET to be able to do. If you want to be able to delete files, give the Network Service account permissions to delete files in the appropriate folder.</p>
<p>In the next couple of weeks, I&#8217;m hoping to create a C#/ASP.NET based file-manager. It&#8217;s very likely that we&#8217;ll be using a lot of these methods for this project. If there are any features you&#8217;d like to see developed in it, don&#8217;t hesitate to <a href="http://www.adventuresindevelopment.com/contact/">contact me</a>.</p>
<p><strong>Update &#8211; Mar 18 @ 9:00 PM </strong>- When I wrote the above commands, I initially neglected to use <a href="http://msdn.microsoft.com/en-us/library/h21280bw.aspx">escape sequences</a> for the file names. Remember to always use a double back-slash or start your string with the @ symbol for your physical file paths to work properly. I&#8217;ve updated the commands to reflect the correct way to reference directories.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/05/18/using-systemio-to-create-read-rename-and-delete-files-and-folders-in-c-and-visual-basic/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Format a DateTime in C# / ASP.NET</title>
		<link>http://www.adventuresindevelopment.com/2009/05/17/how-to-format-a-datetime-in-c-aspnet/</link>
		<comments>http://www.adventuresindevelopment.com/2009/05/17/how-to-format-a-datetime-in-c-aspnet/#comments</comments>
		<pubDate>Sun, 17 May 2009 19:57:32 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=35</guid>
		<description><![CDATA[When I was a new developer making websites in .NET, I ended up doing some pretty screwy things to make date&#8217;s and time&#8217;s look how I wanted to. I would do things like: DateTime thisDate = DateTime.Now; string AMPM = String.Empty; if(thisDate.Hour&#62;=12) {AMPM = "PM";} else {AMPM = "AM";} String FormattedDate = (thisDate.Hour%12).ToString() + ":" [...]]]></description>
			<content:encoded><![CDATA[<p>When I was a new developer making websites in .NET, I ended up doing some pretty screwy things to make date&#8217;s and time&#8217;s look how I wanted to. I would do things like:</p>
<p><code>DateTime thisDate = DateTime.Now;<br />
string AMPM = String.Empty;<br />
if(thisDate.Hour&gt;=12) {AMPM = "PM";} else {AMPM = "AM";}<br />
String FormattedDate = (thisDate.Hour%12).ToString() + ":" + thisDate.Minute.ToString() + " " + AMPM;<br />
</code></p>
<p>Fortunately I soon learned there were much better ways to do that. C# and Visual Basic both have a very powerful DateTime variable type, which will let you display your date/time in just about any way that you&#8217;d like  using the .ToString(&#8220;&#8221;) method. Let&#8217;s say we want to display a formatted date/time that we had stored in a variable called &#8220;ThisDate&#8221;. Here are some common ways to display your DateTime Variable:</p>
<ul>
<li>ThisDate.ToString(&#8220;d&#8221;);  // &#8220;01/10/2009&#8243;</li>
<li>ThisDate.ToString(&#8220;D&#8221;);  // &#8220;Monday, 10 January 2009&#8243;</li>
<li>ThisDate.ToString(&#8220;G&#8221;);  // &#8220;01/10/2009 12:00:00&#8243;</li>
<li>ThisDate.ToString(&#8220;hh:mm tt&#8221;) // &#8220;12:00 AM&#8221;</li>
<li>ThisDate.ToString(&#8220;<span style="font-family: 'Courier New';"><span style="font-family: Tahoma; font-size: x-small;">dddd&#8221;) // &#8220;Monday&#8221;</span></span></li>
</ul>
<p>These are the manners that I most frequently use the ASP.NET DateTime format tool. There&#8217;s a blogger named Kathy Kam that has a great extended writeup on how to <a href="http://blogs.msdn.com/kathykam/archive/2006/09/29/.NET-Format-String-102_3A00_-DateTime-Format-String.aspx">create your own specially formatted DateTime views</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/05/17/how-to-format-a-datetime-in-c-aspnet/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
