<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Shahed Khan (MVP)</title>
        <link>http://geekswithblogs.net/shahed/Default.aspx</link>
        <description>blog</description>
        <language>en-US</language>
        <copyright>Shahed Khan</copyright>
        <managingEditor>shahed.khan@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <image>
            <title>Shahed Khan (MVP)</title>
            <url>http://geekswithblogs.net/images/RSS2Image.gif</url>
            <link>http://geekswithblogs.net/shahed/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>ASP.NET MVC Tips: 301 Redirect non-www versions of URL to www.</title>
            <link>http://geekswithblogs.net/shahed/archive/2009/05/19/132204.aspx</link>
            <description>&lt;style&gt;&lt;![CDATA[






.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;  &lt;p&gt;Search Engine Optimization guides, recommends to have one version of a URL of the same content. Search engines may pickup www and non-www versions of URL as 2 separate URLs, i.e. &lt;a href="http://xyz.com/page1"&gt;http://xyz.com/page1&lt;/a&gt; may be considered different to &lt;a href="http://www.xyz.com/page1"&gt;http://www.xyz.com/page1&lt;/a&gt;. It is a good idea to pick on of these URLs as preferred and use 301 redirects to send traffic from the other URLS to the preferred URL. Today we will pickup www version of URL as our preferred URL and look at how we can redirect the non-www version of the URL to our preferred URL structure using ASP.NET MVC.     &lt;br /&gt;    &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETMVCTips301Redirectnonwwwversions_14CC2/Workflow_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="362" alt="Workflow" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETMVCTips301Redirectnonwwwversions_14CC2/Workflow_thumb.png" width="607" border="0" /&gt;&lt;/a&gt;  &lt;br /&gt;    &lt;br /&gt;We can achieve the above by writing a custom handler that extends from the MvcHandler. We are basically interested in the ProcessRequest method where we can get hold of the non-www version of the URL to replace it to the www. version of URL.&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;    public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; The301GlobalHandler : MvcHandler
    {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; The301GlobalHandler(RequestContext requestContext) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(requestContext) { }

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ProcessRequest(HttpContextBase httpContext)
        {
            &lt;span class="rem"&gt;//We do not want this handler to process local requests&lt;/span&gt;
&lt;strong&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (httpContext.Request.IsLocal)&lt;/strong&gt;
                &lt;span class="kwrd"&gt;base&lt;/span&gt;.ProcessRequest(httpContext);
            &lt;span class="kwrd"&gt;else&lt;/span&gt;
                ProcessExternalRequest(httpContext);
        }

        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ProcessExternalRequest(HttpContextBase httpContext)
        {
            &lt;span class="kwrd"&gt;bool&lt;/span&gt; urlChanged = &lt;span class="kwrd"&gt;false&lt;/span&gt;;
            &lt;span class="kwrd"&gt;string&lt;/span&gt; url = RequestContext.HttpContext.Request.Url.AbsoluteUri;
            &lt;span class="rem"&gt;//Check for non-www version URL&lt;/span&gt;
&lt;strong&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (!RequestContext.HttpContext.Request.Url.AbsoluteUri.Contains(&lt;span class="str"&gt;"www"&lt;/span&gt;))&lt;/strong&gt;
            {
                urlChanged = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
                &lt;span class="rem"&gt;//change to www. version URL&lt;/span&gt;
&lt;strong&gt;                url = url.Replace(&lt;span class="str"&gt;"http://"&lt;/span&gt;, &lt;span class="str"&gt;"http://www."&lt;/span&gt;);  &lt;/strong&gt;                  
            }
            ProcessExternalRequest(url, urlChanged, httpContext);
        }

        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ProcessExternalRequest(&lt;span class="kwrd"&gt;string&lt;/span&gt; url, &lt;span class="kwrd"&gt;bool&lt;/span&gt; urlChanged, HttpContextBase httpContext)
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (urlChanged)
            {
                &lt;span class="rem"&gt;//mark as 301&lt;/span&gt;
                &lt;strong&gt;httpContext.Response.Status = &lt;span class="str"&gt;"301 Moved Permanently"&lt;/span&gt;;
                httpContext.Response.StatusCode = 301;
                httpContext.Response.AppendHeader(&lt;span class="str"&gt;"Location"&lt;/span&gt;, url);
&lt;/strong&gt;            }
            &lt;span class="kwrd"&gt;else&lt;/span&gt;
                &lt;span class="kwrd"&gt;base&lt;/span&gt;.ProcessRequest(httpContext);
        }
    }&lt;/pre&gt;

&lt;p&gt;The above code is self explanatory, we are checking if the Request is local or external, we are only interested on external URLs. Next we check, whether the RequestContext.HttpCOntext.Request.Url.AbsoluteUri contains a "www" or not. If it do not contain a "www" we go and inject a "www" to the URL to convert the non-www version of URL to a www. version URL. Then we mark the previous response to a 301, which indicates that the previous URL has been permanently removed and all future requests should be directed to the given new URL.  &lt;br /&gt;

  &lt;br /&gt;We have developed our core handler above, however we need a wrapper IRouteHandler class to get the above The301GlobalHandler to work within the MVC Framework. This wrapper class is responsible to return an instance of the The301GlobalHandler.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;    public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Route301GlobalHandler : IRouteHandler
    {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; The301GlobalHandler(requestContext);
        }
    }&lt;/pre&gt;

&lt;p&gt;
  &lt;br /&gt;We have got our Route301GlobalHandler ready, now we need all of our requests to go via this handler instead of the default. One easy way to do this, is to iterate through the route collection and  assign them to this Route301GlobalHandler during the Application_Start().&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;    protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
&lt;strong&gt;        Route301Global.ReAssignHandler(RouteTable.Routes);&lt;/strong&gt;
    }&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;    public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Route301Global
    {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ReAssignHandler(RouteCollection routes)
        {
            &lt;span class="kwrd"&gt;using&lt;/span&gt; (routes.GetReadLock())
            {
                AssignRoute301GlobalHandler(routes);
            }
        }

        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AssignRoute301GlobalHandler(IEnumerable&amp;lt;RouteBase&amp;gt; routes)
        {
            &lt;strong&gt;&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var routeBase &lt;span class="kwrd"&gt;in&lt;/span&gt; routes)&lt;/strong&gt;
            {
                &lt;strong&gt;AssignRoute301GlobalHandler(routeBase);&lt;/strong&gt;
            }
        }

        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AssignRoute301GlobalHandler(RouteBase routeBase)
        {
            var route = routeBase &lt;span class="kwrd"&gt;as&lt;/span&gt; Route;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (route == &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="kwrd"&gt;return&lt;/span&gt;;
            &lt;strong&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (route.RouteHandler.GetType() != &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Route301Handler))
                route.RouteHandler = &lt;span class="kwrd"&gt;new&lt;/span&gt; Route301GlobalHandler();&lt;/strong&gt;
        }
    }&lt;/pre&gt;

&lt;p&gt;Note, if you have other custom RouteHandler in your application, you will need to tweak this code, I am assuming all routes that will be mapped in the RegisterRoutes(RouteCollection routes) method uses the MvcHandler. 
  &lt;br /&gt;

  &lt;br /&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; 

  &lt;br /&gt;

  &lt;br /&gt;We have identified keeping one set of URL is important for SEO. Then we have looked at how we can 301 redirect non-www version of URL to www. version URL in ASP.NET MVC. Hope this saves you some time and thank you for being with me so far.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=132204"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=132204" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/132204.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2009/05/19/132204.aspx</guid>
            <pubDate>Mon, 18 May 2009 15:02:21 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/132204.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2009/05/19/132204.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/132204.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/132204.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET MVC Tips: Form POST, TryUpdateModel, unit test, Moq</title>
            <link>http://geekswithblogs.net/shahed/archive/2009/05/16/132131.aspx</link>
            <description>&lt;p&gt;Recently I have started playing with the &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; (pronounced "Mock-you" or just "Mock") a Mocking Library for .NET Developers, that takes full advantage of .NET 3.5 (i.e. Linq expression trees) and C# 3.0 features. Here in this post I will discuss how I have used the TryUpdateModel method in the Form POST scenario and also share how I have written a test case using Moq mocking library to deal with the TryUpdateModel&amp;lt;TModel&amp;gt;(TModel model) method of the ASP.NET MVC controller.     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;I have a very simple user interface, that allows user to  enter data and submit, to add a new "User".    &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/shahed/CreateUser_5F00_WebForm_5F00_thumb.png" /&gt;  &lt;/p&gt;  &lt;p&gt;My View Page is:    &lt;br /&gt;    &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/shahed/CreateUser_5F00_View_5F00_thumb.png" /&gt; &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;and my "Action" is     &lt;br /&gt;    &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/shahed/CreateUser_5F00_Action_5F00_thumb_5F00_2.png" /&gt;    &lt;br /&gt;You may have already noticed that I have decorated my action without any parameters, and my POST related codes are executed by checking the request type.     &lt;br /&gt;    &lt;br /&gt;if (Request.RequestType == POST)     &lt;br /&gt;{     &lt;br /&gt;  //code     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;As I have no parameters in my CreateUser(), I did not need to create separate action  with AcceptVerbs("GET") and AcceptVert("POST") attribute. I have created an empty instance of MembershipCreateViewModel object and passed it to the TryUpdateModel() method to get the values of the posted form into my viewmodel object. MVC framework takes care of the rest and populates the viewModel with the desired values. Also note the RedirectToAction() method has been on success, it is  very important to perform this client - redirect. It will ensure that  the form does not resubmit, and the user will never see a prompt like this, if he hits the browser refresh button.     &lt;br /&gt;    &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/shahed/IE_5F00_resubmit_5F00_thumb.png" /&gt;   &lt;br /&gt;    &lt;br /&gt;My MembershipCreateViewModel class is as follows,     &lt;br /&gt;    &lt;br /&gt;    &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/shahed/CreateUser_5F00_ViewModel_5F00_thumb.png" /&gt;   &lt;br /&gt;    &lt;br /&gt;I did not prefer passing four parameters to my CreateUser() Action such as,     &lt;br /&gt;    &lt;br /&gt;[AcceptVerbs["POST"])     &lt;br /&gt;CreateUser(string username, string email, string password, string confirmPassword)     &lt;br /&gt;{     &lt;br /&gt;//code     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;instead I kept the method simple by using the MembershipCreateUserViewModel to communicated between the Controller and the View.     &lt;br /&gt;    &lt;br /&gt;This is all good, but by now you may be wondering how would I test this Controller&amp;gt;Action. Lets explore that part. Here I will demonstrate my TestCase using the Moq framework, but you can do the same using other popular mocking frameworks like Rhino Mock, NMock, NMock2, NUnit.Mocks, EasyMock, TypeMock etc, or even with plain vanilla C# code. First of all I will need a FakeHttpContext to interact with my Controller from the test environment.     &lt;br /&gt;    &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/shahed/fakeContext_5F00_thumb.png" /&gt; &lt;/p&gt;  &lt;p&gt;It is very straight forward to create a mock of a class or interface in Moq, I have created mocks of HttpContextBase, HttpRequestBase, HttpResponseBase, HttpSessionStateBase, HttpServerUtility above and decorated methods and properties that I use in my test cases using the Setup and SetupGet method that is available in the Moq library.  &lt;br /&gt;    &lt;br /&gt;Note how easily with couple of lines of code I have a mockup of the HttpContextBase. The Mock&amp;lt;T&amp;gt; allows creation of custom value matchers that can be used on setups and verification, completely replacing the built-in It class with our own argument matching rules.  &lt;/p&gt;  &lt;p&gt;var httpContext = new Mock&amp;lt;HttpContextBase&amp;gt;();    &lt;br /&gt;var request = new Mock&amp;lt;HttpRequestBase&amp;gt;     &lt;br /&gt;httpContext.Setup(ctx =&amp;gt; ctx.Request(request.Object);     &lt;br /&gt;    &lt;br /&gt;This above code snippet is creating mock instances of HttpContexBase and HttpRequestBase classes, and then also setting up that httpContext.Request will return the mock instance "request".     &lt;br /&gt;    &lt;br /&gt;Now lets look at how I have used this mock httpContext in a test case.     &lt;br /&gt;    &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/shahed/EhrAdmin.MvcApplication-_2D00_-Microsoft-Visual-Studio-_2800_10_29005F00_thumb.png" /&gt;   &lt;br /&gt;I have created a test case here that tests our CreateUser() action. Note I have created an instance of the ControllerContext and passed the httpContext mock object as a parameter of its constructor. After that I have assigned this instance ( context ) to the controller.ControllerContext property.     &lt;br /&gt;    &lt;br /&gt;The Mock.Get() method is interesting it can retrieve a mock object for the given object instance. Here I have retrieved the request object and have modified it further. I have setup so that the request.Form returns NameValueCollection. You may be wondering why did I pass NamValueCollection. You may recall that I have used the TryUpdateModel() method in my action to get the form values into our viewmodel, and if you look what is happening under the hood of TryUpdateModel() method you will find that, "Form" is of type System.Collections.Specialized.NameValueCollection and a member of System.Web.HttpRequestBase, and     &lt;br /&gt;&lt;img src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/shahed/valuedictionary_5F00_thumb.png" /&gt; &lt;/p&gt;  &lt;p&gt;the internals of TryUpdateModel() iterates through each item of the ContollerContext.HttpContext.Request.Form object and populates the matching properties of the viewmodel using reflection. The above code snippet from the ValueProviderDictionary class where we see that the keys are being read from the Form object to populate a dictionary.    &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;     &lt;br /&gt;    &lt;br /&gt;Here in this post I have discussed a Form POST scenario and I have decorated my action method without any parameters as by default TryUpdateModel method looks at the submitted Form object and attempt to assign it to the viewmodel object that we pass. I have also demonstrated how I have used the Moq library to create fake/mock objects for my testcases, how I have setup some fake methods and properties. I have also demonstrated the handy Mock.Get method that can retrieve a mock object from a given object instance and allows us to modify the mock object further. Thank you for being with me so far and I hope this helps.     &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=132131"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=132131" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/132131.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2009/05/16/132131.aspx</guid>
            <pubDate>Fri, 15 May 2009 14:10:58 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/132131.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2009/05/16/132131.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/132131.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/132131.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking</title>
            <link>http://geekswithblogs.net/shahed/archive/2009/02/14/129386.aspx</link>
            <description>&lt;p&gt;Routing Library resides in the System.Web.Routing Namespace of the .NET Framework 3.5, which provides us the flexibility to use URLs that has no mapping to a physical file. This means ASP.NET MVC framework provides flexible URL mapping engine and enables us to write SEO (&lt;a href="http://en.wikipedia.org/wiki/Search_engine_optimization"&gt;Search Engine Optimization&lt;/a&gt;) friendly URLs with very little effort. No one can deny the importance of SEO, to be successful in search based marketing. What better way to analyze a business than from what customers are looking for on the Internet through keyword research. SEO is the way to go. &lt;br /&gt;&lt;br /&gt;SEO friendly URL Format for an e-commerce application may be&lt;br /&gt;/Products/List/ProductCategory    &lt;br /&gt;/Products/Detail/ProductName&lt;/p&gt; &lt;p&gt;URL Example&lt;br /&gt;/Products/List/CareCare&lt;br /&gt;/Products/Detail/MiracleCarDuster&lt;br /&gt;&lt;br /&gt;In ASP.NET MVC world the URL Routing System maps the incoming URLs to the relevant Controller and Action, in the above example our Contoller is Products and Action is List or Detail. We normally go and define a Route object and add it to the RouteCollection and register the RouteCollection during Application_Start(). Out of the box System.Web.Mvc library ships with the RouteCollectionExtensions which allows us to define routes easily using the the different overloads of MapRoute method.&lt;br /&gt;&lt;br /&gt;Example:&lt;/p&gt; &lt;p&gt;public static void RegisterRoutes(RouteCollection routes)&lt;br /&gt;{&lt;br /&gt;            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");              &lt;/p&gt;&lt;p&gt;           &lt;strong&gt;routes.MapRoute&lt;/strong&gt;(&lt;br /&gt;                "Default",                                              // Route name&lt;br /&gt;                "{controller}/{action}/{id}",                           // URL with parameters&lt;br /&gt;                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults&lt;br /&gt;            );  &lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;br /&gt;If you look under the hood you will find, a Route object is created and added to the RouteCollection.&lt;br /&gt;&lt;br /&gt;Route route = new Route(url, new MvcRouteHandler()){};&lt;br /&gt;.....&lt;br /&gt;.....&lt;br /&gt;routes.Add(name, route);&lt;br /&gt;&lt;br /&gt;note that "MvcRouteHandler" have been passed as a parameter by default, when we use the routes.MapRoute() method. The overloads of MapRoute() extension methods are just helper methods to make things easy for us, it is not mandatory that we will have to always use this. We can define a Route object in plain .NET code and assign necessary properties to it, we can also pass our preferred IRouteHandler. This gives superb flexibility with handling URLs. For instance In a practical world of web marketing / search marketing we always need to support Legacy URLs. In the web marketing world ad hoc campaigns are launched, and what may have worked last week may not work this week any more, as a result the URL structure changes frequently and we face the need to start redirecting to the new URLs. &lt;br /&gt;&lt;br /&gt;On Top of that during this transformation of URLs we need to implement "301 Moved Permanently" redirections, which means the previous URL has been permanently removed and all future requests should be directed to the given new URI. Handling this scenario has become very easy with the Routing Engine. All we need to do is add a new Route or modify the existing Route to fit our need. Matt Hawley has an excellent post on &lt;a href="http://blog.eworldui.net/post/2008/04/ASPNET-MVC---Legacy-Url-Routing.aspx"&gt;Legacy Url Routing&lt;/a&gt; which describes how to route existing aspx file based URLs to the appropriate MVC Controller and Action. This article also gives directions on how to implement custom Route and custom RouteHandler.&lt;br /&gt;&lt;/p&gt; &lt;p&gt;public static void RegisterRoutes(RouteCollection routes)&lt;br /&gt;{&lt;br /&gt;            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");              &lt;/p&gt;&lt;p&gt;           &lt;strong&gt;&lt;/strong&gt;routes.MapRoute(&lt;br /&gt;                "Default",                                              // Route name&lt;br /&gt;                "{controller}/{action}/{id}",                           // URL with parameters&lt;br /&gt;                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults&lt;br /&gt;            );&lt;br /&gt; &lt;/p&gt;&lt;blockquote&gt; &lt;p&gt;  routes.Add("", new LegacyRoute(&lt;br /&gt;    "Users/Login.aspx",&lt;br /&gt;    "Login",&lt;br /&gt;    new &lt;strong&gt;LegacyRouteHandler&lt;/strong&gt;())); // Defined a Custom Route class and have passed a custom IRouteHandler. &lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;}&lt;br /&gt;&lt;/p&gt;&lt;pre&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;As you can see in the above code, how easily we can add a custom route and pass a custom IRouteHandler (in this case LegacyHandler). &lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Tracking - is another common task performed, we want to track everything, every single clicks a consumer performs on the site. This has also becomes easy in the world of ASP.NET MVC. By design we define separate actions for each functionality and we Route to the Controller - Action to get anything done. So tracking would be a viable option to do centrally just before creating the Controller object or just before delegating to the Action. &lt;br /&gt;&lt;br /&gt;You will notice the implementation of IRouteHandler requires to implement only one method "GetHttpHandler" which returns a IHttpHandler&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;public interface IRouteHandler&lt;br /&gt;{&lt;br /&gt;        IHttpHandler GetHttpHandler(RequestContext requestContext);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;You will find the MvcRouteHandler implements IRouteHandler like this&lt;br /&gt;&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;public class MvcRouteHandler : IRouteHandler {&lt;br /&gt;        protected virtual IHttpHandler GetHttpHandler(RequestContext requestContext) {&lt;br /&gt;            return new MvcHandler(requestContext);&lt;br /&gt;        } 
&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;br /&gt;The real delegation of a Route to a Controller and Action happens in the ProcessRequest(HttpContext context); method of the MVCHandler class which is the implementation of IHttpHandler. The ProcessRequest(HttpContext contxt) method may be a be a good place to centrally control tracking in one of our custom Handlers. The implementaiton of MVCHandler is as follows, where you can see how a controller is created by the IControllerFactory factory, and then Execute() method is called. We can do our tracking somewhere before calling the Execute() method.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;protected internal virtual void ProcessRequest(HttpContextBase httpContext) {&lt;br /&gt;            AddVersionHeader(httpContext); 
&lt;/p&gt;&lt;p&gt;            // Get the controller type&lt;br /&gt;            string controllerName = RequestContext.RouteData.GetRequiredString("controller"); 
&lt;/p&gt;&lt;p&gt;            // Instantiate the controller and call Execute&lt;br /&gt;            IControllerFactory factory = ControllerBuilder.GetControllerFactory();&lt;br /&gt;            IController controller = factory.CreateController(RequestContext, controllerName);&lt;br /&gt;            if (controller == null) {&lt;br /&gt;                throw new InvalidOperationException(&lt;br /&gt;                    String.Format(&lt;br /&gt;                        CultureInfo.CurrentUICulture,&lt;br /&gt;                        MvcResources.ControllerBuilder_FactoryReturnedNull,&lt;br /&gt;                        factory.GetType(),&lt;br /&gt;                        controllerName));&lt;br /&gt;            }&lt;br /&gt;            try {&lt;br /&gt;                controller.Execute(RequestContext);&lt;br /&gt;            }&lt;br /&gt;            finally {&lt;br /&gt;                factory.ReleaseController(controller);&lt;br /&gt;            }&lt;br /&gt;        } 
&lt;/p&gt;&lt;p&gt;&lt;br /&gt;Did you know, Routes in the ASP.NET Mvc are matched and executed on a first match bases! It may be important to order the routes so that the correct pattern is matched first before a more general pattern matches and executes it. It is sometimes hard to figure out which particular pattern will be caught first when we have a lot of routes, ASP.NET Routing Debugger comes in to rescue, Phil Hack has put together this nice little &lt;a href="http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx"&gt;route tester utility&lt;/a&gt; which can save a lot of time. This utility quickly displays in Red and Green color what Route patterns have matched for a particular URL. So we can type in various URLs in the addressbar to see which routes matches.&lt;br /&gt;&lt;br /&gt;Lets now looks at a different problem, we normally define all the routes in the global.aspx.cs file, this causes a problem when Routes changes frequently, every single time a new route is added or an existing one is modified we need to recompile web application and upload the new dll to the server, again it is not mandatory to write Routing rules in the global.aspx.cs file, we can easily store the routing rules to a Xml file and use a combination XML related .NET libraries and .NET Reflection APIs to read from the Xml file and create/deserialize Route Objects to add them to the RouteCollection during the Application_Start(). But still we haven't overcome the limitations of restarting the application as the RouteCollection gettting registered during Application_Start. I think we have to live with that, unless we go and implement some kind of  FileSystemWatcher to monitor the Xml file and force to refresh the RouteTable.Routes object when the xml file changes. I haven't tried implementing this yet but this would work I think.&lt;br /&gt;&lt;br /&gt;We have discussed here, how ASP.NET Routing engine eases writing SEO friendly Url, maitaining Url redirections and tracking centrally. Hope this helps.&lt;br /&gt;&lt;br /&gt;Thank you for being with me so far. &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129386"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129386" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/129386.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2009/02/14/129386.aspx</guid>
            <pubDate>Fri, 13 Feb 2009 19:06:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/129386.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2009/02/14/129386.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/129386.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/129386.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET tips: Display resultset from Multiple DataTable</title>
            <link>http://geekswithblogs.net/shahed/archive/2009/02/11/129310.aspx</link>
            <description>&lt;p&gt;I normally do not use DataSet and prefer Objects instead generated by the ORM frameworks, but recently I had to produce a ASP.NET page that displayed a list of records from multiple DataTables. Lets look at a similar example.    &lt;br /&gt;    &lt;br /&gt;Lets assume we consume this DataSet which has two DataTables "Agent" and "RealEstateProperty". The task is to display, what properties belongs to which agent, in a GridView.     &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/RealEstateDataset_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="291" alt="RealEstateDataset" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/RealEstateDataset_thumb.png" width="307" border="0" /&gt;&lt;/a&gt;  &lt;br /&gt;    &lt;br /&gt;There are different ways to accomplish this task, lets look at them one by one.     &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;Method 1: Bind DataTables to  a single GridView      &lt;br /&gt;&lt;/strong&gt;    &lt;br /&gt;To bind these multiple DataTables to a single GridView control, we can quickly create a new temporary DataTable, with the required fields and populate the Rows and then bind the new DataTable Rows to the GridView.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/BindSingleGrid_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="524" alt="BindSingleGrid" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/BindSingleGrid_thumb.png" width="651" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The above code is self explanatory, where I have created a temporary DataTable, populated its rows by iterating through the original DataTables and then binded the Grid to the new DataTable.Rows. The interesting piece of code to note here is the row.GetChildRows(), which respects the relationship and automatically returns the related child rows. The aspx part looks like the following, where we have a GridView with three columns.    &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/SingleGridView)_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="144" alt="SingleGridView)" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/SingleGridView)_thumb.png" width="584" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;and the RowDataBound code is    &lt;br /&gt;    &lt;br /&gt; &lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/SingleGridRowBound_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="242" alt="SingleGridRowBound" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/SingleGridRowBound_thumb_1.png" width="604" border="0" /&gt;&lt;/a&gt;     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Method 2: Bind DataTables to a nested GridView&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;If we want to avoid creating temporary dummy DataTable as described in method 1, we can use the technique of nested list control. Now we can do this by using    &lt;br /&gt;    &lt;br /&gt;two GridView, or     &lt;br /&gt;two DataList, or     &lt;br /&gt;two Repeaters or     &lt;br /&gt;a combination of Repeater and GridView, or     &lt;br /&gt;a combination of Repeater and DataList, or     &lt;br /&gt;a combination of GridView and DataList.     &lt;br /&gt;    &lt;br /&gt;We are going to look at a combination of 2 GridViews here, where one GridView is nested inside another, you can do any of the above combinations. For this case the the aspx code is,&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/nestedgridview_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="299" alt="nestedgridview" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/nestedgridview_thumb.png" width="708" border="0" /&gt;&lt;/a&gt;     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;Notice the parent and nested GridViews has different onrowdatabound methods. In this technique the parent grid is binded to the the Agent DataTable Rows and the nested GridView is binded to the child RealEstateProperty DataTable Rows. Here is the code.    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/nesteddatagridbind_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="580" alt="nesteddatagridbind" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/nesteddatagridbind_thumb.png" width="707" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Method 3: Convert the DataSet to Objects and then bind to GridView      &lt;br /&gt;&lt;/strong&gt;    &lt;br /&gt;We can generate csharp class from the dataset schema using Xsd.exe and then bind the GridViews to the objects. I have discussed similar technique in one of my previous &lt;a href="http://msmvps.com/blogs/shahed/archive/2008/03/23/datatable-to-json-and-tojson-extension.aspx"&gt;blog post&lt;/a&gt;, where you will find how we can use the Xsd.exe that ships with the .NET Framework.     &lt;br /&gt;    &lt;br /&gt;Example     &lt;br /&gt;    C:\temp&amp;gt;xsd propertyDataSet.xsd /l:cs /c     &lt;br /&gt;    Microsoft (R) Xml Schemas/DataTypes support utility     &lt;br /&gt;    [Microsoft (R) .NET Framework, Version 2.0.50727.42]     &lt;br /&gt;    Copyright (C) Microsoft Corporation. All rights reserved.     &lt;br /&gt;    Writing file 'C:\temp\propertydatasetclass.cs'.&lt;/p&gt;  &lt;p&gt;I also demonstrated a handy DataTableToT() method to assist in converting DataTable to strongly typed object. And when we have the DataTable converted to a stronglytyped object/list/collection it is very easy to bind to the bind to the GridView.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Other Tips&lt;/strong&gt;     &lt;br /&gt;1. DataSet also comes with handy Merge Method, &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.dataset.merge.aspx"&gt;check here&lt;/a&gt;, we could have also used this method.     &lt;br /&gt;2. When we load a DataSet from xml schema if we do not provide the strongly typed DataSet it will not preserve the Relationship automatically.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/QuickWatch_2.png"&gt; &lt;/a&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/QuickWatch_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="97" alt="onlydataset" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/onlydataset_thumb.png" width="435" border="0" /&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="394" alt="QuickWatch" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/QuickWatch_thumb.png" width="640" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Note: The Relations Count = 0;    &lt;br /&gt;    &lt;br /&gt;3. To preserve Relations automatically during a DataSet load from xml, provide strongly typed DataSet. Both of the following methods will automatically preserve the parent child relationship. Also even if you consume them from a webservice client application they will preserve the Relations.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/stronglytypeddataset_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="249" alt="stronglytypeddataset" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsDisplayresultsetfromMultipleD_31DD/stronglytypeddataset_thumb.png" width="439" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;4. DataSet.WriteXml() method serializes the DataSet content and writes XML data.    &lt;br /&gt;5. DataSet.WriteXmlSchema() method writes the DataSet structure as XML Schema (xsd file).&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;     &lt;br /&gt;We have discussed different techniques to bind multiple DataTables to GridView, we have also discussed some handy tips about the DataSet. Hope this helps.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129310"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129310" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/129310.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2009/02/11/129310.aspx</guid>
            <pubDate>Tue, 10 Feb 2009 18:06:19 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/129310.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2009/02/11/129310.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/129310.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/129310.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET tips: CustomBase class with Generic Class</title>
            <link>http://geekswithblogs.net/shahed/archive/2009/01/11/128564.aspx</link>
            <description>&lt;p&gt;All our ASP.NET pages must derive from System.Web.UI.Page class, but we can take the advantage of inheritance and create a Custom Base Class to manage Security, Session, Error Handling and other repetitive custom codes elegantly. We normally go and create a custom base class that derives from System.Web.UI.Page class which looks something like this.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/basepage_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="371" alt="basepage" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/basepage_thumb.png" width="537" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;Then we go and inherit our ASP.NET pages from this class. Note: For ASP.NET 2.0 website projects this file needs to be in a separate class library because of its dynamic compilation mechanism. Our ASP.NET page may look like this. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/defaultpage_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="136" alt="defaultpage" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/defaultpage_thumb.png" width="515" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;This is good but, we also have the flexibility to use generic classes and enjoy the benefits of generic classes in our ASP.NET pages and do not need to worry about type casting to and from the universal base type Object, moreover the lack of compile time checking in our pages may sometimes lead to some grief, that we can avoid easily.    &lt;br /&gt;    &lt;br /&gt;Lets look at how we can extend our BasePage using Generic Class. Lets create a an ExtendedBasePage.     &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/ExtendedBasePage_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="446" alt="extendedbasepage" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/ExtendedBasePage_thumb_1.png" width="701" border="0" /&gt;&lt;/a&gt;  &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/basepagediagram_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="431" alt="basepagediagram" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/basepagediagram_thumb.png" width="373" border="0" /&gt;&lt;/a&gt;    &lt;br /&gt;In the above code we have declared a type parameter T in the angle brackets, which must derive from RealEstateProperty class, and our ExtendedBasePage derives from our previous Basepage. Then we have used T as return type of property "Property". You may be wondering by now where did RealEstateProperty class came from. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/RealEstate_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="444" alt="RealEstate" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/RealEstate_thumb.png" width="431" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here you can see Land and Apartment derives from RealEstateProperty class, these are all business objects of my fictitious realestate website and I want to access the "this.Property" object from my pages. Lets now create a ASP.NET page that derives from ExtendedBasePage&amp;lt;T&amp;gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/LandPage_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="326" alt="LandPage" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/LandPage_thumb.png" width="517" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here in this ASP.NET page that derives from ExtendedBasePage&amp;lt;Land&amp;gt; I have passed the "Land" as the type parameter, now this.Property is type safe, we cannot go and write this.Property = new Apartment(), the compiler will throw an exception straightway "Cannot implicitly convert type 'Website.Util.Apartment' to 'Website.Util.Land'". Also notice we do not need to type cast this.Property object to Land to be able to access its public properties. &lt;/p&gt;  &lt;p&gt;Here we discussed how we can take advantage of Generic Classes in our ASP.NET custom base pages, hope this helps thank you for being with me so far.    &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=128564"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=128564" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/128564.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2009/01/11/128564.aspx</guid>
            <pubDate>Sun, 11 Jan 2009 05:34:16 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/128564.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2009/01/11/128564.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/128564.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/128564.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET tips: CustomBase class with Generic Class</title>
            <link>http://geekswithblogs.net/shahed/archive/2009/01/11/128560.aspx</link>
            <description>&lt;p&gt;All our ASP.NET pages must derive from System.Web.UI.Page class, but we can take the advantage of inheritance and create a Custom Base Class to manage Security, Session, Error Handling and other repetitive custom codes elegantly. We normally go and create a custom base class that derives from System.Web.UI.Page class which looks something like this.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/basepage_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="371" alt="basepage" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/basepage_thumb.png" width="537" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;Then we go and inherit our ASP.NET pages from this class. Note: For ASP.NET 2.0 website projects this file needs to be in a separate class library because of its dynamic compilation mechanism. Our ASP.NET page may look like this. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/defaultpage_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="136" alt="defaultpage" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/defaultpage_thumb.png" width="515" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;This is good but, we also have the flexibility to use generic classes and enjoy the benefits of generic classes in our ASP.NET pages and do not need to worry about type casting to and from the universal base type Object, moreover the lack of compile time checking in our pages may sometimes lead to some grief.    &lt;br /&gt;    &lt;br /&gt;Lets look at how we can extend our BasePage using Generic Class. Lets create a an ExtendedBasePage.     &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/ExtendedBasePage_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="446" alt="extendedbasepage" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/ExtendedBasePage_thumb_1.png" width="701" border="0" /&gt;&lt;/a&gt;  &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/basepagediagram_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="431" alt="basepagediagram" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/basepagediagram_thumb.png" width="373" border="0" /&gt;&lt;/a&gt;    &lt;br /&gt;In the above code we have declared a type parameter T in the angle brackets, which must derive from RealEstateProperty class, and our ExtendedBasePage derives from our previous Basepage. Then we have used T as return type of property "Property". You may be wondering by now where did RealEstateProperty class came from. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/RealEstate_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="444" alt="RealEstate" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/RealEstate_thumb.png" width="431" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here you can see Land and Apartment derives from RealEstateProperty class, these are all business objects of my fictitious realestate website and I want to access the "this.Property" object from my pages. Lets now create a ASP.NET page that derives from ExtendedBasePage&amp;lt;T&amp;gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/LandPage_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="326" alt="LandPage" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsCustomBaseclasswithGenericCla_E4B3/LandPage_thumb.png" width="517" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here in this ASP.NET page that derives from ExtendedBasePage&amp;lt;Land&amp;gt; I have passed the "Land" as the type parameter, now this.Property is type safe, we cannot go and write this.Property = new Apartment(), the compiler will throw an exception straightway "Cannot implicitly convert type 'Website.Util.Apartment' to 'Website.Util.Land'". Also notice we do not need to type cast this.Property object to Land to be able to access its public properties. &lt;/p&gt;  &lt;p&gt;Here we discussed how we can take advantage of Generic Classes in our ASP.NET custom base pages, hope this helps thank you for being with me so far.    &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=128560"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=128560" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/128560.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2009/01/11/128560.aspx</guid>
            <pubDate>Sun, 11 Jan 2009 05:16:07 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/128560.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2009/01/11/128560.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/128560.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/128560.aspx</trackback:ping>
        </item>
        <item>
            <title>ADO.NET Data Services Framework (former - astoria)</title>
            <link>http://geekswithblogs.net/shahed/archive/2008/12/11/127802.aspx</link>
            <description>&lt;p&gt;ADO.NET Data Services enable applications to expose data as a data service to be consumable via web clients. The data service which can be easily accessible via regular HTTP requests. CRUD operations are performed using HTTP verbs - GET, POST, PUT and DELETE. The data service responses open formats such as JSON and Aop/APP which are idal back-ead for AJAX-style applications, Rick Interactive Applications and other web clients. ADO.NET client libraries  and developer tools works with both on premises services created using ADO.NET Data Services and also with hosted CLOUD services.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Some useful links&lt;/strong&gt;&lt;/p&gt;  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;Data Platform Developer Center - Data Services page&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/cc668792.aspx"&gt;Data services conceptual documentation on MSDN&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.services.aspx"&gt;Data services API reference documentation on MSDN&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/astoriateam"&gt;Data services team blog&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;a href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=1430&amp;amp;SiteID=1"&gt;Data services online forum (a place to ask us your data services related questions)&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/cc907912.aspx"&gt;Whitepaper: Using ADO.NET Data Services V1&lt;/a&gt;     &lt;br /&gt;&lt;strong&gt;Technical Articles&lt;/strong&gt;     &lt;p&gt;&lt;a href="http://msdn.microsoft.com/library/cc956153.aspx"&gt;&lt;strong&gt;Overview: ADO.NET Data Services&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;The ADO.NET Data Services framework consists of a combination of patterns and libraries that enable the creation and consumption of data services for the web. This article will discuss the key elements of the ADO.NET Data Services framework at a high level and the motivation behind its design.&lt;/p&gt;    &lt;p&gt;&lt;a href="http://msdn.microsoft.com/library/cc907912.aspx"&gt;&lt;strong&gt;Using ADO.NET Data Services&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;Learn how to create and use ADO.NET Data Services, and get documentation on the various details around the URI and payload formats.&lt;/p&gt;    &lt;p&gt;&lt;a href="http://msdn.microsoft.com/magazine/cc748663.aspx"&gt;&lt;strong&gt;Expose and Consume Data in a Web Services World&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;What is a data service? The ADO.NET Data Services framework consists of a combination of patterns and libraries that enable the creation and consumption of data services for the web. This article will discuss exposing and consuming data using Data Services, describing data with the Entity Data Model, and securing your service.&lt;/p&gt;    &lt;p&gt;&lt;a href="http://msdn.microsoft.com/library/cc668792.aspx"&gt;&lt;strong&gt;MSDN Reference Documentation&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&lt;a href="http://msdn.microsoft.com/library/system.data.services.aspx"&gt;&lt;strong&gt;MSDN API Reference Documentation – Server&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;&lt;a href="http://msdn.microsoft.com/library/system.data.services.client.aspx"&gt;&lt;strong&gt;MSDN API Reference Documentation – Client&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&lt;a href="http://msdn.microsoft.com/library/cc838234(VS.95).aspx"&gt;&lt;strong&gt;ADO.NET Data Services (Silverlight)&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;   &lt;strong&gt;Data Services Futures&lt;/strong&gt;     &lt;p&gt;&lt;a href="http://channel9.msdn.com/pdc2008/TL08/"&gt;&lt;strong&gt;Offline-Enabled Data Services and Desktop Applications&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;Learn how to create offline-capable applications that have a local replica of their data, how to synchronize that replica with an online data service when a network connection becomes available, and how replicas can be used with the ADO.NET Entity Framework.&lt;/p&gt;    &lt;strong&gt;Transparent Design Process&lt;/strong&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/astoriateam/archive/tags/Design%20Notes/default.aspx"&gt;&lt;strong&gt;Design Notes&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;See the teams feature design notes online and give your comments on the design.&lt;/p&gt;   &lt;strong&gt;How Do I …&lt;/strong&gt;     &lt;ul&gt;     &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/data/cc745957.aspx"&gt;How Do I: Getting Started with ADO.NET Data Services over a Relational Database&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/data/cc745968.aspx"&gt;How Do I: Getting Started with ADO.NET Data Services over a Non-Relational Data Source&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/data/cc974474.aspx"&gt;How Do I: Consuming an ADO.NET Data Service in a Silverlight Application&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/data/cc974504.aspx"&gt;How Do I: Consume an ADO.NET Data Service in a .NET Application&lt;/a&gt; &lt;/li&gt;   &lt;/ul&gt;   &lt;strong&gt;Deep Dive Videos&lt;/strong&gt;     &lt;p&gt;&lt;a href="http://channel9.msdn.com/pdc2008/TL07/"&gt;&lt;strong&gt;Developing Applications Using Data Services&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;See how to use common libraries and tools when building applications using on premises and/or cloud services (Windows Azure services, SQL Data Services, etc.).&lt;/p&gt;    &lt;p&gt;&lt;a href="http://channel9.msdn.com/pdc2008/ES07/"&gt;&lt;strong&gt;Programming Windows Azure Tables Using Data Services Client Libraries and Tools&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;Learn how to use the highly scalable, available and durable Windows Azure table storage service. This session presents a deep dive with demos (using ADO.NET Data Services clients &amp;amp; interaction conventions) showing in detail how to work with cloud storage.&lt;/p&gt;    &lt;p&gt;     &lt;br /&gt;&lt;/p&gt; &lt;/li&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=127802"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=127802" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/127802.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2008/12/11/127802.aspx</guid>
            <pubDate>Thu, 11 Dec 2008 03:40:33 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/127802.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2008/12/11/127802.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/127802.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/127802.aspx</trackback:ping>
        </item>
        <item>
            <title>Microsoft Chart Controls for ASP.NET and Windows Forms</title>
            <link>http://geekswithblogs.net/shahed/archive/2008/12/02/127509.aspx</link>
            <description>&lt;p&gt;Now &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=1D69CE13-E1E5-4315-825C-F14D33A303E9&amp;amp;displaylang=en"&gt;Microsoft Chart controls Add-on for Microsoft Visual Studio 2008&lt;/a&gt; can be used for charting needs.&lt;/p&gt;  &lt;p&gt;Prerequisite: Microsoft Visual Studio 2008 SP1 and &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=130f7986-bf49-4fe5-9ca8-910ae6ea442c&amp;amp;DisplayLang=en"&gt;Microsoft Chart Controls for Microsoft .NET Framework 3.5&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;img src="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=mschart&amp;amp;DownloadId=3633" /&gt; &lt;/p&gt;  &lt;p&gt;For sample Environment for Microsoft Chart Controls check &lt;a href="http://code.msdn.microsoft.com/mschart"&gt;here&lt;/a&gt;. The sample environment contains over 200 samples for both ASP.NET and Windows Forms.    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;See the following related links:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=128852"&gt;Chart Controls for .NET Framework&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=581FF4E3-749F-4454-A5E3-DE4C463143BD&amp;amp;displaylang=en"&gt;Chart Controls for .NET Framework Language Pack&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=1D69CE13-E1E5-4315-825C-F14D33A303E9&amp;amp;displaylang=en"&gt;Chart Controls Add-on for Visual Studio 2008&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=128301"&gt;Chart Controls for .NET Framework Documentation&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=128713"&gt;Chart Controls Forum&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/alexgor"&gt;Alex Gorev's Chart Blog&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;There are lots of third party Chart Controls available for ASP.NET as well, please check &lt;a href="http://www.asp.net/community/control-gallery/browse.aspx?category=7"&gt;here&lt;/a&gt;. &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=127509"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=127509" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/127509.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2008/12/02/127509.aspx</guid>
            <pubDate>Tue, 02 Dec 2008 01:35:52 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/127509.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2008/12/02/127509.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/127509.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/127509.aspx</trackback:ping>
        </item>
        <item>
            <title>Reflection Tips on Nested Classes: Use (+) plus instead of (.) dot with Assembly.GetType</title>
            <link>http://geekswithblogs.net/shahed/archive/2008/11/07/126838.aspx</link>
            <description>&lt;p&gt;Lets say we want to use reflection to generate a mock test case for this following Nested Class &lt;strong&gt;ClientAddress&lt;/strong&gt;.&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ReflectionTipsonNestedClassesUse.GetType_10F28/class_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="621" alt="class" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ReflectionTipsonNestedClassesUse.GetType_10F28/class_thumb.png" width="372" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;When we load an assembly from a disk dynamically, we can use &lt;strong&gt;GetType&lt;/strong&gt; to look up a type defined in the assembly.     &lt;br /&gt;    &lt;br /&gt;Example:     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;Assembly assembly = Assembly.GetAssembly(typeof(TheClient));    &lt;br /&gt;Type type = assembly.GetType("ConsoleApplicationUsePlus.TheClient");     &lt;br /&gt;object obj = Activator.CreateInstance(type); &lt;/p&gt;  &lt;p&gt;But one point to remember here, to get Assembly.GeType working with nested classes a &lt;strong&gt;Plus(+)&lt;/strong&gt; sign should precede the nested class. To get a parent class and a nested class we have to use     &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;Type.GetType("ParentClass+NestedClass") //Notice the Plus(+) sign.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;So in the above example to get &lt;strong&gt;ClientAddress&lt;/strong&gt; we would have to write something like this.&lt;/p&gt;  &lt;p&gt;Assembly assembly = Assembly.GetAssembly(typeof(TheClient));    &lt;br /&gt;&lt;strong&gt;Type type = assembly.GetType("ConsoleApplicationUsePlus.TheClient+ClientAddress"); //Notice the Plus(+) sign.      &lt;br /&gt;&lt;/strong&gt;object obj = Activator.CreateInstance(type); &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;This is well documented in MSDN and here is the full chart     &lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa332510.aspx"&gt;http://msdn.microsoft.com/en-us/library/aa332510.aspx&lt;/a&gt;     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;To Get An unmanaged pointer to &lt;b&gt;MyType&lt;/b&gt;     &lt;br /&gt;&lt;tt&gt;Use Type.GetType("MyType*")&lt;/tt&gt; &lt;/p&gt;  &lt;p&gt;To Get An unmanaged pointer to a pointer to &lt;b&gt;MyType&lt;/b&gt;     &lt;br /&gt;&lt;tt&gt;Use Type.GetType("MyType**")&lt;/tt&gt; &lt;/p&gt;  &lt;p&gt;To Get A managed pointer or reference to &lt;b&gt;MyType&lt;/b&gt;     &lt;br /&gt;&lt;tt&gt;Use Type.GetType("MyType&amp;amp;")&lt;/tt&gt;. Note that unlike pointers, references are limited to one level. &lt;/p&gt;  &lt;p&gt;To Get A parent class and a nested class    &lt;br /&gt;&lt;tt&gt;Use Type.GetType("MyParentClass+MyNestedClass")&lt;/tt&gt; &lt;/p&gt;  &lt;p&gt;To Get A one-dimensional array with a lower bound of 0    &lt;br /&gt;&lt;tt&gt;Use Type.GetType("MyArray[]")&lt;/tt&gt; &lt;/p&gt;  &lt;p&gt;To Get A one-dimensional array with an unknown lower bound    &lt;br /&gt;&lt;tt&gt;Use Type.GetType("MyArray[*]")&lt;/tt&gt; &lt;/p&gt;  &lt;p&gt;To Get An n-dimensional array    &lt;br /&gt;Use A comma (,) inside the brackets a total of n-1 times. For example,&lt;tt&gt;System.Object[,,]&lt;/tt&gt; represents a three-dimensional &lt;b&gt;Object&lt;/b&gt; array. &lt;/p&gt;  &lt;p&gt;To Get A two-dimensional array's array    &lt;br /&gt;&lt;tt&gt;Use Type.GetType("MyArray[][]")&lt;/tt&gt; &lt;/p&gt;  &lt;p&gt;To Get A rectangular two-dimensional array with unknown lower bounds    &lt;br /&gt;&lt;tt&gt;Use Type.GetType("MyArray[*,*]")&lt;/tt&gt; or &lt;tt&gt;Type.GetType("MyArray[,]")&lt;/tt&gt; &lt;/p&gt;  &lt;p&gt;  &lt;/p&gt;  &lt;p&gt;Lets go back to generate our mock test case for the Nested class &lt;strong&gt;ClientAddress&lt;/strong&gt;.     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;namespace ConsoleApplicationUsePlus    &lt;br /&gt;{     &lt;br /&gt;    class Program     &lt;br /&gt;    {     &lt;br /&gt;        static void Main(string[] args)     &lt;br /&gt;        {     &lt;br /&gt;            MockHelper generator = new MockHelper();     &lt;br /&gt;            //Generate mock testcase     &lt;br /&gt;          &lt;strong&gt;  generator.Generate();      &lt;br /&gt;&lt;/strong&gt;            Console.ReadLine();     &lt;br /&gt;        }     &lt;br /&gt;    } &lt;/p&gt;  &lt;p&gt;    class MockHelper    &lt;br /&gt;    {     &lt;br /&gt;        int index = 0; &lt;/p&gt;  &lt;p&gt;        private void GenerateMock(object obj)    &lt;br /&gt;        {     &lt;br /&gt;            DoWork(obj, string.Empty);     &lt;br /&gt;        } &lt;/p&gt;  &lt;p&gt;        Dictionary&amp;lt;string, string&amp;gt; keys = new Dictionary&amp;lt;string, string&amp;gt;(); &lt;/p&gt;  &lt;p&gt;        private void DoWork(object obj, string baseInstanceName)    &lt;br /&gt;        {     &lt;br /&gt;            Console.WriteLine(obj.GetType().ToString());     &lt;br /&gt;            index = index + 1; &lt;/p&gt;  &lt;p&gt;            string instanceName = string.Format("{0}{1}", obj.GetType().Name, index);    &lt;br /&gt;            Console.WriteLine(String.Format("{0} {1} = new {0}(); ", obj.GetType().Name, instanceName)); &lt;/p&gt;  &lt;p&gt;            keys.Add(baseInstanceName, instanceName); &lt;/p&gt;  &lt;p&gt;            foreach (System.Reflection.PropertyInfo property in obj.GetType().GetProperties())    &lt;br /&gt;            {     &lt;br /&gt;                if (property.PropertyType.Assembly.FullName.Contains("ConsoleApplicationUsePlus"))     &lt;br /&gt;                { &lt;/p&gt;  &lt;p&gt;                    object propertyInstance = property.GetValue(obj, null);    &lt;br /&gt;                    if (propertyInstance == null)     &lt;br /&gt;                    {     &lt;br /&gt;                        propertyInstance = Activator.CreateInstance(property.PropertyType);     &lt;br /&gt;                        if (property.CanWrite)     &lt;br /&gt;                            property.SetValue(obj, propertyInstance, null); &lt;/p&gt;  &lt;p&gt;                        string baseI = String.Format(@"{0}.{1}", instanceName, property.Name);    &lt;br /&gt;                        DoWork(propertyInstance, baseI); &lt;/p&gt;  &lt;p&gt;                        Console.WriteLine(String.Format(@"{0}.{1} = {2} ;", instanceName, property.Name, keys[baseI]));    &lt;br /&gt;                    }     &lt;br /&gt;                }     &lt;br /&gt;                else if (property.PropertyType == typeof(string))     &lt;br /&gt;                {     &lt;br /&gt;                    Console.WriteLine(String.Format(@"{0}.{1} = {2} ;", instanceName, property.Name, GetRandomString()));     &lt;br /&gt;                }     &lt;br /&gt;            }     &lt;br /&gt;        } &lt;/p&gt;  &lt;p&gt;        //Entry point    &lt;br /&gt;        public void Generate()     &lt;br /&gt;        { &lt;/p&gt;  &lt;p&gt;            Assembly assembly = Assembly.GetAssembly(typeof(TheClient));    &lt;br /&gt;            &lt;strong&gt;Type type = assembly.GetType("ConsoleApplicationUsePlus.TheClient+ClientAddress"); //Notice the plus sign(+)      &lt;br /&gt;&lt;/strong&gt;            object obj = Activator.CreateInstance(type); &lt;/p&gt;  &lt;p&gt;            GenerateMock(obj); &lt;/p&gt;  &lt;p&gt;        } &lt;/p&gt;  &lt;p&gt;        private string GetRandomString()    &lt;br /&gt;        {     &lt;br /&gt;            long i = 1;     &lt;br /&gt;            foreach (byte b in Guid.NewGuid().ToByteArray())     &lt;br /&gt;            {     &lt;br /&gt;                i *= ((int)b + 1);     &lt;br /&gt;            }     &lt;br /&gt;            return string.Format("{0:x}", i - DateTime.Now.Ticks);     &lt;br /&gt;        } &lt;/p&gt;  &lt;p&gt;    }    &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;  &lt;/p&gt;  &lt;p&gt;and the output looks like the following. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ReflectionTipsonNestedClassesUse.GetType_10F28/output_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="316" alt="output" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ReflectionTipsonNestedClassesUse.GetType_10F28/output_thumb_1.png" width="673" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;  &lt;/p&gt;  &lt;p&gt;I have discussed about the &lt;strong&gt;MockHelper&lt;/strong&gt; in one of my &lt;a href="http://geekswithblogs.net/shahed/archive/2008/08/06/124263.aspx"&gt;earlier post&lt;/a&gt;, so not discussing this code again here.     &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We have looked at the qualified type name of nested classes, also looked at how to use Type.GetType() for various types. The best way to avoid any mistakes on the  type name possibly, is to create and instance of the class and to invoke the GetType().ToString() on the instantiated object;&lt;/p&gt;  &lt;p&gt;Example : &lt;/p&gt;  &lt;p&gt;TheClient.ClientAddress obj = new TheClient.ClientAddress(); &lt;/p&gt;  &lt;p&gt;Console.WriteLine(obj.GetType().ToString()); &lt;/p&gt;  &lt;p&gt;  &lt;/p&gt;  &lt;p&gt;Enjoy Coding!!&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=126838"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=126838" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/126838.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2008/11/07/126838.aspx</guid>
            <pubDate>Fri, 07 Nov 2008 08:23:40 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/126838.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2008/11/07/126838.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/126838.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/126838.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET Tips: DropDownList.ClearSelection() to avoid &amp;quot;Cannot have multiple items selected in DropDownList&amp;quot;</title>
            <link>http://geekswithblogs.net/shahed/archive/2008/08/23/124652.aspx</link>
            <description>&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;     &lt;br /&gt;I was facing the following Exception when I was programmatically assigning Selected Value of a DropDownList with the following piece of code.&lt;/p&gt; ListItem item = DropDownListTest.Items.FindByValue("Test");  &lt;br /&gt;  if (item != null)  &lt;br /&gt;     item.Selected = true;  &lt;br /&gt;  &lt;br /&gt;&lt;strong&gt;Exception&lt;/strong&gt;  &lt;br /&gt;  &lt;h4&gt;&lt;i&gt;Cannot have multiple items selected in a DropDownList.&lt;/i&gt;&lt;/h4&gt; &lt;b&gt;Description: &lt;/b&gt;An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.   &lt;br /&gt;&lt;b&gt;Exception Details: &lt;/b&gt;System.Web.HttpException: Cannot have multiple items selected in a DropDownList.  &lt;br /&gt;&lt;b&gt;Source Error:&lt;/b&gt;  &lt;p&gt;&lt;code&gt;An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Stack Trace:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;  &lt;pre&gt;[HttpException (0x80004005): Cannot have multiple items selected in a DropDownList.]
   System.Web.UI.WebControls.DropDownList.VerifyMultiSelect() +107
   System.Web.UI.WebControls.ListControl.RenderContents(HtmlTextWriter writer) +1825978
   System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
   System.Web.UI.Control.Render(HtmlTextWriter writer) +7
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   CSSFriendly.WizardAdapter.RenderStep(HtmlTextWriter writer, Wizard wizard) +137
   CSSFriendly.WizardAdapter.RenderContents(HtmlTextWriter writer) +93
   System.Web.UI.WebControls.Adapters.WebControlAdapter.Render(HtmlTextWriter writer) +23
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +2116097
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
   System.Web.UI.Control.Render(HtmlTextWriter writer) +7
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
   System.Web.UI.UpdatePanel.RenderChildren(HtmlTextWriter writer) +236
 &lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Solution
    &lt;br /&gt;

    &lt;br /&gt;&lt;/strong&gt;The solution is to remember to call ClearSelection() Method before calling FindByValue("") or FindByText("") Method.

  &lt;br /&gt;

  &lt;br /&gt;The following piece of code will work fine.&lt;/p&gt;

&lt;p&gt;DropDownListTest.ClearSelection();&lt;/p&gt;

&lt;p&gt;ListItem item = DropDownListTest.Items.FindByValue("Test");
  &lt;br /&gt;  if (item != null)

  &lt;br /&gt;     item.Selected = true;

  &lt;br /&gt;

  &lt;br /&gt;I also noticed, I do not get the "Cannot have multiple items" exception when I use SelectedIndex instead. i.e.

  &lt;br /&gt;

  &lt;br /&gt;DropDownListTest.SelectedIndex = 12;&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;Hope this saves some of your time. Happy Coding.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124652"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124652" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/124652.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2008/08/23/124652.aspx</guid>
            <pubDate>Sat, 23 Aug 2008 06:50:13 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/124652.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2008/08/23/124652.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/124652.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/124652.aspx</trackback:ping>
        </item>
        <item>
            <title>Reflection Tips: Generate Mock objects for Test Cases.</title>
            <link>http://geekswithblogs.net/shahed/archive/2008/08/06/124263.aspx</link>
            <description>&lt;p&gt;Filling up Mock objects with random data is possibly one of the most time consuming and boring chapter of writing test cases. Let say we have the following classes and we want to write some mock objects.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ReflectionTipsGenerateMockobjectsforTes_12585/SmartDataManagement%20-%20Microsoft%20Visual%20Studio_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="466" alt="SmartDataManagement - Microsoft Visual Studio" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ReflectionTipsGenerateMockobjectsforTes_12585/SmartDataManagement%20-%20Microsoft%20Visual%20Studio_thumb.png" width="618" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Mock Object Code Example&lt;/strong&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;       private Client GetMockClient()    &lt;br /&gt;        {     &lt;br /&gt;            Client Client1 = new Client();     &lt;br /&gt;            Client1.FirstName = "Random String 6dc22d70-ad0c-4552-9c86-2925e723a326";     &lt;br /&gt;            Client1.LastName = "Random String 3e08dc98-495a-4a70-bff4-b7b21489b0d0"; &lt;/p&gt;  &lt;p&gt;            ClientAddress ClientAddress2 = new ClientAddress(); &lt;/p&gt;  &lt;p&gt;            ContactAddress ContactAddress3 = new ContactAddress();    &lt;br /&gt;            ContactAddress3.Address = "Random String e5957034-893b-4d2c-a29a-5b23e2561c00";     &lt;br /&gt;            ContactAddress3.Phone = "Random String 8d034e87-ece3-42b8-ae97-35fad1639f41";     &lt;br /&gt;            ContactAddress3.Mobile = "Random String 73a6abad-3268-4ae4-b01c-1732ab0f5f52";     &lt;br /&gt;            ClientAddress2.HomeAddress = ContactAddress3; &lt;/p&gt;  &lt;p&gt;            ContactAddress ContactAddress4 = new ContactAddress();    &lt;br /&gt;            ContactAddress4.Address = "Random String 117ef00d-f29d-4697-97ae-123a6b56da4f";     &lt;br /&gt;            ContactAddress4.Phone = "Random String 8e4d0bbe-f470-4438-8940-06d9889e3943";     &lt;br /&gt;            ContactAddress4.Mobile = "Random String 6f0cba5c-5a88-4e5b-88ae-32a569cffa41";     &lt;br /&gt;            ClientAddress2.ShippingAddress = ContactAddress4; &lt;/p&gt;  &lt;p&gt;            Client1.ClientAddress = ClientAddress2; &lt;/p&gt;  &lt;p&gt;            Misc Misc5 = new Misc();    &lt;br /&gt;            Misc5.OrderDate = DateTime.Now;     &lt;br /&gt;            Misc5.Quaitity = 945515547;     &lt;br /&gt;            Misc5.DeliveryDate = DateTime.Now; &lt;/p&gt;  &lt;p&gt;            Customer Customer6 = new Customer(); &lt;/p&gt;  &lt;p&gt;            CustomerDemographics CustomerDemographics7 = new CustomerDemographics();    &lt;br /&gt;            CustomerDemographics7.FirstName = "Random String 9b9542e6-2eb9-4ac8-bbd9-46272c8531b4";     &lt;br /&gt;            CustomerDemographics7.LastName = "Random String 275ca427-9373-41a7-8249-9a3fd94631bd"; &lt;/p&gt;  &lt;p&gt;            ContactAddress ContactAddress8 = new ContactAddress();    &lt;br /&gt;            ContactAddress8.Address = "Random String c9d18341-cb7e-4086-ba09-76fd7eb09f71";     &lt;br /&gt;            ContactAddress8.Phone = "Random String d88849f2-f443-46b1-9d21-3eaed1739696";     &lt;br /&gt;            ContactAddress8.Mobile = "Random String 984cfe2b-c35e-46f4-8eb1-27342d3e21b2";     &lt;br /&gt;            CustomerDemographics7.HomeAddress = ContactAddress8; &lt;/p&gt;  &lt;p&gt;            ContactAddress ContactAddress9 = new ContactAddress();    &lt;br /&gt;            ContactAddress9.Address = "Random String e67ae73f-8edd-47d1-add0-b1e84ddb6b05";     &lt;br /&gt;            ContactAddress9.Phone = "Random String 91b8d44e-3718-4cc7-be8a-194d308b8969";     &lt;br /&gt;            ContactAddress9.Mobile = "Random String a0d19e07-2b80-4f7a-acd2-7a507455c000";     &lt;br /&gt;            CustomerDemographics7.ShippingAddress = ContactAddress9; &lt;/p&gt;  &lt;p&gt;            Customer6.CustomerDemographics = CustomerDemographics7; &lt;/p&gt;  &lt;p&gt;            Misc5.AlternativeCustomer1 = Customer6; &lt;/p&gt;  &lt;p&gt;            Client1.Misc = Misc5; &lt;/p&gt;  &lt;p&gt;            return Client1; &lt;/p&gt;  &lt;p&gt;        }&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;As you can see we will need to spare significant amount of time to prepare a Mock object for "Client" class. But you will be happy to know that I did not write a single line of the code that I pasted above. Yes, I generated it using Reflection. Let me share that magical piece of code with you which can save you a heaps of time.    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Mock Helper&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;    public class MockHelper   &lt;br /&gt;    {    &lt;br /&gt;        public MockHelper()    &lt;br /&gt;        {    &lt;br /&gt;            this.MaxDepth = 100;    &lt;br /&gt;        } &lt;/p&gt;  &lt;p&gt;        int index = 1;    &lt;/p&gt;  &lt;p&gt;        Dictionary&amp;lt;string, string&amp;gt; keys = new Dictionary&amp;lt;string, string&amp;gt;();   &lt;br /&gt;        List&amp;lt;string&amp;gt; assemblyList = new List&amp;lt;string&amp;gt;();    &lt;br /&gt;        StringBuilder codeBuilder = null; &lt;/p&gt;  &lt;p&gt;        public int MaxIndexValue { get; set; }   &lt;br /&gt;        public string Generate(object obj)    &lt;br /&gt;        {    &lt;br /&gt;&lt;strong&gt;            //Assembly List that we are interested in for recursive calls, add according to your need.&lt;/strong&gt;    &lt;br /&gt;            assemblyList.Add("SmartDataManagement.Blog"); &lt;/p&gt;  &lt;p&gt;            codeBuilder = new StringBuilder();   &lt;br /&gt;            GenerateCode(obj, string.Empty);    &lt;br /&gt;            return codeBuilder.ToString();    &lt;br /&gt;        }        &lt;/p&gt;  &lt;p&gt;        private void GenerateCode(object obj, string theKey)   &lt;br /&gt;        {          &lt;br /&gt;            string instanceName = string.Format("{0}{1}", obj.GetType().Name, index);    &lt;br /&gt;            codeBuilder.AppendLine(String.Format("{0} {1} = new {0}();", obj.GetType().Name, instanceName));    &lt;br /&gt;            keys.Add(theKey, instanceName);    &lt;br /&gt;&lt;strong&gt;            //Iterate through the list of Public Properties of the object instance&lt;/strong&gt;    &lt;br /&gt;            foreach (System.Reflection.PropertyInfo property in obj.GetType().GetProperties())    &lt;br /&gt;            {      &lt;br /&gt;&lt;strong&gt;                //Decide to dig deeper, if a PropertyType belongs to the assemblies that we are interested in, we go deep.     &lt;br /&gt;&lt;/strong&gt;                if (GoDeeper(property))    &lt;br /&gt;                {    &lt;br /&gt;                    index = index + 1;                   &lt;br /&gt;                    &lt;strong&gt;//Avoid infinite Loop situation     &lt;br /&gt;&lt;/strong&gt;                    if (index &amp;lt; this.MaxIndexValue)    &lt;br /&gt;                    {    &lt;br /&gt;                        object propertyInstance = property.GetValue(obj, null);                        &lt;br /&gt;                        if (propertyInstance == null)    &lt;br /&gt;                        {    &lt;br /&gt;                            //Dynamically create Property Instance    &lt;br /&gt;                            propertyInstance = Activator.CreateInstance(property.PropertyType);    &lt;br /&gt;                            if (property.CanWrite)    &lt;br /&gt;                                property.SetValue(obj, propertyInstance, null);    &lt;br /&gt;                            string key = Guid.NewGuid().ToString();    &lt;br /&gt;                            codeBuilder.AppendLine();    &lt;br /&gt;                            //Recursive call    &lt;br /&gt;                            GenerateCode(propertyInstance, key); &lt;/p&gt;  &lt;p&gt;                            codeBuilder.AppendLine(String.Format(@"{0}.{1} = {2};", instanceName, property.Name, keys[key]));   &lt;br /&gt;                            codeBuilder.AppendLine();    &lt;br /&gt;                        }    &lt;br /&gt;                    }    &lt;br /&gt;                }    &lt;br /&gt;                else if (property.PropertyType == typeof(string))    &lt;br /&gt;                {    &lt;br /&gt;                    codeBuilder.AppendLine(String.Format(@"{0}.{1} = ""{2}"";", instanceName, property.Name, GetRandomString()));    &lt;br /&gt;                }    &lt;br /&gt;                else if (property.PropertyType == typeof(DateTime))    &lt;br /&gt;                {    &lt;br /&gt;                    codeBuilder.AppendLine(String.Format(@"{0}.{1} = DateTime.Now;", instanceName, property.Name));    &lt;br /&gt;                }    &lt;br /&gt;                else if (property.PropertyType == typeof(int))    &lt;br /&gt;                {    &lt;br /&gt;                    codeBuilder.AppendLine(String.Format(@"{0}.{1} = {2};", instanceName, property.Name, GetRandomInt()));    &lt;br /&gt;                }    &lt;br /&gt;            }    &lt;br /&gt;        } &lt;/p&gt;  &lt;p&gt;        private bool GoDeeper(PropertyInfo property)   &lt;br /&gt;        {    &lt;br /&gt;&lt;strong&gt;            //Dig deeper for interested Assemblies only, Please feel free to extend and put complex logics to serve your purpose.&lt;/strong&gt;    &lt;br /&gt;            foreach (string assemblyName in assemblyList)    &lt;br /&gt;            {    &lt;br /&gt;                if (property.PropertyType.Assembly.FullName.Contains(assemblyName))    &lt;br /&gt;                    return true;    &lt;br /&gt;            }    &lt;br /&gt;            return false;    &lt;br /&gt;        } &lt;/p&gt;  &lt;p&gt;        private string GetRandomString()   &lt;br /&gt;        {  &lt;br /&gt;            return String.Format("Random String {0}", Guid.NewGuid().ToString());            &lt;br /&gt;        } &lt;/p&gt;  &lt;p&gt;        private int GetRandomInt()   &lt;br /&gt;        {    &lt;br /&gt;            byte[] buffer = Guid.NewGuid().ToByteArray();    &lt;br /&gt;            return BitConverter.ToInt32(buffer, 0);    &lt;br /&gt;        }    &lt;br /&gt;    }&lt;/p&gt;  &lt;p&gt;The above code is self explanatory, I also commented to give an idea of how this is working, In short, I take an instance of the desired class, iterate through all of its Properties using Reflection and generate desired piece of code. While iterating, I also create property instance  as required and loop thorough the properties of the child in a recursive manner until all of them are served. The assemblyList object maintains the list of the assemblies that we are interested in for recursion.  &lt;strong&gt;Its worth mentioning the above piece of code has limitations, it will not work in the cases where there are circular references of Types for properties&lt;/strong&gt;, that is why I kept a check for MaxIndexValue to break the infinite loop scenario. &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ReflectionTipsGenerateMockobjectsforTes_12585/SmartDataManagement%20-%20Microsoft%20Visual%20Studio%20(2)_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="254" alt="SmartDataManagement - Microsoft Visual Studio (2)" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ReflectionTipsGenerateMockobjectsforTes_12585/SmartDataManagement%20-%20Microsoft%20Visual%20Studio%20(2)_thumb.png" width="483" border="0" /&gt;&lt;/a&gt;  &lt;br /&gt;    &lt;br /&gt;Fig, shows an example of circular references of Types, here Client has Misc property (of Type: Misc), and Misc has AlternativeClient1 property (of Type: Client).     &lt;br /&gt;    &lt;br /&gt;The MockHelper has very simple logic to decide to create dynamic instances and to do recursive calls, it will get stuck within Client and Misc for this example. We can definitely overcome this by putting more complex logics and checks. I wanted to keep it simple and moreover it serves my purpose. Also here I cater with int, string and DateTime only, please feel free to extend to serve your purpose. You will notice I used StringBuilder above to accumulate the generated codes, but I normally use my &lt;a href="http://www.codeplex.com/smartcodegenerator"&gt;SmartCodeGenerator&lt;/a&gt; and write simple templates for this kind of tricks, &lt;a href="http://www.codeplex.com/smartcodegenerator"&gt;SmartCodeGenerator&lt;/a&gt; provides a lot more flexibility and comes with robust templating feature for purposes like this.    &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;Usage&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;            MockHelper helper = new MockHelper();    &lt;br /&gt;            //Your desired class instance     &lt;br /&gt;            Client client = new Client();    &lt;br /&gt;            string code = helper.Generate(client);&lt;/p&gt;  &lt;p&gt;Its quite simple to use the above helper, just create an instance of the MockHelper, then pass an instance of your desired class, it will return you the mock object code as we have seen above. Infact, the above piece of code can speed you up for any cases where you need to create instances of a class and need to write code to fill all its properties, moreover the generated code can also reduce chances of errors and typos that normally happens when we do it manually.   &lt;br /&gt;    &lt;br /&gt;Thank you for being with me so far, hope this saves you some time, Happy coding :)&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124263"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=124263" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/124263.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2008/08/06/124263.aspx</guid>
            <pubDate>Tue, 05 Aug 2008 14:20:50 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/124263.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2008/08/06/124263.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/124263.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/124263.aspx</trackback:ping>
        </item>
        <item>
            <title>C# Reflection Tips: Data transformation using Reflection</title>
            <link>http://geekswithblogs.net/shahed/archive/2008/07/24/123998.aspx</link>
            <description>&lt;p&gt;.NET Reflection can be quite handy to transform one object to another, and specially when the target data structure varies a lot. Lets say, a "source party" has a stable Source data structure. But different clients have different requirement and expects to served with data in different format. These clients may pass their object instances in their own format and expect to be served with data in their structure. Let me make up an Example here:  &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;Source Data Structure which is consistent and never changes.&lt;/strong&gt;    &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/SmartDataManagement%20-%20Microsoft%20Visual%20Studio_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="255" alt="SmartDataManagement - Microsoft Visual Studio" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/SmartDataManagement%20-%20Microsoft%20Visual%20Studio_thumb.png" width="180" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Client 1 wants data to be transformed/served in the following structure&lt;/strong&gt;  &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/Customer_4.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="323" alt="Customer" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/Customer_thumb_1.png" width="443" border="0" /&gt;&lt;/a&gt;     &lt;br /&gt;&lt;strong&gt; Client 2 wants data to be transformed/served in the following structure&lt;/strong&gt;    &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/Client_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="295" alt="Client" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/Client_thumb.png" width="550" border="0" /&gt;&lt;/a&gt;     &lt;br /&gt;and there may be other N-number of clients with N-number of structures, on which "Source Party" has no control. &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;The use of .NET Attribute and .NET Reflection can produce a very powerful solution to address this kind of scenario. So instead of writing different transforms for different clients, "source party" can simply ask clients to mark their class properties with custom attributes that the "source party" can understand. When clients mark their classes with custom attributes the  "source party" can easily take advantage of .NET Reflection to analyze those objects runtime and can act accordingly.    &lt;br /&gt;    &lt;br /&gt;A .NET Attribute class can be designed to keep track of of the Mapping between the source and target data structure.    &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/attribute_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="285" alt="attribute" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/attribute_thumb.png" width="558" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The client can mark their class/properties with this custom attribute for the (target -&amp;gt; source) mapping.   &lt;br /&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/attributeMapping_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="537" alt="attributeMapping" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/attributeMapping_thumb.png" width="913" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As the mapping has been performed, we can take advantage of .NET Reflection to transform/serve data in different structure ( infact any structure - considering mapping is done properly ). Lets look at the following &lt;strong&gt;magical&lt;/strong&gt; piece of code.    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;public class TransformHelper   &lt;br /&gt;{    &lt;br /&gt;    public Source SourceObject { get; set; }    &lt;br /&gt;    public object GetTransformedObjectObject(object obj)    &lt;br /&gt;    {    &lt;br /&gt;        foreach (PropertyInfo property in obj.GetType().GetProperties())    &lt;br /&gt;        {    &lt;br /&gt;            FillObject(property, obj);    &lt;br /&gt;        }    &lt;br /&gt;        return obj;    &lt;br /&gt;    } &lt;/p&gt;  &lt;p&gt;    private void FillObject(PropertyInfo property, object obj)   &lt;br /&gt;    {    &lt;br /&gt;        //Identify Custom attribute    &lt;br /&gt;        SourceMapAttribute attribute = (SourceMapAttribute)Attribute.GetCustomAttribute(property, typeof(SourceMapAttribute));    &lt;br /&gt;        if (attribute != null)    &lt;br /&gt;        {    &lt;br /&gt;            //Check propertyName    &lt;br /&gt;            &lt;strong&gt;/*Put your desired code and logic here.*/     &lt;br /&gt;&lt;/strong&gt;            if (attribute.PropertyName != string.Empty)    &lt;br /&gt;            {                            &lt;br /&gt;&lt;strong&gt;                /*Put your desired code and logic here.      &lt;br /&gt;                I have simply demonstrated with one attribute property,       &lt;br /&gt;                you can have as many as you like, and can perform any complex operation you prefer.*/ &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;                //Get Source Object Value   &lt;br /&gt;                object sourceValue = GetSourceValue(attribute.PropertyName);    &lt;br /&gt;                if (property.CanWrite)    &lt;br /&gt;                {    &lt;br /&gt;                    //Assign source value to the mapped property    &lt;br /&gt;                    property.SetValue(obj, sourceValue, null);    &lt;br /&gt;                }    &lt;br /&gt;            } &lt;/p&gt;  &lt;p&gt;            Type propertyType = property.PropertyType;   &lt;br /&gt;            object propertyInstance = property.GetValue(obj, null); &lt;/p&gt;  &lt;p&gt;            if (propertyInstance == null)   &lt;br /&gt;            {    &lt;br /&gt;                //Instantiate when Property is not instantiated    &lt;br /&gt;                propertyInstance = Activator.CreateInstance(property.PropertyType);    &lt;br /&gt;                if (property.CanWrite)    &lt;br /&gt;                    property.SetValue(obj, propertyInstance, null);    &lt;br /&gt;            } &lt;/p&gt;  &lt;p&gt;            foreach (PropertyInfo info in propertyType.GetProperties())   &lt;br /&gt;            {    &lt;br /&gt;                &lt;strong&gt;//recursive call     &lt;br /&gt;&lt;/strong&gt;                FillObject(info, propertyInstance);    &lt;br /&gt;            }    &lt;br /&gt;        }    &lt;br /&gt;    } &lt;/p&gt;  &lt;p&gt;    private object GetSourceValue(string propertyName)   &lt;br /&gt;    {    &lt;br /&gt;        if (SourceObject == null)    &lt;br /&gt;        {    &lt;br /&gt;            SourceDataProvider provider = new SourceDataProvider();    &lt;br /&gt;            this.SourceObject = provider.GetSource();    &lt;br /&gt;        } &lt;/p&gt;  &lt;p&gt;        switch (propertyName)   &lt;br /&gt;        {    &lt;br /&gt;            case "FirstName":    &lt;br /&gt;                return SourceObject.FirstName;    &lt;br /&gt;            case "LastName":    &lt;br /&gt;                return SourceObject.LastName;    &lt;br /&gt;            case "ContactAddress":    &lt;br /&gt;                return this.SourceObject.ContactAddress;    &lt;br /&gt;            case "ContactPhone":    &lt;br /&gt;                return this.SourceObject.ContactPhone;    &lt;br /&gt;            case "ContactMobile":    &lt;br /&gt;                return this.SourceObject.ContactMobile;    &lt;br /&gt;            case "ShippingAddress":    &lt;br /&gt;                return this.SourceObject.ShippingAddress;    &lt;br /&gt;            case "ShippingPhone":    &lt;br /&gt;                return this.SourceObject.ShippingPhone;    &lt;br /&gt;            case "ShippingMobile":    &lt;br /&gt;                return this.SourceObject.ShippingMobile;    &lt;br /&gt;            default:    &lt;br /&gt;                return string.Empty;    &lt;br /&gt;        }    &lt;br /&gt;    }    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;Its worth talking a little bit about the above code snippet, all it does is takes an instance of the target class, uses reflection to loop through all its properties, while doing that identifies the custom attributes, checks for the mapping property and assigns values from the Source. This also creates property instances as required and recursively keeps working until all the properties ( including all descendents ) are checked and served.   &lt;br /&gt;    &lt;br /&gt;Lets look at this Transformer in action.    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Serve Client 1&lt;/strong&gt;:    &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/Client1_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="121" alt="Client1" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/Client1_thumb.png" width="531" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here is what we see after transformation.   &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/serveclient%201_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="279" alt="serveclient 1" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/serveclient%201_thumb.png" width="804" border="0" /&gt;&lt;/a&gt;     &lt;br /&gt;    &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;Serve Client 2&lt;/strong&gt;:    &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/client2_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="104" alt="client2" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/client2_thumb.png" width="486" border="0" /&gt;&lt;/a&gt;     &lt;br /&gt;and here is the result.    &lt;br /&gt;    &lt;br /&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/serveclient2_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="355" alt="serveclient2" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/CReflectionTipsDatatransformationusingRe_2990/serveclient2_thumb.png" width="541" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;In this way we can serve any clients objects in any structure ( considering, mapping is done properly ). You will sure agree with me Transforming one object to another by taking advantage of .NET Attribute and .NET Reflection is quite cool. Thank you for being with me so far, Happy coding :)    &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=123998"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=123998" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/123998.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2008/07/24/123998.aspx</guid>
            <pubDate>Wed, 23 Jul 2008 19:52:24 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/123998.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2008/07/24/123998.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/123998.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/123998.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET tips: Golden rules for Dynamic Controls.</title>
            <link>http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx</link>
            <description>&lt;p&gt;1. Make sure your dynamic controls are Loaded on every postback.    &lt;br /&gt;    &lt;br /&gt;Lets play with a very simple example,     &lt;br /&gt;    &lt;br /&gt;ASPX     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&amp;lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&amp;gt; &lt;/p&gt;  &lt;p&gt;&amp;lt;body&amp;gt;    &lt;br /&gt;    &amp;lt;form id="form1" runat="server"&amp;gt;     &lt;br /&gt;    &amp;lt;div&amp;gt;     &lt;br /&gt;        &amp;lt;asp:PlaceHolder ID="PlaceHolder1" runat="server"&amp;gt;&amp;lt;/asp:PlaceHolder&amp;gt;     &lt;br /&gt;        &amp;lt;asp:Button ID="Button1" runat="server" Text="Button" /&amp;gt;     &lt;br /&gt;    &amp;lt;/div&amp;gt;     &lt;br /&gt;    &amp;lt;/form&amp;gt;     &lt;br /&gt;&amp;lt;/body&amp;gt;     &lt;br /&gt;&amp;lt;/html&amp;gt; &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;C# Code Behind     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;public partial class _Default : System.Web.UI.Page    &lt;br /&gt;{    &lt;br /&gt;    protected void Page_Load(object sender, EventArgs e)     &lt;br /&gt;    {     &lt;br /&gt;        TextBox t = new TextBox();     &lt;br /&gt;        t.ID = "textBox";     &lt;br /&gt;        this.PlaceHolder1.Controls.Add(t);         &lt;br /&gt;    } &lt;/p&gt;  &lt;p&gt;}    &lt;br /&gt;    &lt;br /&gt;The above code works fine, but a common mistake is to try to conditionally load dynamic controls, if we tweak the code a little bit you will notice we loose our TextBox after any postback. The following code will not load the TextBox after our first postback.     &lt;br /&gt;    &lt;br /&gt;public partial class _Default : System.Web.UI.Page     &lt;br /&gt;{    &lt;br /&gt;    protected void Page_Load(object sender, EventArgs e)     &lt;br /&gt;    {       &lt;br /&gt;       &lt;strong&gt;if (!IsPostBack)      &lt;br /&gt;&lt;/strong&gt;        {     &lt;br /&gt;            TextBox t = new TextBox();     &lt;br /&gt;            t.ID = "textBox";     &lt;br /&gt;            this.PlaceHolder1.Controls.Add(t);     &lt;br /&gt;        }     &lt;br /&gt;    } &lt;/p&gt;  &lt;p&gt;} &lt;/p&gt;  &lt;p&gt;Its recommended to load the dynamic controls during the Page_Init instead, because we may want to hook up our events with proper handler at an early stage.    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;public partial class _Default : System.Web.UI.Page    &lt;br /&gt;{     &lt;br /&gt;    &lt;strong&gt;protected void Page_Init(object sender, EventArgs e)      &lt;br /&gt;&lt;/strong&gt;    {     &lt;br /&gt;        TextBox t = new TextBox();     &lt;br /&gt;        t.ID = "textBox";     &lt;br /&gt;        t.TextChanged+=new EventHandler(t_TextChanged);     &lt;br /&gt;        this.PlaceHolder1.Controls.Add(t);     &lt;br /&gt;    } &lt;/p&gt;  &lt;p&gt;} &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;2. Do not assigning properties of a dynamic control (viewstate enabled), during Page_Init, it will not be reflected.     &lt;br /&gt;    &lt;br /&gt;Here is scenario of another common mistake, "123" assigned to the Text property during Page_Init,     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;public partial class _Default : System.Web.UI.Page    &lt;br /&gt;{     &lt;br /&gt;    protected void Page_Init(object sender, EventArgs e)     &lt;br /&gt;    {     &lt;br /&gt;        TextBox t = new TextBox();     &lt;br /&gt;        t.ID = "textBox";     &lt;br /&gt;       &lt;strong&gt;t.Text = "123";      &lt;br /&gt;&lt;/strong&gt;        this.PlaceHolder1.Controls.Add(t);     &lt;br /&gt;    } &lt;/p&gt;  &lt;p&gt;} &lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsGoldenrulesforDynamicControl_14FD2/controllifecycle_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="244" alt="controllifecycle" src="http://geekswithblogs.net/images/geekswithblogs_net/shahed/WindowsLiveWriter/ASP.NETtipsGoldenrulesforDynamicControl_14FD2/controllifecycle_thumb.png" width="108" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;the above code will not work because, Initialization happens before LoadViewState during the control lifecycle. The value assigned to the properties during Initialization will simply get overwritten by the ViewState values.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;3. If you are expecting your ViewState to retain after the postback, always assign same ID to the dynamic control    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;The following piece of code will not work, as I am assigning a new ID to the dynamic control after each postback. The LoadViewState retrieves previously saved viewstate data using the control ID, as the control ID has changed, it doesn't know anymore what to load, as a result it cannot load previously saved viewstate data any more.&lt;/p&gt;  &lt;p&gt;public partial class _Default : System.Web.UI.Page    &lt;br /&gt;{     &lt;br /&gt;    protected void Page_Init(object sender, EventArgs e)     &lt;br /&gt;    {     &lt;br /&gt;        TextBox t = new TextBox();     &lt;br /&gt;&lt;strong&gt;        t.ID = Guid.NewGuid().ToString(); &lt;/strong&gt;    &lt;br /&gt;        this.form1.Controls.Add(t);        &lt;br /&gt;    }     &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Thank you for being with me so far.    &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=123391"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=123391" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/123391.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx</guid>
            <pubDate>Wed, 25 Jun 2008 16:36:46 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/123391.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/123391.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/123391.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET tips, Making Custom Validators work in Partial Rendering mode.</title>
            <link>http://geekswithblogs.net/shahed/archive/2008/06/17/122926.aspx</link>
            <description>&lt;p&gt;&lt;strong&gt;Introduction     &lt;br /&gt;&lt;/strong&gt;    &lt;br /&gt;There are many situations where we need to identify if partial rendering is supported in a page, especially when a control uses javascript, to get the control work in partial rendering mode, the script needs to be registered using a ScriptManager Type instead. A classic example will be Validators.    &lt;br /&gt;    &lt;br /&gt;The ASP.NET Page class exposes the Validators property, which is a list of all the IValidator types on the page. A page keeps track of its validators, and registers a javascript array of validators automatically to the page. Example, When we add 3 RequiredFieldValidator in a page the following javascript Array will be automatically generated and added in our page automatically during the page load.  &lt;br /&gt;    &lt;br /&gt;Page_Validators = new Array(document.getElementById("RequiredFieldValidator1"),     &lt;br /&gt;document.getElementById("RequiredFieldValidator2"),     &lt;br /&gt;document.getElementById("RequiredFieldValidator3"));    &lt;br /&gt;    &lt;br /&gt;The ASP.NET Page also registers couple of other script which eventually hooks up different events ( onclick, onkeypress, onchange, onblur ) to the the target control (ControlToValidate), to some predefined javascript functions that resides in WebUIValidation.js file. So when we add a validator in our Page we also notice the following script is automatically added. [WebUIValidation.js ships with ASP.NET and resides in the following folder "/aspnet_client/system_web/&amp;lt;version&amp;gt;/WebUIValidation.js".]    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&amp;lt;script type="text/javascript"&amp;gt;   &lt;br /&gt;&amp;lt;!--    &lt;br /&gt;var Page_ValidationActive = false;    &lt;br /&gt;if (typeof(ValidatorOnLoad) == "function") {    &lt;br /&gt; ValidatorOnLoad();    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;function ValidatorOnSubmit() {   &lt;br /&gt; if (Page_ValidationActive) {    &lt;br /&gt; return ValidatorCommonOnSubmit();    &lt;br /&gt; }    &lt;br /&gt; else {    &lt;br /&gt; return true;    &lt;br /&gt; }    &lt;br /&gt;}    &lt;br /&gt;// --&amp;gt;    &lt;br /&gt;&amp;lt;/script&amp;gt;&lt;/p&gt;  &lt;p&gt;ValidatorOnLoad plays the big role of hooking up the the events mentioned above, and here is a code snippet from this function,&lt;/p&gt;  &lt;p&gt; for (i = 0; i &amp;lt; Page_Validators.length; i++) {   &lt;br /&gt; val = Page_Validators[i];    &lt;br /&gt; if (typeof(val.evaluationfunction) == "string") {    &lt;br /&gt; eval("val.evaluationfunction = " + val.evaluationfunction + ";");    &lt;br /&gt; }    &lt;br /&gt;...&lt;/p&gt;  &lt;p&gt;if (typeof(val.controltovalidate) == "string") {   &lt;br /&gt; ValidatorHookupControlID(val.controltovalidate, val);    &lt;br /&gt; }    &lt;br /&gt;...    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;keen eyes may have already noticed the val.evaluationfunction property, yes every validators needs to have this property for it to work properly under the ASP.NET validation framework. Custom validators takes advantage of this property to point to custom js functions. Custom validator developers normally use RegisterExpandoAttribute method to register this attribute.    &lt;br /&gt;    &lt;br /&gt;protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)    &lt;br /&gt;{    &lt;br /&gt;   base.AddAttributesToRender(writer);    &lt;br /&gt;   if (this.RenderUplevel)    &lt;br /&gt;   {    &lt;br /&gt;      string clientID = this.ClientID;    &lt;br /&gt;      Page.ClientScript.RegisterExpandoAttribute(clientID, "evaluationfunction", "EntryValidatorEvaluateIsValid");    &lt;br /&gt;   }    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;Problem&lt;/strong&gt;    &lt;br /&gt;When I used Update Panel with partial rendering enabled the Page.ClientScript.RegisterExpandoAttribute did not work for me. My validators always stopped working after the first postback, which was performed via partial rendering and triggering. I found the "evaluationfunction" in the javascript to be undefined.    &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;Solution&lt;/strong&gt;    &lt;br /&gt;I started looking under the hood, and soon discovered, that the ASP.NET Validators that ships out of the box, ( eg. RangeValidator, RequiredFieldValidator ) uses a different internal method "AddExpandoAttribute" to register the property. Here is a code snippet from the RangeValidator.    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;protected override void AddAttributesToRender(HtmlTextWriter writer)   &lt;br /&gt;{    &lt;br /&gt;    base.AddAttributesToRender(writer);    &lt;br /&gt;    if (base.RenderUplevel)    &lt;br /&gt;    {    &lt;br /&gt;        string clientID = this.ClientID;    &lt;br /&gt;        HtmlTextWriter writer2 = base.EnableLegacyRendering ? writer : null;    &lt;br /&gt;        base.AddExpandoAttribute(writer2, clientID, "evaluationfunction", "RangeValidatorEvaluateIsValid", false);   &lt;br /&gt;        ...    &lt;br /&gt;    }    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;and code snippet from BaseValidator, the internal method AddExpandoAttribute.&lt;/p&gt;  &lt;p&gt;internal void AddExpandoAttribute(HtmlTextWriter writer, string controlId, string attributeName, string attributeValue, bool encode)   &lt;br /&gt;{    &lt;br /&gt;    AddExpandoAttribute(this, writer, controlId, attributeName, attributeValue, encode);    &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;After digging further I realized, AddExpandoAttribute checks the ASP.Page whether partial rendering is supported, then it registers the attribute using ScriptManager instead. I did the same with my validation control and it works for me. Here is the piece of code that solved my problem.   &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)   &lt;br /&gt;       {    &lt;br /&gt;           base.AddAttributesToRender(writer);    &lt;br /&gt;           if (this.RenderUplevel)    &lt;br /&gt;           {    &lt;br /&gt;               string clientID = this.ClientID;    &lt;br /&gt;               if (!this.IsPartialRenderingSupported)    &lt;br /&gt;               {    &lt;br /&gt;                   Page.ClientScript.RegisterExpandoAttribute(clientID, "evaluationfunction", "EntryValidatorEvaluateIsValid");                    &lt;br /&gt;               }    &lt;br /&gt;               else    &lt;br /&gt;               {    &lt;br /&gt;                   Type scriptManagerType = BuildManager.GetType("System.Web.UI.ScriptManager", false);    &lt;br /&gt;                   scriptManagerType.InvokeMember("RegisterExpandoAttribute", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object[] { this, clientID, "evaluationfunction", "QuantityEntryValidatorEvaluateIsValid", false });    &lt;br /&gt;               }    &lt;br /&gt;           }    &lt;br /&gt;       }    &lt;br /&gt;    &lt;br /&gt;Note, the I am first checking whether Partial Rendering is Supported and using the ScriptManager  Type to register the property instead.    &lt;br /&gt;    &lt;br /&gt;The following piece of code uses Reflection to figure out whether partial rendering is supported.     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;internal bool IsPartialRenderingSupported   &lt;br /&gt;{    &lt;br /&gt;    get    &lt;br /&gt;    {    &lt;br /&gt;        if (!this.PartialRenderingChecked)    &lt;br /&gt;        {    &lt;br /&gt;            Type scriptManagerType = BuildManager.GetType("System.Web.UI.ScriptManager", false);    &lt;br /&gt;            if (scriptManagerType != null)    &lt;br /&gt;            {    &lt;br /&gt;                object obj2 = this.Page.Items[scriptManagerType];    &lt;br /&gt;                if (obj2 != null)    &lt;br /&gt;                {    &lt;br /&gt;                    PropertyInfo property = scriptManagerType.GetProperty("SupportsPartialRendering");    &lt;br /&gt;                    if (property != null)    &lt;br /&gt;                    {    &lt;br /&gt;                        object obj3 = property.GetValue(obj2, null);    &lt;br /&gt;                        this.IsPartialRenderingEnabled = (bool)obj3;    &lt;br /&gt;                    }    &lt;br /&gt;                }    &lt;br /&gt;            }    &lt;br /&gt;            this.PartialRenderingChecked = true;    &lt;br /&gt;        }    &lt;br /&gt;        return this.IsPartialRenderingEnabled;    &lt;br /&gt;    } &lt;/p&gt;  &lt;p&gt;} &lt;/p&gt;  &lt;p&gt;private bool PartialRenderingChecked   &lt;br /&gt;{    &lt;br /&gt;    get    &lt;br /&gt;    {    &lt;br /&gt;        object val = ViewState["PartialRenderingChecked"];    &lt;br /&gt;        if (val != null)    &lt;br /&gt;            return (bool)val;    &lt;br /&gt;        return false;    &lt;br /&gt;    }    &lt;br /&gt;    set    &lt;br /&gt;    {    &lt;br /&gt;        ViewState["PartialRenderingChecked"] = value;    &lt;br /&gt;    }    &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;private bool IsPartialRenderingEnabled   &lt;br /&gt;{    &lt;br /&gt;    get    &lt;br /&gt;    {    &lt;br /&gt;        object val = ViewState["IsPartialRenderingEnabled"];    &lt;br /&gt;        if (val != null)    &lt;br /&gt;            return (bool)val;    &lt;br /&gt;        return false;    &lt;br /&gt;    }    &lt;br /&gt;    set    &lt;br /&gt;    {    &lt;br /&gt;        ViewState["IsPartialRenderingEnabled"] = value;    &lt;br /&gt;    }    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The Page.ClientScript.RegisterExpandoAttribute may not work in Partial Rendiring mode, when a postback is performed via triggering,    &lt;br /&gt;to get this work we need to determine whether partial rendering is supported and use the ScriptManager Type instead like described above.     &lt;br /&gt;    &lt;br /&gt;Hope this helps, and saves some of your time, Thank you for being with me so far.    &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=122926"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=122926" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/122926.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2008/06/17/122926.aspx</guid>
            <pubDate>Mon, 16 Jun 2008 19:43:27 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/122926.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2008/06/17/122926.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/122926.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/122926.aspx</trackback:ping>
        </item>
        <item>
            <title>C# 3.0 tips, Automatic Property</title>
            <link>http://geekswithblogs.net/shahed/archive/2008/06/11/122791.aspx</link>
            <description>&lt;p&gt;Declaring a property in C# 3.0 is super easy and super short.    &lt;br /&gt;    &lt;br /&gt;public class Student     &lt;br /&gt;{     &lt;br /&gt;  public string Name {  get; set; }     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;yes that's it, the framework will take care of the rest, the private variables will be automatically created and the getter and setter will be automatically implemented.     &lt;br /&gt;    &lt;br /&gt;Here is how we can assign value to an automatic property via the constructor     &lt;br /&gt;    &lt;br /&gt;public class Student     &lt;br /&gt;{     &lt;br /&gt;    public string Name {  get; set; }     &lt;br /&gt;    public Student (string name)     &lt;br /&gt;    {&lt;/p&gt;  &lt;p&gt;       this.Name = name;&lt;/p&gt;  &lt;p&gt;    }  &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;And finally, here is how we can declare a Readonly property     &lt;br /&gt;    &lt;br /&gt;public class Student     &lt;br /&gt;{  &lt;/p&gt;     public string Name {  get; private set; }   &lt;p&gt;   public Student (string name)    &lt;br /&gt;   {&lt;/p&gt;  &lt;p&gt;     this.Name = name;&lt;/p&gt;  &lt;p&gt;   }  &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;Hope this helps, Enjoy coding.     &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=122791"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=122791" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/shahed/aggbug/122791.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Shahed Khan</dc:creator>
            <guid>http://geekswithblogs.net/shahed/archive/2008/06/11/122791.aspx</guid>
            <pubDate>Tue, 10 Jun 2008 14:21:52 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/shahed/comments/122791.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/shahed/archive/2008/06/11/122791.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/shahed/comments/commentRss/122791.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/shahed/services/trackbacks/122791.aspx</trackback:ping>
        </item>
    </channel>
</rss>