<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Sam Beauvois</title>
	
	<link>http://www.sambeauvois.be/blog</link>
	<description>general dev, .net and other stuff</description>
	<lastBuildDate>Wed, 11 Apr 2012 09:25:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/SamBeauvois" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="sambeauvois" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Quick tip : Enable wordpress mail feature on windows hosting</title>
		<link>http://www.sambeauvois.be/blog/2012/04/quick-tip-enable-wordpress-mail-feature-on-windows-hosting/</link>
		<comments>http://www.sambeauvois.be/blog/2012/04/quick-tip-enable-wordpress-mail-feature-on-windows-hosting/#comments</comments>
		<pubDate>Wed, 11 Apr 2012 09:25:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[WebSites]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=949</guid>
		<description><![CDATA[When you install a wordpress on a windows server (using IIS), the php mail() function may be disabled. A simple workaround is to use the wp mail smtp plugin. Install-it, then go to the settings : You can test it directly from the settings page. Tweet]]></description>
			<content:encoded><![CDATA[<p>When you install a wordpress on a windows server (using IIS), the php mail() function may be disabled.</p>
<p>A simple workaround is to use the <a href="http://wordpress.org/extend/plugins/wp-mail-smtp/" target="_blank">wp mail smtp plugin</a>.</p>
<p>Install-it, then go to the settings :</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2012/04/Advanced-Email-Options--480x326.jpg" target="_blank"><br />
<img class="alignnone size-medium wp-image-951" title="Advanced Email Options" src="http://www.sambeauvois.be/blog/wp-content/uploads/2012/04/Advanced-Email-Options--480x326.jpg" alt="" width="480" height="326" /></a></p>
<p>You can test it directly from the settings page.</p>
<div id="social-essentials" class="se_left"><div class="se_button se_button_small" style="width:85px;margin:0px 10px 0px 0px"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sambeauvois.be/blog/2012/04/quick-tip-enable-wordpress-mail-feature-on-windows-hosting/" data-text="Quick tip : Enable wordpress mail feature on windows hosting" data-via="SamBeauvois" data-counturl="http://www.sambeauvois.be/blog/2012/04/quick-tip-enable-wordpress-mail-feature-on-windows-hosting/" data-count="horizontal" data-lang="en">Tweet</a></div><div class="se_button se_button_small" style="width:72px;margin:0px 10px 0px 0px"><fb:like href="http://www.sambeauvois.be/blog/2012/04/quick-tip-enable-wordpress-mail-feature-on-windows-hosting/" send="false" layout="button_count" width="90" show_faces="false"></fb:like></div><div class="se_button se_button_small" style="width:60px;margin:0px 10px 0px 0px"><g:plusone size="medium" href="http://www.sambeauvois.be/blog/2012/04/quick-tip-enable-wordpress-mail-feature-on-windows-hosting/" count="true"></g:plusone></div><div class="se_button se_button_small" style="width:65px;margin:0px 10px 0px 0px"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.sambeauvois.be%2Fblog%2F2012%2F04%2Fquick-tip-enable-wordpress-mail-feature-on-windows-hosting%2F&media=&description=" class="se-pin-it-button" always-show-count="true" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div><div class="se_button se_button_small" style="margin:0px 10px 0px 0px"><su:badge layout="1" location="http://www.sambeauvois.be/blog/2012/04/quick-tip-enable-wordpress-mail-feature-on-windows-hosting/"></su:badge></div></div><div class="clear"></div><style type="text/css">#call_to_action h4{padding:0px 5px;}</style>]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2012/04/quick-tip-enable-wordpress-mail-feature-on-windows-hosting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple proxy to bypass cors</title>
		<link>http://www.sambeauvois.be/blog/2012/03/simple-proxy-to-bypass-cors/</link>
		<comments>http://www.sambeauvois.be/blog/2012/03/simple-proxy-to-bypass-cors/#comments</comments>
		<pubDate>Mon, 12 Mar 2012 13:16:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=935</guid>
		<description><![CDATA[CORS is a pain in the ass, all works great with chrome and firefox,but, as often, internet explorer doesn&#8217;t (it should work with ie 10 but you know&#8230;) So the proxy solution is simple and works, even if I would prefer a full html/jquery solution. To create a proxy for the get requests it&#8217;s simple. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Cross-origin_resource_sharing" target="_blank">CORS</a> is a pain in the ass, all works great with chrome and firefox,but, as often, internet explorer doesn&#8217;t (it should work with ie 10 but you know&#8230;)</p>
<p>So the proxy solution is simple and works, even if I would prefer a full html/jquery solution.</p>
<p>To create a proxy for the get requests it&#8217;s simple.</p>
<p>Create an asp.net webform, add this in the pageload.</p>
<pre class="brush: csharp; title: ; notranslate">

protected void Page_Load(object sender, EventArgs e)
{
 string serviceUrl = HttpContext.Current.Request.Params[&quot;service&quot;];
 if (string.IsNullOrEmpty(serviceUrl))
 {
  return;
 }

 string query = string.Empty;
 int firstParamIndex = HttpContext.Current.Request.Url.Query.IndexOf('&amp;');

 if (firstParamIndex &gt; -1)
 {
  query = HttpContext.Current.Request.Url.Query.Remove(0,firstParamIndex+1);
 }

 string service = serviceUrl + &quot;?&quot; + query;

 WebClient client = new WebClient();

 foreach (string headerKey in HttpContext.Current.Request.Headers.Keys)
 {
  try
  {
    client.Headers[headerKey] = HttpContext.Current.Request.Headers[headerKey];
  }
  catch (Exception ex)
  {
    System.Diagnostics.Debug.WriteLine(ex.Message);
  }
 }

 // specify encoding : avoid special characters to be unrecognized.
 client.Encoding = Encoding.UTF8;
 string resultString = client.DownloadString(service);

 // carriage returns.
 Response.Write(resultString.Replace(@&quot;\u000d\u000d&quot;, &quot;&lt;br/&gt;&quot;));
}
</pre>
<p>you can restrict some headers this way</p>
<pre class="brush: csharp; title: ; notranslate">

protected void Page_Load(object sender, EventArgs e)
{
 string[] allowedHeaders =
 {
  &quot;x-headers1&quot;,
  &quot;x-headers2&quot;,
  &quot;x-headers3&quot;
 };

 string serviceUrl = HttpContext.Current.Request.Params[&quot;service&quot;];
 if (string.IsNullOrEmpty(serviceUrl))
 {
  return;
 }

 string query = string.Empty;
 int firstParamIndex = HttpContext.Current.Request.Url.Query.IndexOf('&amp;');

 if (firstParamIndex &gt; -1)
 {
 query = HttpContext.Current.Request.Url.Query.Remove(0,firstParamIndex+1);
 }

 string service = serviceUrl + &quot;?&quot; + query;

 WebClient client = new WebClient();

 foreach (string headerKey in HttpContext.Current.Request.Headers.Keys)
 {
  if (allowedHeaders.Contains(headerKey))
  {
   try
   {
     client.Headers[headerKey] = HttpContext.Current.Request.Headers[headerKey];
   }
   catch (Exception ex)
   {
     System.Diagnostics.Debug.WriteLine(ex.Message);
   }
  }
 }

 // specify encoding : avoid special characters to be unrecognized.
 client.Encoding = Encoding.UTF8;
 string resultString = client.DownloadString(service);

 // carriage returns.
 Response.Write(resultString.Replace(@&quot;\u000d\u000d&quot;, &quot;&lt;br/&gt;&quot;));
}
</pre>
<p>In your web client :</p>
<pre class="brush: jscript; title: ; notranslate">

if (jQuery.support.ajax)
{
 $.ajax({
 headers:
 {
  &quot;x-headers1&quot;: &quot;header value 1&quot;,
  &quot;x-headers2&quot;: &quot;header value 2&quot;,
  &quot;x-headers3&quot;: &quot;header value 3&quot;
 },
 contentType: &quot;application/json; charset=utf-8&quot;,
 dataType: &quot;json&quot;,
 url: &quot;youpage.aspx?service=yourservicecompleteurl&quot;,
 data:
 {
  param1: value1,
  param2: value2
 },
 success: function (msg)
 {
  callback(msg);
 },
 error: function (errormsg)
 {
  if (errorCallback != 'undefined')
  {
  errorCallback(errormsg);
  }
 }
 });
 }
 else
 {
  alert('You browser doesnt support ajax.');
 }
</pre>
<p>I could use a webservice, but it would force me to do POST requests and I doesn&#8217;t want to make changes in my jscripts.</p>
<p><strong>[Update 20120405 ]</strong></p>
<p>Here is the full proxy code, which handles both get and post requests :</p>
<pre class="brush: csharp; title: ; notranslate">

using System;
using System.Linq;
using System.Web;
using System.Text;
using System.Net;
using System.IO;

namespace yournamespace
{
 public partial class Proxy : System.Web.UI.Page
{
/// &lt;summary&gt;
/// Definition of the headers to transfer.
/// &lt;/summary&gt;
private static string[] requiredHeaders =
{
&quot;x-your-required-header-1&quot;,
&quot;x-your-required-header-2&quot;,
&quot;x-your-required-header-3&quot;,
&quot;Content-type&quot;
};

/// &lt;summary&gt;
/// Overrides the onload event to handle get and post requests.
/// &lt;/summary&gt;
/// &lt;param name=&quot;e&quot;&gt;&lt;/param&gt;
protected override void OnLoad(EventArgs e)
{
bool isPost = string.Equals(HttpContext.Current.Request.HttpMethod, &quot;POST&quot;, StringComparison.OrdinalIgnoreCase);
string serviceUrl = HttpContext.Current.Request.Params[&quot;service&quot;];
if (string.IsNullOrEmpty(serviceUrl))
{
return;
}

string service = serviceUrl + (isPost ? &quot;&quot; : &quot;?&quot; + GetQuery());
WebClient client = new WebClient();

// copy required headers
foreach (string headerKey in HttpContext.Current.Request.Headers.Keys)
{
if (requiredHeaders.Contains(headerKey))
{
try
{
client.Headers[headerKey] = HttpContext.Current.Request.Headers[headerKey];
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}

// specify encoding : avoid special characters to be unrecognized.
client.Encoding = Encoding.UTF8;
string resultString = string.Empty;
try
{
if (isPost)
{
client.Headers.Add(HttpRequestHeader.ContentType, &quot;application/json&quot;);

using (MemoryStream str = new MemoryStream())
{
HttpContext.Current.Request.InputStream.CopyTo(str);
byte[] result = client.UploadData(service, str.ToArray());
resultString += Encoding.UTF8.GetString(result);
}
}
else // GET
{
resultString = client.DownloadString(service);
}
}
catch (WebException wex)
{
System.Diagnostics.Debug.Write(wex);
resultString = &quot;&lt;h1&gt;An error occured : &quot; + wex.Message + &quot;&lt;/h1&gt;&quot;;
resultString += &quot;&lt;br/&gt;Status : &quot; + wex.Status;
if (wex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse httpResponse = wex.Response as HttpWebResponse;
if (httpResponse != null)
{
resultString += string.Format(&quot;&lt;br/&gt;Status Code : {0}&quot;, httpResponse.StatusCode);
resultString += string.Format(&quot;&lt;br/&gt;Status Description : {0}&quot;, httpResponse.StatusDescription);
}
}
resultString += &quot;&lt;br/&gt;&lt;br/&gt;Requested service : &quot; + service;
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex);
resultString = &quot;&lt;h1&gt;An error occured : &quot; + ex.Message + &quot;&lt;/h1&gt;&quot;;
resultString = ex.GetFullMessage();
resultString += &quot;&lt;br/&gt;&lt;br/&gt;requested service : &quot; + service;
}
finally
{
client.Dispose();
}
Response.Clear();
Response.Write(resultString);
}

/// &lt;summary&gt;
/// Gets the query part.
/// &lt;/summary&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private static string GetQuery()
{
string query = string.Empty;
int firstParamIndex = HttpContext.Current.Request.Url.Query.IndexOf('&amp;');
if (firstParamIndex &gt; -1)
{
query = HttpContext.Current.Request.Url.Query.Remove(0, firstParamIndex + 1);
}

return query;
}
}
}
</pre>
<div id="social-essentials" class="se_left"><div class="se_button se_button_small" style="width:85px;margin:0px 10px 0px 0px"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sambeauvois.be/blog/2012/03/simple-proxy-to-bypass-cors/" data-text="Simple proxy to bypass cors" data-via="SamBeauvois" data-counturl="http://www.sambeauvois.be/blog/2012/03/simple-proxy-to-bypass-cors/" data-count="horizontal" data-lang="en">Tweet</a></div><div class="se_button se_button_small" style="width:72px;margin:0px 10px 0px 0px"><fb:like href="http://www.sambeauvois.be/blog/2012/03/simple-proxy-to-bypass-cors/" send="false" layout="button_count" width="90" show_faces="false"></fb:like></div><div class="se_button se_button_small" style="width:60px;margin:0px 10px 0px 0px"><g:plusone size="medium" href="http://www.sambeauvois.be/blog/2012/03/simple-proxy-to-bypass-cors/" count="true"></g:plusone></div><div class="se_button se_button_small" style="width:65px;margin:0px 10px 0px 0px"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.sambeauvois.be%2Fblog%2F2012%2F03%2Fsimple-proxy-to-bypass-cors%2F&media=&description=" class="se-pin-it-button" always-show-count="true" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div><div class="se_button se_button_small" style="margin:0px 10px 0px 0px"><su:badge layout="1" location="http://www.sambeauvois.be/blog/2012/03/simple-proxy-to-bypass-cors/"></su:badge></div></div><div class="clear"></div><style type="text/css">#call_to_action h4{padding:0px 5px;}</style>]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2012/03/simple-proxy-to-bypass-cors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic is magic : A solution to use the ASP.NET profile in web applications.</title>
		<link>http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/</link>
		<comments>http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 13:38:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=926</guid>
		<description><![CDATA[Profiles are not auto generated with Web Applications, so you can’t just add them in the web.config and use them directly. (unless …) The WebSite project do a magic thing : when you add properties to the profile in the web.config file, a class is autogenerated and allow the developer to access these properties. The [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Profiles are not auto generated with Web Applications, so you can’t just add them in the web.config and use them directly. (unless …)</p></blockquote>
<p>The WebSite project do a magic thing : when you add properties to the profile in the web.config file, a class is autogenerated and allow the developer to access these properties. The web application project don&#8217;t do that.</p>
<p>You can find some workarounds over the web, here is some</p>
<ul>
<li><a href="http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx" target="_blank">http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx</a></li>
<li><a href="http://leedumond.com/blog/asp-net-profiles-in-web-application-projects/" target="_blank">http://leedumond.com/blog/asp-net-profiles-in-web-application-projects/</a></li>
<li><a href="http://willmtz.blogspot.com/2011/09/using-aspnet-profile-feature-in-web.html" target="_blank">http://willmtz.blogspot.com/2011/09/using-aspnet-profile-feature-in-web.html</a></li>
<li><a href="http://stackoverflow.com/questions/426609/how-to-assign-profile-values" target="_blank">http://stackoverflow.com/questions/426609/how-to-assign-profile-values</a></li>
</ul>
<p>The &#8220;<a href="http://msdn.microsoft.com/en-us/library/dd264741.aspx" target="_blank">dynamic</a>&#8221; keyword help us here.</p>
<p>So, no more chit chat, here is the code snippets for a profile with 3 parameters : LastName, FirstName and GSM</p>
<p>web.config:</p>
<pre class="brush: xml; title: ; notranslate">

&lt;profile enabled=&quot;true&quot;&gt;
 &lt;providers&gt;
 &lt;clear/&gt;
 &lt;add name=&quot;AspNetSqlProfileProvider&quot; type=&quot;System.Web.Profile.SqlProfileProvider&quot; connectionStringName=&quot;ApplicationServices&quot; applicationName=&quot;MyApp&quot;/&gt;
 &lt;/providers&gt;
 &lt;properties&gt;
 &lt;add name=&quot;FirstName&quot; type=&quot;string&quot;/&gt;
 &lt;add name=&quot;LastName&quot; type=&quot;string&quot;/&gt;
 &lt;add name=&quot;GSM&quot; type=&quot;string&quot;/&gt;
 &lt;/properties&gt;
 &lt;/profile&gt;
</pre>
<p>A &#8220;business&#8221; class, I called it &#8220;ProfilePresenter&#8221;, it returns the profile of the logged user and allow modifications:</p>
<pre class="brush: csharp; title: ; notranslate">

public class ProfilePresenter
{
 public dynamic GetProfile()
 {
 return HttpContext.Current.Profile;
 }
 public dynamic UpdateProfile(string FirstName, string LastName, string GSM)
 {
 HttpContext.Current.Profile.SetPropertyValue(&quot;FirstName&quot;, FirstName);
 HttpContext.Current.Profile.SetPropertyValue(&quot;LastName&quot;, LastName);
 HttpContext.Current.Profile.SetPropertyValue(&quot;GSM&quot;, GSM);
 HttpContext.Current.Profile.Save();
 return HttpContext.Current.Profile;
 }
}
</pre>
<p>A page that uses the ProfilePresenter class : displaying the profile properties, and edit them</p>
<p>ObjectDataSource:</p>
<pre class="brush: csharp; title: ; notranslate">

&lt;asp:ObjectDataSource runat=&quot;server&quot; ID=&quot;ProfileODS&quot; TypeName=&quot;Board.Presenters.ProfilePresenter&quot;
 SelectMethod=&quot;GetProfile&quot; UpdateMethod=&quot;UpdateProfile&quot;/&gt;
&lt;pre&gt;</pre>
<p>FormView: Display part</p>
<pre class="brush: csharp; title: ; notranslate">
 &lt;asp:FormView runat=&quot;server&quot; ID=&quot;ProfileFV&quot; DataSourceID=&quot;ProfileODS&quot; RenderOuterTable=&quot;false&quot;&gt;
 &lt;EmptyDataTemplate&gt;
 &lt;p&gt;
 No profil datas
 &lt;/p&gt;
 &lt;/EmptyDataTemplate&gt;
 &lt;ItemTemplate&gt;
 &lt;h2&gt;
 Profil informations&lt;/h2&gt;
 &lt;div&gt;
 &lt;asp:Button runat=&quot;server&quot; ID=&quot;Edit&quot; CommandName=&quot;Edit&quot; Text=&quot;Edit&quot; /&gt;&lt;/div&gt;
 &lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th colspan=&quot;2&quot;&gt;
 Infos
 &lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;th&gt;
 LastName
 &lt;/th&gt;
 &lt;td&gt;
 &lt;%# Eval(&quot;LastName&quot;)%&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;th&gt;
 FirstName
 &lt;/th&gt;
 &lt;td&gt;
 &lt;%# Eval(&quot;FirstName&quot;)%&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;th&gt;
 GSM
 &lt;/th&gt;
 &lt;td&gt;
 &lt;%# Eval(&quot;GSM&quot;)%&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
 &lt;/table&gt;
 &lt;/ItemTemplate&gt;
</pre>
<p>FormView : the Edit part</p>
<pre class="brush: csharp; title: ; notranslate">
 &lt;EditItemTemplate&gt;
 &lt;h2&gt;
 Profil Update&lt;/h2&gt;
 &lt;div&gt;
 &lt;asp:Button runat=&quot;server&quot; ID=&quot;Edit&quot; CommandName=&quot;Cancel&quot; Text=&quot;Annuler&quot; /&gt;
 &lt;asp:Button runat=&quot;server&quot; ID=&quot;Button1&quot; CommandName=&quot;Update&quot; Text=&quot;Sauver&quot; /&gt;&lt;/div&gt;
 &lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th colspan=&quot;2&quot;&gt;
Infos
 &lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;th&gt;
 LastName
 &lt;/th&gt;
 &lt;td&gt;
 &lt;asp:TextBox runat=&quot;server&quot; ID=&quot;LastName&quot; Text='&lt;%# Bind(&quot;LastName&quot;)%&gt;' /&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;th&gt;
 FirstName
 &lt;/th&gt;
 &lt;td&gt;
 &lt;asp:TextBox runat=&quot;server&quot; ID=&quot;FirstName&quot; Text='&lt;%# Bind(&quot;FirstName&quot;)%&gt;' /&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;th&gt;
 GSM
 &lt;/th&gt;
 &lt;td&gt;
 &lt;asp:TextBox runat=&quot;server&quot; ID=&quot;GSM&quot; Text='&lt;%# Bind(&quot;GSM&quot;)%&gt;' /&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
 &lt;/table&gt;
 &lt;/EditItemTemplate&gt;
 &lt;/asp:FormView&gt;
</pre>
<p>A simple use :</p>
<pre class="brush: csharp; title: ; notranslate">

dynamic profil = HttpContext.Current.Profile;
if (profil != null)
{
 Console.Writeline(profil.GSM);
}
</pre>
<p>Another example : retrieve the gsm numbers from  members of a security role. It shows how to retrieve the profile of an other user</p>
<pre class="brush: csharp; title: ; notranslate">

string[] users = Roles.GetUsersInRole(role);

List&lt;string&gt; gsm = new List&lt;string&gt;();
foreach (string user in users)
{
 dynamic profil = HttpContext.Current.Profile;
 dynamic profi = profil.GetProfile(user);
 if (profi != null)
 {
 if (!string.IsNullOrEmpty(profi.GSM))
 {
 gsm.Add(profi.GSM);
 }
 }
}
</pre>
<p>Let me know what you think !</pre>
<div id="social-essentials" class="se_left"><div class="se_button se_button_small" style="width:85px;margin:0px 10px 0px 0px"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/" data-text="Dynamic is magic : A solution to use the ASP.NET profile in web applications." data-via="SamBeauvois" data-counturl="http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/" data-count="horizontal" data-lang="en">Tweet</a></div><div class="se_button se_button_small" style="width:72px;margin:0px 10px 0px 0px"><fb:like href="http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/" send="false" layout="button_count" width="90" show_faces="false"></fb:like></div><div class="se_button se_button_small" style="width:60px;margin:0px 10px 0px 0px"><g:plusone size="medium" href="http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/" count="true"></g:plusone></div><div class="se_button se_button_small" style="width:65px;margin:0px 10px 0px 0px"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.sambeauvois.be%2Fblog%2F2012%2F01%2Fdynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications%2F&media=&description=" class="se-pin-it-button" always-show-count="true" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div><div class="se_button se_button_small" style="margin:0px 10px 0px 0px"><su:badge layout="1" location="http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/"></su:badge></div></div><div class="clear"></div><style type="text/css">#call_to_action h4{padding:0px 5px;}</style>]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Different Ways to Transfer Data between pages by Peter Bromberg</title>
		<link>http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/</link>
		<comments>http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 08:46:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=922</guid>
		<description><![CDATA[It&#8217;s a common question when working with asp.net, and Peter Bromberg has done a nice job by grouping eight methods to do it! Read his article here : http://www.eggheadcafe.com/tutorials/asp-net/e653f028-01fb-4d0e-843b-058deae562a2/eight-different-ways-to-transfer-data-from-one-page-to-another-page.aspx Tweet]]></description>
			<content:encoded><![CDATA[<blockquote><p>It&#8217;s a common question when working with asp.net, and Peter Bromberg has done a nice job by grouping eight methods to do it!</p></blockquote>
<p>Read his article here :<a href="http://www.eggheadcafe.com/tutorials/asp-net/e653f028-01fb-4d0e-843b-058deae562a2/eight-different-ways-to-transfer-data-from-one-page-to-another-page.aspx" target="_blank"> http://www.eggheadcafe.com/tutorials/asp-net/e653f028-01fb-4d0e-843b-058deae562a2/eight-different-ways-to-transfer-data-from-one-page-to-another-page.aspx</a></p>
<div id="social-essentials" class="se_left"><div class="se_button se_button_small" style="width:85px;margin:0px 10px 0px 0px"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/" data-text="Different Ways to Transfer Data between pages by Peter Bromberg" data-via="SamBeauvois" data-counturl="http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/" data-count="horizontal" data-lang="en">Tweet</a></div><div class="se_button se_button_small" style="width:72px;margin:0px 10px 0px 0px"><fb:like href="http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/" send="false" layout="button_count" width="90" show_faces="false"></fb:like></div><div class="se_button se_button_small" style="width:60px;margin:0px 10px 0px 0px"><g:plusone size="medium" href="http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/" count="true"></g:plusone></div><div class="se_button se_button_small" style="width:65px;margin:0px 10px 0px 0px"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.sambeauvois.be%2Fblog%2F2011%2F12%2Fdifferent-ways-to-transfer-data-between-pages-by-peter-bromberg%2F&media=&description=" class="se-pin-it-button" always-show-count="true" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div><div class="se_button se_button_small" style="margin:0px 10px 0px 0px"><su:badge layout="1" location="http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/"></su:badge></div></div><div class="clear"></div><style type="text/css">#call_to_action h4{padding:0px 5px;}</style>]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bring a bit of the Subsonic power to Entity Framework by adding automatic audit and logical delete fields</title>
		<link>http://www.sambeauvois.be/blog/2011/11/bring-a-bit-of-the-subsonic-power-to-entity-framework-by-adding-automatic-audit-and-logical-delete-fields/</link>
		<comments>http://www.sambeauvois.be/blog/2011/11/bring-a-bit-of-the-subsonic-power-to-entity-framework-by-adding-automatic-audit-and-logical-delete-fields/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 16:04:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[Productivity]]></category>
		<category><![CDATA[SubSonic]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=915</guid>
		<description><![CDATA[Here is the code to do the same thing than in my previous article &#8220;Bring a bit of the Subsonic power to Linq to sql by adding automatic audit and logical delete fields&#8221; Tweet]]></description>
			<content:encoded><![CDATA[<p>Here is the code to do the same thing than in my previous article <a href="http://www.sambeauvois.be/blog/2010/09/bring-a-bit-of-the-subsonic-power-to-linq-to-sql-by-adding-automatic-audit-and-logical-delete-fields/" target="_blank">&#8220;Bring a bit of the Subsonic power to Linq to sql by adding automatic audit and logical delete fields&#8221;</a></p>
<pre class="brush: csharp; title: ; notranslate">

namespace YouNamespace.DAL
{
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Data;
 using System.Data.Common;
 using System.Data.Objects;
 using System.Linq;

 public partial class YOURCONTEXTEntities
 {
 /// &lt;summary&gt;
 /// System fields for automatic audit and logical delete
 /// &lt;/summary&gt;
 private struct SystemFields
 {
 public const string CreatedOn = &quot;CREATEDON&quot;;
 public const string ModifiedOn = &quot;MODIFIEDON&quot;;
 public const string CreatedBy = &quot;CREATEDBY&quot;;
 public const string ModifiedBy = &quot;MODIFIEDBY&quot;;
 public const string IsDeleted = &quot;ISDELETED&quot;;
 }

 /// &lt;summary&gt;
 /// Overriding the SaveChanges method to automaticaly set system fields if any.
 /// &lt;/summary&gt;
 /// &lt;param name=&quot;options&quot;&gt;&lt;/param&gt;
 /// &lt;returns&gt;&lt;/returns&gt;
 public override int SaveChanges(System.Data.Objects.SaveOptions options)
 {
 IEnumerable&lt;ObjectStateEntry&gt; newEntries = this.ObjectStateManager.GetObjectStateEntries(EntityState.Added);

 foreach (ObjectStateEntry entry in newEntries)
 {
 ReadOnlyCollection&lt;FieldMetadata&gt; fieldsMetaData = entry.CurrentValues
 .DataRecordInfo.FieldMetadata;

 FieldMetadata createdOnField = fieldsMetaData
 .Where(f =&gt; string.Equals(f.FieldType.Name, SystemFields.CreatedOn, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (createdOnField.FieldType != null)
 {
 entry.CurrentValues.SetValue(createdOnField.Ordinal, DateTime.Now);
 }

 FieldMetadata createdByField = fieldsMetaData
 .Where(f =&gt; string.Equals(f.FieldType.Name, SystemFields.CreatedBy, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (createdByField.FieldType != null)
 {
 entry.CurrentValues.SetValue(createdByField.Ordinal, &quot;Sam&quot;);
 }

 FieldMetadata deletedField = fieldsMetaData
 .Where(f =&gt; string.Equals(f.FieldType.Name, SystemFields.IsDeleted, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (deletedField.FieldType != null)
 {
 entry.CurrentValues.SetValue(deletedField.Ordinal, false);
 }
 }

 IEnumerable&lt;ObjectStateEntry&gt; modifiedEntries = this.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
 foreach (ObjectStateEntry entry in modifiedEntries)
 {
 ReadOnlyCollection&lt;FieldMetadata&gt; fieldsMetaData = entry.CurrentValues
 .DataRecordInfo.FieldMetadata;

 FieldMetadata createdOnField = fieldsMetaData
 .Where(f =&gt; string.Equals(f.FieldType.Name, SystemFields.ModifiedOn, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (createdOnField.FieldType != null)
 {
 entry.CurrentValues.SetValue(createdOnField.Ordinal, DateTime.Now);
 }

 FieldMetadata createdByField = fieldsMetaData
 .Where(f =&gt; string.Equals(f.FieldType.Name, SystemFields.ModifiedBy, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (createdByField.FieldType != null)
 {
 entry.CurrentValues.SetValue(createdByField.Ordinal, &quot;Sam&quot;);
 }
 }

 IEnumerable&lt;ObjectStateEntry&gt; deletedEntries = this.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted);
 foreach (ObjectStateEntry entry in deletedEntries)
 {
 // change from deleted to modified (!important)
 this.ObjectStateManager.ChangeObjectState(entry.Entity, EntityState.Modified);

 ReadOnlyCollection&lt;FieldMetadata&gt; fieldsMetaData = entry.CurrentValues
 .DataRecordInfo.FieldMetadata;

 FieldMetadata deletedField = fieldsMetaData
 .Where(f =&gt; string.Equals(f.FieldType.Name, SystemFields.IsDeleted, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

 if (deletedField.FieldType != null)
 {
 entry.CurrentValues.SetValue(deletedField.Ordinal, true);
 }
else
 {
 // change back from modified to deleted (!important)
 this.ObjectStateManager.ChangeObjectState(entry.Entity, EntityState.Deleted);
 }
 }

 return base.SaveChanges(options);
 }
 }
}
</pre>
<div id="social-essentials" class="se_left"><div class="se_button se_button_small" style="width:85px;margin:0px 10px 0px 0px"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sambeauvois.be/blog/2011/11/bring-a-bit-of-the-subsonic-power-to-entity-framework-by-adding-automatic-audit-and-logical-delete-fields/" data-text="Bring a bit of the Subsonic power to Entity Framework by adding automatic audit and logical delete fields" data-via="SamBeauvois" data-counturl="http://www.sambeauvois.be/blog/2011/11/bring-a-bit-of-the-subsonic-power-to-entity-framework-by-adding-automatic-audit-and-logical-delete-fields/" data-count="horizontal" data-lang="en">Tweet</a></div><div class="se_button se_button_small" style="width:72px;margin:0px 10px 0px 0px"><fb:like href="http://www.sambeauvois.be/blog/2011/11/bring-a-bit-of-the-subsonic-power-to-entity-framework-by-adding-automatic-audit-and-logical-delete-fields/" send="false" layout="button_count" width="90" show_faces="false"></fb:like></div><div class="se_button se_button_small" style="width:60px;margin:0px 10px 0px 0px"><g:plusone size="medium" href="http://www.sambeauvois.be/blog/2011/11/bring-a-bit-of-the-subsonic-power-to-entity-framework-by-adding-automatic-audit-and-logical-delete-fields/" count="true"></g:plusone></div><div class="se_button se_button_small" style="width:65px;margin:0px 10px 0px 0px"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.sambeauvois.be%2Fblog%2F2011%2F11%2Fbring-a-bit-of-the-subsonic-power-to-entity-framework-by-adding-automatic-audit-and-logical-delete-fields%2F&media=&description=" class="se-pin-it-button" always-show-count="true" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div><div class="se_button se_button_small" style="margin:0px 10px 0px 0px"><su:badge layout="1" location="http://www.sambeauvois.be/blog/2011/11/bring-a-bit-of-the-subsonic-power-to-entity-framework-by-adding-automatic-audit-and-logical-delete-fields/"></su:badge></div></div><div class="clear"></div><style type="text/css">#call_to_action h4{padding:0px 5px;}</style>]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2011/11/bring-a-bit-of-the-subsonic-power-to-entity-framework-by-adding-automatic-audit-and-logical-delete-fields/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Enum HasFlag method extension for &lt; 4.0 Framework</title>
		<link>http://www.sambeauvois.be/blog/2011/08/enum-hasflag-method-extension-for-4-0-framework/</link>
		<comments>http://www.sambeauvois.be/blog/2011/08/enum-hasflag-method-extension-for-4-0-framework/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 19:52:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=909</guid>
		<description><![CDATA[In the same idea than the previous article, here is an extension method to mimic the 4.0 HasFlag method (http://msdn.microsoft.com/en-us/library/system.enum.hasflag.aspx). Tweet]]></description>
			<content:encoded><![CDATA[<p>In the same idea than the previous article, here is an extension method to mimic the 4.0 HasFlag method (<a href="http://msdn.microsoft.com/en-us/library/system.enum.hasflag.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/system.enum.hasflag.aspx</a>).</p>
<pre class="brush: csharp; title: ; notranslate">
     /// &lt;summary&gt;
    /// Extentions for enums.
    /// &lt;/summary&gt;
    public static class EnumExtensions
    {
        /// &lt;summary&gt;
        /// A FX 3.5 way to mimic the FX4 &quot;HasFlag&quot; method.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;variable&quot;&gt;The tested enum.&lt;/param&gt;
        /// &lt;param name=&quot;value&quot;&gt;The value to test.&lt;/param&gt;
        /// &lt;returns&gt;True if the flag is set. Otherwise false.&lt;/returns&gt;
        public static bool HasFlag(this Enum variable, Enum value)
        {
            // check if from the same type.
            if (variable.GetType() != value.GetType())
            {
                throw new ArgumentException(&quot;The checked flag is not from the same type as the checked variable.&quot;);
            }

            Convert.ToUInt64(value);
            ulong num = Convert.ToUInt64(value);
            ulong num2 = Convert.ToUInt64(variable);

            return (num2 &amp; num) == num;
        }
    }
</pre>
<div id="social-essentials" class="se_left"><div class="se_button se_button_small" style="width:85px;margin:0px 10px 0px 0px"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sambeauvois.be/blog/2011/08/enum-hasflag-method-extension-for-4-0-framework/" data-text="Enum HasFlag method extension for  4.0 Framework" data-via="SamBeauvois" data-counturl="http://www.sambeauvois.be/blog/2011/08/enum-hasflag-method-extension-for-4-0-framework/" data-count="horizontal" data-lang="en">Tweet</a></div><div class="se_button se_button_small" style="width:72px;margin:0px 10px 0px 0px"><fb:like href="http://www.sambeauvois.be/blog/2011/08/enum-hasflag-method-extension-for-4-0-framework/" send="false" layout="button_count" width="90" show_faces="false"></fb:like></div><div class="se_button se_button_small" style="width:60px;margin:0px 10px 0px 0px"><g:plusone size="medium" href="http://www.sambeauvois.be/blog/2011/08/enum-hasflag-method-extension-for-4-0-framework/" count="true"></g:plusone></div><div class="se_button se_button_small" style="width:65px;margin:0px 10px 0px 0px"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.sambeauvois.be%2Fblog%2F2011%2F08%2Fenum-hasflag-method-extension-for-4-0-framework%2F&media=&description=" class="se-pin-it-button" always-show-count="true" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div><div class="se_button se_button_small" style="margin:0px 10px 0px 0px"><su:badge layout="1" location="http://www.sambeauvois.be/blog/2011/08/enum-hasflag-method-extension-for-4-0-framework/"></su:badge></div></div><div class="clear"></div><style type="text/css">#call_to_action h4{padding:0px 5px;}</style>]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2011/08/enum-hasflag-method-extension-for-4-0-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stream.CopyTo method for &lt; 4.0 framework</title>
		<link>http://www.sambeauvois.be/blog/2011/08/stream-copyto-method-for-4-0-framework/</link>
		<comments>http://www.sambeauvois.be/blog/2011/08/stream-copyto-method-for-4-0-framework/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 18:47:19 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=902</guid>
		<description><![CDATA[The .NET Framework 4.0 introduced the CopyTo() method for the Stream class. (http://msdn.microsoft.com/en-us/library/system.io.stream.copyto.aspx) You copy a stream into one other this way: If you are stucked with a lower version of the framework, you might want to use it anyway. A bit of reflector is realy usefull to see how the implementation is done in [...]]]></description>
			<content:encoded><![CDATA[<p>The .NET Framework 4.0 introduced the CopyTo() method for the Stream class. (<a href="http://msdn.microsoft.com/en-us/library/system.io.stream.copyto.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/system.io.stream.copyto.aspx</a>)</p>
<p>You copy a stream into one other this way:</p>
<pre class="brush: csharp; title: ; notranslate">

   MemoryStream memoryStream = new MemoryStream();
   using (Stream stream = new FileStream(@&quot;c:\input.txt&quot;, FileMode.Open))
   {
      stream.CopyTo(memoryStream);
   }
</pre>
<p>If you are stucked with a lower version of the framework, you might want to use it anyway.</p>
<p>A bit of reflector is realy usefull to see how the implementation is done in 4.0.</p>
<p>The CopyTo method :</p>
<pre class="brush: csharp; title: ; notranslate">

public void CopyTo(Stream destination)
{
    if (destination == null)
    {
        throw new ArgumentNullException(&quot;destination&quot;);
    }
    if (!this.CanRead &amp;&amp; !this.CanWrite)
    {
        throw new ObjectDisposedException(null, Environment.GetResourceString(&quot;ObjectDisposed_StreamClosed&quot;));
    }
    if (!destination.CanRead &amp;&amp; !destination.CanWrite)
    {
        throw new ObjectDisposedException(&quot;destination&quot;, Environment.GetResourceString(&quot;ObjectDisposed_StreamClosed&quot;));
    }
    if (!this.CanRead)
    {
        throw new NotSupportedException(Environment.GetResourceString(&quot;NotSupported_UnreadableStream&quot;));
    }
    if (!destination.CanWrite)
    {
        throw new NotSupportedException(Environment.GetResourceString(&quot;NotSupported_UnwritableStream&quot;));
    }
    this.InternalCopyTo(destination, 0x1000);
}
</pre>
<p>We see that the InternalCopyTo is used :</p>
<pre class="brush: csharp; title: ; notranslate">

private void InternalCopyTo(Stream destination, int bufferSize)
{
    int num;
    byte[] buffer = new byte[bufferSize];
    while ((num = this.Read(buffer, 0, buffer.Length)) != 0)
    {
        destination.Write(buffer, 0, num);
    }
}
</pre>
<p>Remark :<br />
0&#215;1000 is in hexadecimal, which corresponds to 4096 in decimal.</p>
<p>So we can retrieve that for use with lower versions of the language</p>
<p>For .NET FX >= 3.0 you can create an extension method</p>
<pre class="brush: csharp; title: ; notranslate">

    using System;
    using System.IO;

    /// &lt;summary&gt;
    /// Extension methods for streams.
    /// &lt;/summary&gt;
    public static class StreamExtensions
    {
        /// &lt;summary&gt;
        /// Reads all the bytes from the current stream and writes them to the destination stream.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;original&quot;&gt;The current stream.&lt;/param&gt;
        /// &lt;param name=&quot;destination&quot;&gt;The stream that will contain the contents of the current stream.&lt;/param&gt;
        /// &lt;exception cref=&quot;System.ArgumentNullException&quot;&gt;Destination is null.&lt;/exception&gt;
        /// &lt;exception cref=&quot;System.NotSupportedException&quot;&gt;The current stream does not support reading.-or-destination does not support Writing.&lt;/exception&gt;
        /// &lt;exception cref=&quot;System.ObjectDisposedException&quot;&gt;Either the current stream or destination were closed before the System.IO.Stream.CopyTo(System.IO.Stream) method was called.&lt;/exception&gt;
        /// &lt;exception cref=&quot;System.IO.IOException&quot;&gt;An I/O error occurred.&lt;/exception&gt;
        public static void CopyTo(this Stream original, Stream destination)
        {
            if (destination == null)
            {
                throw new ArgumentNullException(&quot;destination&quot;);
            }
            if (!original.CanRead &amp;&amp; !original.CanWrite)
            {
                throw new ObjectDisposedException(&quot;ObjectDisposedException&quot;);
            }
            if (!destination.CanRead &amp;&amp; !destination.CanWrite)
            {
                throw new ObjectDisposedException(&quot;ObjectDisposedException&quot;);
            }
            if (!original.CanRead)
            {
                throw new NotSupportedException(&quot;NotSupportedException source&quot;);
            }
            if (!destination.CanWrite)
            {
                throw new NotSupportedException(&quot;NotSupportedException destination&quot;);
            }

            byte[] array = new byte[4096];
            int count;
            while ((count = original.Read(array, 0, array.Length)) != 0)
            {
                destination.Write(array, 0, count);
            }
        }
    }
</pre>
<p>You use it this way (same ways as the FX 4.0):</p>
<pre class="brush: csharp; title: ; notranslate">

   MemoryStream memoryStream = new MemoryStream();
   using (Stream stream = new FileStream(@&quot;c:\input.txt&quot;, FileMode.Open))
   {
      stream.CopyTo(memoryStream);
   }
</pre>
<p>For lower version, you can create an helper class.</p>
<pre class="brush: csharp; title: ; notranslate">

    using System;
    using System.IO;

    /// &lt;summary&gt;
    /// An helper class for streams.
    /// &lt;/summary&gt;
    public class StreamHelper
    {
        /// &lt;summary&gt;
        /// Reads all the bytes from the current stream and writes them to the destination stream.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;original&quot;&gt;The original stream.&lt;/param&gt;
        /// &lt;param name=&quot;destination&quot;&gt;The stream that will contain the contents of the current stream.&lt;/param&gt;
        /// &lt;exception cref=&quot;System.ArgumentNullException&quot;&gt;Destination is null.&lt;/exception&gt;
        /// &lt;exception cref=&quot;System.NotSupportedException&quot;&gt;The current stream does not support reading.-or-destination does not support Writing.&lt;/exception&gt;
        /// &lt;exception cref=&quot;System.ObjectDisposedException&quot;&gt;Either the current stream or destination were closed before the System.IO.Stream.CopyTo(System.IO.Stream) method was called.&lt;/exception&gt;
        /// &lt;exception cref=&quot;System.IO.IOException&quot;&gt;An I/O error occurred.&lt;/exception&gt;
        public static void CopyStreamTo(Stream original, Stream destination)
        {
            if (destination == null)
            {
                throw new ArgumentNullException(&quot;destination&quot;);
            }
            if (!original.CanRead &amp;&amp; !original.CanWrite)
            {
                throw new ObjectDisposedException(&quot;ObjectDisposedException&quot;);
            }
            if (!destination.CanRead &amp;&amp; !destination.CanWrite)
            {
                throw new ObjectDisposedException(&quot;ObjectDisposedException&quot;);
            }
            if (!original.CanRead)
            {
                throw new NotSupportedException(&quot;NotSupportedException source&quot;);
            }
            if (!destination.CanWrite)
            {
                throw new NotSupportedException(&quot;NotSupportedException destination&quot;);
            }

            byte[] array = new byte[4096];
            int count;
            while ((count = original.Read(array, 0, array.Length)) != 0)
            {
                destination.Write(array, 0, count);
            }
        }
    }
</pre>
<p>Use it this way:</p>
<pre class="brush: csharp; title: ; notranslate">

   MemoryStream memoryStream = new MemoryStream();
   using (Stream stream = new FileStream(@&quot;c:\input.txt&quot;, FileMode.Open))
   {
        StreamHelper.CopyStreamTo(stream, memoryStream);
   }
</pre>
<div id="social-essentials" class="se_left"><div class="se_button se_button_small" style="width:85px;margin:0px 10px 0px 0px"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sambeauvois.be/blog/2011/08/stream-copyto-method-for-4-0-framework/" data-text="Stream.CopyTo method for  4.0 framework" data-via="SamBeauvois" data-counturl="http://www.sambeauvois.be/blog/2011/08/stream-copyto-method-for-4-0-framework/" data-count="horizontal" data-lang="en">Tweet</a></div><div class="se_button se_button_small" style="width:72px;margin:0px 10px 0px 0px"><fb:like href="http://www.sambeauvois.be/blog/2011/08/stream-copyto-method-for-4-0-framework/" send="false" layout="button_count" width="90" show_faces="false"></fb:like></div><div class="se_button se_button_small" style="width:60px;margin:0px 10px 0px 0px"><g:plusone size="medium" href="http://www.sambeauvois.be/blog/2011/08/stream-copyto-method-for-4-0-framework/" count="true"></g:plusone></div><div class="se_button se_button_small" style="width:65px;margin:0px 10px 0px 0px"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.sambeauvois.be%2Fblog%2F2011%2F08%2Fstream-copyto-method-for-4-0-framework%2F&media=&description=" class="se-pin-it-button" always-show-count="true" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div><div class="se_button se_button_small" style="margin:0px 10px 0px 0px"><su:badge layout="1" location="http://www.sambeauvois.be/blog/2011/08/stream-copyto-method-for-4-0-framework/"></su:badge></div></div><div class="clear"></div><style type="text/css">#call_to_action h4{padding:0px 5px;}</style>]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2011/08/stream-copyto-method-for-4-0-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Chanel 9 | Rx Workshop: Unified Programming Model</title>
		<link>http://www.sambeauvois.be/blog/2011/08/chanel-9-rx-workshop-unified-programming-model/</link>
		<comments>http://www.sambeauvois.be/blog/2011/08/chanel-9-rx-workshop-unified-programming-model/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 14:46:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Quick posts]]></category>
		<category><![CDATA[Reminders]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=912</guid>
		<description><![CDATA[Learn how to wrap existing event sources, including tasks, asynchronous methods, .NET events, etc. in observable sequences. Tweet]]></description>
			<content:encoded><![CDATA[<p><iframe style="height:288px;width:512px" src="http://channel9.msdn.com/Series/Rx-Workshop/Rx-Workshop-Unified-Programming-Model/player?w=512&#038;h=288" frameBorder="0" scrolling="no" ></iframe></p>
<p>Learn how to wrap existing event sources, including tasks, asynchronous methods, .NET events, etc. in observable sequences.</p>
<div id="social-essentials" class="se_left"><div class="se_button se_button_small" style="width:85px;margin:0px 10px 0px 0px"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sambeauvois.be/blog/2011/08/chanel-9-rx-workshop-unified-programming-model/" data-text="Chanel 9 | Rx Workshop: Unified Programming Model" data-via="SamBeauvois" data-counturl="http://www.sambeauvois.be/blog/2011/08/chanel-9-rx-workshop-unified-programming-model/" data-count="horizontal" data-lang="en">Tweet</a></div><div class="se_button se_button_small" style="width:72px;margin:0px 10px 0px 0px"><fb:like href="http://www.sambeauvois.be/blog/2011/08/chanel-9-rx-workshop-unified-programming-model/" send="false" layout="button_count" width="90" show_faces="false"></fb:like></div><div class="se_button se_button_small" style="width:60px;margin:0px 10px 0px 0px"><g:plusone size="medium" href="http://www.sambeauvois.be/blog/2011/08/chanel-9-rx-workshop-unified-programming-model/" count="true"></g:plusone></div><div class="se_button se_button_small" style="width:65px;margin:0px 10px 0px 0px"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.sambeauvois.be%2Fblog%2F2011%2F08%2Fchanel-9-rx-workshop-unified-programming-model%2F&media=&description=" class="se-pin-it-button" always-show-count="true" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div><div class="se_button se_button_small" style="margin:0px 10px 0px 0px"><su:badge layout="1" location="http://www.sambeauvois.be/blog/2011/08/chanel-9-rx-workshop-unified-programming-model/"></su:badge></div></div><div class="clear"></div><style type="text/css">#call_to_action h4{padding:0px 5px;}</style>]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2011/08/chanel-9-rx-workshop-unified-programming-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET videos : Choosing the Right Programming Model</title>
		<link>http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/</link>
		<comments>http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/#comments</comments>
		<pubDate>Sun, 12 Jun 2011 20:34:09 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=897</guid>
		<description><![CDATA[As mentioned by Kris van der Mast on the asp.net forums, this video was on the get started page before, but don&#8217;t appears to be there anymore. This video is great to know in 5 minutes which model is good for you. So, because I don&#8217;t want to search for it, I post this video [...]]]></description>
			<content:encoded><![CDATA[<p>As mentioned by <a href="http://blog.krisvandermast.com/" target="_blank">Kris van der Mast</a> on the asp.net forums, this video was on the get started page before, but don&#8217;t appears to be there anymore.</p>
<p>This video is great to know in 5 minutes which model is good for you.</p>
<p>So, because I don&#8217;t want to search for it, I post this video here :</p>
<p><object style="width:400px;height:338px;" autoupdate="true" data="data:application/x-silverlight-2," type="application/x-silverlight-2"><param value="2.0.31005.0" name="MinRuntimeVersion"/><param name="source" value="http://www.asp.net/clientbin/mediaplayer/MSCommunities.MediaPlayer.xap" /><param value="videoid=24686" name="InitParams"/><a href="http://go2.microsoft.com/fwlink/?LinkID=114576&amp;v=2.0"><img style="border-width: 0px;" alt="Install Silverlight" src="http://i2.asp.net/common/static-asp/asp.net/videos/silverlight.mediaplayer/slplayer_disabled.png?cdn_id=04302010"/></a></object></p>
<p>(<a href="http://www.asp.net/general/videos/choosing-the-right-programming-model" target="_blank">direct link</a>)</p>
<div id="social-essentials" class="se_left"><div class="se_button se_button_small" style="width:85px;margin:0px 10px 0px 0px"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/" data-text="ASP.NET videos : Choosing the Right Programming Model" data-via="SamBeauvois" data-counturl="http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/" data-count="horizontal" data-lang="en">Tweet</a></div><div class="se_button se_button_small" style="width:72px;margin:0px 10px 0px 0px"><fb:like href="http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/" send="false" layout="button_count" width="90" show_faces="false"></fb:like></div><div class="se_button se_button_small" style="width:60px;margin:0px 10px 0px 0px"><g:plusone size="medium" href="http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/" count="true"></g:plusone></div><div class="se_button se_button_small" style="width:65px;margin:0px 10px 0px 0px"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.sambeauvois.be%2Fblog%2F2011%2F06%2Fasp-net-videos-choosing-the-right-programming-model%2F&media=&description=" class="se-pin-it-button" always-show-count="true" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div><div class="se_button se_button_small" style="margin:0px 10px 0px 0px"><su:badge layout="1" location="http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/"></su:badge></div></div><div class="clear"></div><style type="text/css">#call_to_action h4{padding:0px 5px;}</style>]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>One line how to’s : Save text in a file, or read text from a file.</title>
		<link>http://www.sambeauvois.be/blog/2011/06/one-line-how-tos-save-text-in-a-file-or-read-text-from-a-file/</link>
		<comments>http://www.sambeauvois.be/blog/2011/06/one-line-how-tos-save-text-in-a-file-or-read-text-from-a-file/#comments</comments>
		<pubDate>Sun, 12 Jun 2011 20:25:08 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=733</guid>
		<description><![CDATA[Sometimes your solution lies in a single line. How to save a string in a text file How to read a text file How does it work ? A bit or Reflector gives us : For the write method and the &#8220;InternalWriteAllText&#8221; method : For the Read method : And the &#8220;InternalReadAllText&#8221; : As you [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Sometimes your solution lies in a single line.</p></blockquote>
<h4>How to save a string in a text file</h4>
<pre class="brush: csharp; light: true; title: ; notranslate">

System.IO.File.WriteAllText(&quot;c://yourfile.txt&quot;,
&quot;here is the content of my file !&quot;);
</pre>
<h4>How to read a text file</h4>
<pre class="brush: csharp; light: true; title: ; notranslate">

string content =  System.IO.File.ReadAllText(&quot;c://yourfile.txt&quot;);
</pre>
<h5>How does it work ?</h5>
<p>A bit or Reflector gives us :</p>
<p>For the write method</p>
<pre class="brush: csharp; title: ; notranslate">

public static void WriteAllText(string path, string contents)
{
if (path == null)
{
throw new ArgumentNullException(&quot;path&quot;);
}
if (path.Length == 0)
{
throw new ArgumentException(Environment.GetResourceString(&quot;Argument_EmptyPath&quot;));
}
InternalWriteAllText(path, contents, StreamWriter.UTF8NoBOM);
}
</pre>
<p>and the &#8220;InternalWriteAllText&#8221; method :</p>
<pre class="brush: csharp; title: ; notranslate">

private static void InternalWriteAllText(string path, string contents, Encoding encoding)
{
using (StreamWriter writer = new StreamWriter(path, false, encoding))
{
writer.Write(contents);
}
}
</pre>
<p>For the Read method :</p>
<pre class="brush: csharp; title: ; notranslate">
public static string ReadAllText(string path)
{
    if (path == null)
    {
        throw new ArgumentNullException(&quot;path&quot;);
    }
    if (path.Length == 0)
    {
        throw new ArgumentException(Environment.GetResourceString(&quot;Argument_EmptyPath&quot;));
    }
    return InternalReadAllText(path, Encoding.UTF8);
}
</pre>
<p>And the &#8220;InternalReadAllText&#8221; :</p>
<pre class="brush: csharp; title: ; notranslate">
private static string InternalReadAllText(string path, Encoding encoding)
{
    using (StreamReader reader = new StreamReader(path, encoding))
    {
        return reader.ReadToEnd();
    }
}
</pre>
<p>As you can see it don&#8217;t check if the file exists, so you have to check it by yourself :</p>
<pre class="brush: csharp; title: ; notranslate">

if (System.IO.File.Exists(&quot;your file path&quot;))
{
string content = System.IO.File.ReadAllText(&quot;your file path&quot;);
}
</pre>
<div id="_mcePaste" class="mcePaste" style="position: absolute; left: -10000px; top: 313px; width: 1px; height: 1px; overflow: hidden;"><span style="color: #1000a0;">public</span> <span style="color: #1000a0;">static</span> <a title="System.String" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.String">string</a> <strong><a class="bold" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.IO.File/ReadAllText%28String%29:String">ReadAllText</a></strong>(<a title="System.String" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.String">string</a> path) {     <span style="color: #1000a0;">if</span> (<a title="string path; // Parameter">path</a> == <span style="color: #800000;">null</span>)     {         <span style="color: #1000a0;">throw</span> <span style="color: #1000a0;">new</span> <a title="System.ArgumentNullException.ArgumentNullException(string paramName);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.ArgumentNullException/.ctor%28String%29">ArgumentNullException</a>(<span style="color: #800000;">&#8220;path&#8221;</span>);     }     <span style="color: #1000a0;">if</span> (<a title="string path; // Parameter">path</a>.<a title="int System.String.Length { ... }" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.String/property:Length:Int32">Length</a> == <span style="color: #800000;">0</span>)     {         <span style="color: #1000a0;">throw</span> <span style="color: #1000a0;">new</span> <a title="System.ArgumentException.ArgumentException(string message);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.ArgumentException/.ctor%28String%29">ArgumentException</a>(<a title="System.Environment" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Environment">Environment</a>.<a title="string System.Environment.GetResourceString(string key);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Environment/GetResourceString%28String%29:String">GetResourceString</a>(<span style="color: #800000;">&#8220;Argument_EmptyPath&#8221;</span>));     }     <span style="color: #1000a0;">return</span> <a title="string System.IO.File.InternalReadAllText(string path, Encoding encoding);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.IO.File/InternalReadAllText%28String,System.Text.Encoding%29:String">InternalReadAllText</a>(<a title="string path; // Parameter">path</a>, <a title="System.Text.Encoding" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Text.Encoding">Encoding</a>.<a title="Encoding System.Text.Encoding.UTF8 { ... }" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Text.Encoding/property:UTF8:System.Text.Encoding">UTF8</a>); }</div>
<div id="social-essentials" class="se_left"><div class="se_button se_button_small" style="width:85px;margin:0px 10px 0px 0px"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sambeauvois.be/blog/2011/06/one-line-how-tos-save-text-in-a-file-or-read-text-from-a-file/" data-text="One line how to&#8217;s : Save text in a file, or read text from a file." data-via="SamBeauvois" data-counturl="http://www.sambeauvois.be/blog/2011/06/one-line-how-tos-save-text-in-a-file-or-read-text-from-a-file/" data-count="horizontal" data-lang="en">Tweet</a></div><div class="se_button se_button_small" style="width:72px;margin:0px 10px 0px 0px"><fb:like href="http://www.sambeauvois.be/blog/2011/06/one-line-how-tos-save-text-in-a-file-or-read-text-from-a-file/" send="false" layout="button_count" width="90" show_faces="false"></fb:like></div><div class="se_button se_button_small" style="width:60px;margin:0px 10px 0px 0px"><g:plusone size="medium" href="http://www.sambeauvois.be/blog/2011/06/one-line-how-tos-save-text-in-a-file-or-read-text-from-a-file/" count="true"></g:plusone></div><div class="se_button se_button_small" style="width:65px;margin:0px 10px 0px 0px"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.sambeauvois.be%2Fblog%2F2011%2F06%2Fone-line-how-tos-save-text-in-a-file-or-read-text-from-a-file%2F&media=&description=" class="se-pin-it-button" always-show-count="true" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div><div class="se_button se_button_small" style="margin:0px 10px 0px 0px"><su:badge layout="1" location="http://www.sambeauvois.be/blog/2011/06/one-line-how-tos-save-text-in-a-file-or-read-text-from-a-file/"></su:badge></div></div><div class="clear"></div><style type="text/css">#call_to_action h4{padding:0px 5px;}</style>]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2011/06/one-line-how-tos-save-text-in-a-file-or-read-text-from-a-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

