tag:blogger.com,1999:blog-35898127932409175242024-03-14T14:56:49.700+11:00Tech NotesSam FuSam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.comBlogger287125tag:blogger.com,1999:blog-3589812793240917524.post-78400502663659675702011-01-13T10:32:00.015+11:002011-01-13T11:54:00.862+11:00Inversion of Control<div><div><div><ul><li>It breaks dependencies between classes and promote loose coupling.</li><li>IoC is about programming to interface, if A uses B, then you can create an interface I that B implements and then let A uses an instance of I.</li><li>Dependency Injection & Service Locator are two ways of implementing IoC.</li><li>Dependency Injection</li><ul><li>Constructor injection: Constructor Injection is the DI technique of passing an object's dependencies to its constructor.</li><li>Property injection</li></ul><li>IoC is vital for doing Test Driven Development. To be able to unit test your ASP.NET application, we will need to loose couple the business logic layer and data access layer, a mock object can be used. A mock object simulates the responses of an actual object, acting as if it's using a real resource. They're great for mocking access to a database, mocking calls to IO, mocking calls to a web service, etc.</li></ul></div><div>References:</div><div><a href="http://www.codeproject.com/KB/architecture/DependencyInjection.aspx">Dependency Injection for Loose Coupling</a></div></div></div><div><a href="http://joelabrahamsson.com/entry/inversion-of-control-introduction-with-examples-in-dotnet">Inversion of Control – An Introduction with Examples in .NET</a></div>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-57949491971531759102010-03-05T17:09:00.007+11:002010-03-05T17:37:15.361+11:00ASP.NET Caching<div><span class="Apple-style-span" style="font-family:arial;">The ASP.NET 2.0 Framework supports the following types of caching:</span></div><div><ul><li><span class="Apple-style-span" style="font-family:arial;">Page Output Caching</span></li><li><span class="Apple-style-span" style="font-family:arial;">Partial Page Caching</span></li><li><span class="Apple-style-span" style="font-family:arial;">DataSource Caching</span></li><li><span class="Apple-style-span" style="font-family:arial;">Data Caching</span><ul><li><span class="Apple-style-span" style="font-family:arial;">When using the cache, it is important to understand that items that you add to the cache might not be there when you attempt to retrieve the item in the future. The cache supports scavenging. When memory resources become low, items are automatically evicted from the cache.</span></li><li><span class="Apple-style-span" style="font-family:arial;">When you add an item to the Cache, you can specify a particular priority for the item. Specifying a priority provides you with some control over when an item gets evicted from the Cache.</span></li><li><span class="Apple-style-span" style="font-family:arial;">You can add almost any object to the cache. For example, you can add custom components, DataSets, DataTables, ArrayLists, and Lists to the cache.</span></li><li><span class="Apple-style-span" style="font-family:arial;">You shouldn’t add items to the cache that depend on an external resource. For example, it does not make sense to add a SqlDataReader or a FileStream to the cache. When using a SqlDataReader, you need to copy the contents of the SqlDataReader into a static representation such as an ArrayList or List collection.</span></li><li><span class="Apple-style-span"><span class="Apple-style-span" style="font-family:arial;">Adding Items with Dependencies: When you add an item to the Cache object, you can make the item dependent on an external object. If the external object is modified, then the item is automatically dropped from the cache.</span></span></li><li><span class="Apple-style-span" style="font-family:arial;">You can configure the size of the cache by using the web configuration file. Notice that you can’t set the size of the cache directly. However, you can specify limits on the overall memory that your application consumes, which indirectly limits the size of the cache.</span></li></ul></li></ul></div><div><span class="Apple-style-span" style="font-family:arial;">Code Sample:</span></div><div><span class="Apple-style-span" style="font-family:arial;"><br /></span></div><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><span class="Apple-style-span" style="font-family:arial;"><br /></span><div style="font-size: 10pt; color: black; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; "><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"> </p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span style="color:blue;"><span class="Apple-style-span" style="font-family:arial;">private</span></span><span class="Apple-style-span" style="font-family:arial;"> </span><span style="color:blue;"><span class="Apple-style-span" style="font-family:arial;">string</span></span><span class="Apple-style-span" style="font-family:arial;"> GetDataFromCache()</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;">{</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;"> var data = context.Cache[</span><span style="color:#a31515;"><span class="Apple-style-span" style="font-family:arial;">"data"</span></span><span class="Apple-style-span" style="font-family:arial;">];</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"> <span style="color:blue;"><span class="Apple-style-span" style="font-family:arial;">if</span></span><span class="Apple-style-span" style="font-family:arial;"> (data == </span><span style="color:blue;"><span class="Apple-style-span" style="font-family:arial;">null</span></span><span class="Apple-style-span" style="font-family:arial;">)</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;"> {</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"> <span style="color:blue;"><span class="Apple-style-span" style="font-family:arial;">lock</span></span><span class="Apple-style-span" style="font-family:arial;"> (lockString)</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;"> {</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"> <span style="color:blue;"><span class="Apple-style-span" style="font-family:arial;">if</span></span><span class="Apple-style-span" style="font-family:arial;"> (data == </span><span style="color:blue;"><span class="Apple-style-span" style="font-family:arial;">null</span></span><span class="Apple-style-span" style="font-family:arial;">)</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;"> {</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;"> data = GetData();</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;"> context.Cache.Insert(</span><span style="color:#a31515;"><span class="Apple-style-span" style="font-family:arial;">"data"</span></span><span class="Apple-style-span" style="font-family:arial;">, data, </span><span style="color:blue;"><span class="Apple-style-span" style="font-family:arial;">null</span></span><span class="Apple-style-span" style="font-family:arial;">, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;"> }</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;"> }</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;"> }</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"> <span style="color:blue;"><span class="Apple-style-span" style="font-family:arial;">return</span></span><span class="Apple-style-span" style="font-family:arial;"> data.ToString();</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span><p style="margin: 0px;"><span class="Apple-style-span" style="font-family:arial;">}</span></p><span class="Apple-style-span" style="font-family:arial;"><br /></span></div><span class="Apple-style-span" style="font-family:arial;"><br /><br /></span></div><span class="Apple-style-span" style="font-family:arial;"><br /><br />References:</span><div><span class="Apple-style-span" style="font-family:arial;"><br /></span><div><div><a href="http://www.amazon.com/ASP-NET-2-0-Unleashed-Stephen-Walther/dp/0672328232"><span class="Apple-style-span" style="font-family:arial;">ASP.NET 2.0 Unleashed</span></a></div><div><a href="http://stackoverflow.com/questions/39112/what-is-the-best-way-to-lock-cache-in-asp-net"><span class="Apple-style-span" style="font-family:arial;">What is the best way to lock cache in asp.net?</span></a></div></div></div>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-87174257368315041712010-03-04T17:52:00.005+11:002010-03-05T09:55:57.783+11:00Kentico BizForm Custom ValidationHere is how you can custom validate Kentico BizForm fields:<br /><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><div style=" background: white;font-family:Courier New;font-size:10pt;color:black;"><p style="margin: 0px;"><span style="color:blue;">protected</span> <span style="color:blue;">void</span> Page_Init(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p><br /><p style="margin: 0px;"> {</p><br /><p style="margin: 0px;"> BizForm.FormName = <span style="color:#2b91af;">KenticoHelper</span>.GetDocumentValueByKey(<span style="color:#a31515;">"BizFormName"</span>);</p><br /><p style="margin: 0px;"> BizForm.OnBeforeSave += BizForm_OnBeforeSave;</p><p style="margin: 0px;"> </p><br /><p style="margin: 0px;"> }</p></div><br /></div><br /><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><div style=" background: white;font-family:Courier New;font-size:10pt;color:black;"><p style="margin: 0px;"><span style="color:blue;">protected</span> <span style="color:blue;">void</span> BizForm_OnBeforeSave()</p><p style="margin: 0px;"> {</p><br /><p style="margin: 0px;"> CMS.FormControls.<span style="color:#2b91af;">FormEngineUserControl</span> em1 = (CMS.FormControls.<span style="color:#2b91af;">FormEngineUserControl</span>)BizForm.BasicForm.FieldControls[<span style="color:#a31515;">"EmailAddress"</span>];</p><br /><p style="margin: 0px;"> CMS.FormControls.<span style="color:#2b91af;">FormEngineUserControl</span> em2 = (CMS.FormControls.<span style="color:#2b91af;">FormEngineUserControl</span>)BizForm.BasicForm.FieldControls[<span style="color:#a31515;">"ConfirmEmailAddress"</span>];</p><br /><p style="margin: 0px;"> <span style="color:blue;">string</span> EmailAddress = em1.Value.ToString();</p><br /><p style="margin: 0px;"> <span style="color:blue;">string</span> ValidateEmailAddress = em2.Value.ToString();</p><p style="margin: 0px;"> </p><br /><p style="margin: 0px;"> <span style="color:blue;">if</span> (EmailAddress != ValidateEmailAddress)</p><br /><p style="margin: 0px;"> {</p><br /><p style="margin: 0px;"> CMS.ExtendedControls.<span style="color:#2b91af;">LocalizedLabel</span> ictrl = (CMS.ExtendedControls.<span style="color:#2b91af;">LocalizedLabel</span>)BizForm.BasicForm.FieldErrorLabels[<span style="color:#a31515;">"ConfirmEmailAddress"</span>];</p><br /><p style="margin: 0px;"> ictrl.Visible = <span style="color:blue;">true</span>;</p><br /><p style="margin: 0px;"> ictrl.Text = <span style="color:#a31515;">"The e-mail addresses do not match, please check it."</span>;</p><br /><p style="margin: 0px;"> <span style="color:blue;">this</span>.BizForm.StopProcessing = <span style="color:blue;">true</span>;</p><br /><p style="margin: 0px;"> }</p><p style="margin: 0px;"> </p><br /><p style="margin: 0px;"> <span style="color:#2b91af;">CheckBox</span> cbAgreeTerms = (<span style="color:#2b91af;">CheckBox</span>)BizForm.BasicForm.FieldControls[<span style="color:#a31515;">"AgreeTerms"</span>];</p><br /><p style="margin: 0px;"> <span style="color:blue;">if</span> (!cbAgreeTerms.Checked)</p><br /><p style="margin: 0px;"> {</p><br /><p style="margin: 0px;"> CMS.ExtendedControls.<span style="color:#2b91af;">LocalizedLabel</span> agreeTerms_ErrorLabel = (CMS.ExtendedControls.<span style="color:#2b91af;">LocalizedLabel</span>)BizForm.BasicForm.FieldErrorLabels[<span style="color:#a31515;">"AgreeTerms"</span>];</p><br /><p style="margin: 0px;"> agreeTerms_ErrorLabel.Visible = <span style="color:blue;">true</span>;</p><br /><p style="margin: 0px;"> agreeTerms_ErrorLabel.Text = <span style="color:#a31515;">"Field Required."</span>;</p><br /><p style="margin: 0px;"> <span style="color:blue;">this</span>.BizForm.StopProcessing = <span style="color:blue;">true</span>;</p><br /><p style="margin: 0px;"> }</p><p style="margin: 0px;"> </p><br /><p style="margin: 0px;"> <span style="color:#2b91af;">CheckBox</span> cbUnderstandConfidential = (<span style="color:#2b91af;">CheckBox</span>)BizForm.BasicForm.FieldControls[<span style="color:#a31515;">"UnderstandConfidential"</span>];</p><br /><p style="margin: 0px;"> <span style="color:blue;">if</span> (!cbUnderstandConfidential.Checked)</p><br /><p style="margin: 0px;"> {</p><br /><p style="margin: 0px;"> CMS.ExtendedControls.<span style="color:#2b91af;">LocalizedLabel</span> understandConfidential_ErrorLabel = (CMS.ExtendedControls.<span style="color:#2b91af;">LocalizedLabel</span>)BizForm.BasicForm.FieldErrorLabels[<span style="color:#a31515;">"UnderstandConfidential"</span>];</p><br /><p style="margin: 0px;"> understandConfidential_ErrorLabel.Visible = <span style="color:blue;">true</span>;</p><br /><p style="margin: 0px;"> understandConfidential_ErrorLabel.Text = <span style="color:#a31515;">"Field Required."</span>;</p><br /><p style="margin: 0px;"> <span style="color:blue;">this</span>.BizForm.StopProcessing = <span style="color:blue;">true</span>;</p><br /><p style="margin: 0px;"> }</p><p style="margin: 0px;"> </p><br /><p style="margin: 0px;"> <span style="color:#2b91af;">CheckBox</span> cbUnderstandChecks = (<span style="color:#2b91af;">CheckBox</span>)BizForm.BasicForm.FieldControls[<span style="color:#a31515;">"UnderstandChecks"</span>];</p><br /><p style="margin: 0px;"> <span style="color:blue;">if</span> (!cbUnderstandChecks.Checked)</p><br /><p style="margin: 0px;"> {</p><br /><p style="margin: 0px;"> CMS.ExtendedControls.<span style="color:#2b91af;">LocalizedLabel</span> understandChecks_ErrorLabel = (CMS.ExtendedControls.<span style="color:#2b91af;">LocalizedLabel</span>)BizForm.BasicForm.FieldErrorLabels[<span style="color:#a31515;">"UnderstandChecks"</span>];</p><br /><p style="margin: 0px;"> understandChecks_ErrorLabel.Visible = <span style="color:blue;">true</span>;</p><br /><p style="margin: 0px;"> understandChecks_ErrorLabel.Text = <span style="color:#a31515;">"Field Required."</span>;</p><br /><p style="margin: 0px;"> <span style="color:blue;">this</span>.BizForm.StopProcessing = <span style="color:blue;">true</span>;</p><br /><p style="margin: 0px;"> }</p><p style="margin: 0px;"> }</p></div><br /><br /></div>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-87770345412223804232010-01-18T16:16:00.006+11:002010-01-18T16:51:06.901+11:00WCF Basics<span style="font-family:arial;">Service Contract<br /><br /></span><ul><li><span style="font-family:arial;">A Service Contract describes what the service can do. It defines some properties about the service, and a set of actions called Operation Contracts. Operation Contracts are equivalent to web methods in ASMX, or to operation element in WSDL. </span></li><li><span style="font-family:arial;">A service contract is an interface that the WCF tools and infrastructure can convert into a WSDL document, listing the operations for a service as a series of SOAP messages and message responses. </span></li><li><span style="font-family:arial;">You provide an implementation of these methods in a class in the service. When a service executes, the WCF runtime creates a channel stack by using the bindings specified in the service configuration file and listens for client requests in the form of one of these messages. The WCF runtime then converts each SOAP message sent by a client application into a method call and invokes the corresponding method in an instance of the class implementing the service. Any data returned by the method is converted back into a SOAP response message and is sent back through the channel stack for transmission to the client application.</span></li><li><span style="font-family:arial;">WCF assumes that nothing is exposed by default. So, if you do not specify your class with Data Contract attributes, WCF doesn’t allow serialization to the wire.</span></li></ul><p><span style="font-family:arial;">Data Contracts</span></p><ul><li><span style="font-family:arial;">Service Contracts by default, can handle simple service calls. Calls which can have simple type arguments (build-in types), and which return also simple types values. The problem comes when you want to return a custom type, or accept it as an argument for your service operation. In order to do that, the messaging world, has a concept called Data Contract. Data Contract is the normalization of your custom messaging structures. It defines the members of your operation arguments or return values. In WSDL, the Data Contract is equivalent to the type element. </span></li></ul><p><span style="font-family:arial;">References:</span></p><p><a href="http://www.arquitecturadesoftware.org/blogs/hugobatista/archive/2005/11/05/wcf-basics-service-contracts.aspx"><span style="font-family:arial;">WCF Basics - Service Contracts</span></a></p><p><a href="http://www.arquitecturadesoftware.org/blogs/hugobatista/archive/2005/11/06/wcf-basics-data-contracts.aspx"><span style="font-family:arial;">WCF Basics - Data Contracts</span></a><span style="font-family:arial;"> </span></p><p><a href="http://www.microsoft.com/learning/en/us/book.aspx?ID=10022&locale=en-us" border="0"><span style="font-family:arial;">Microsoft Windows Communication Foundation Step by Step</span></a></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-74101214758610783722010-01-07T16:44:00.006+11:002010-01-07T16:56:42.048+11:00Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.<span style="font-family:arial;">If you are using a stored procedures that use temporary tables, then be aware that temporary tables created within a stored procedure use the tempdb database’s collation instead of the current user database’s collation. Tempdb is created at installation and is assigned the default collation.<br /><br />Use 'COLLATE DATABASE_DEFAULT' to resolve the collation conflict, e.g.<br /><br />SELECT * FROM TableA<br />WHERE ID COLLATE DATABASE_DEFAULT<br />IN (SELECT TableAID FROM #temp)<br /><br />References:<br /></span><a href="http://blog.sqlauthority.com/2007/06/11/sql-server-cannot-resolve-collation-conflict-for-equal-to-operation/"><span style="font-family:arial;">SQL SERVER – Cannot resolve collation conflict for equal to operation</span></a><br /><a href="http://www.sqlmag.com/Article/ArticleID/27265/Collation_Conflict.html"><span style="font-family:arial;">Collation Conflict</span></a><span style="font-family:arial;"> </span>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-19949104812717219632010-01-07T16:32:00.003+11:002010-01-07T16:39:41.593+11:00Find the name of the server where the site is hosted on<span style="font-family:arial;">How to find out which website is assigned to which server on your network?<br /><br />1. Start a command prompt windows (start menu, click on run, type in CMD and press OK/run)<br /><br />2. Type in the following command and press enter<br /><br />Nslookup www.yoursite.com.au<br />It will return an ip – xxx.xx.xx.xxx<br /><br />3. Now – we type the same command – with the ip above<br /><br />Nslookup xxx.xx.xx.xxx<br /><br />It will return the name of the server where the site is hosted on.<br /><br />Give it a shot……… knock yourself out!</span>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-2631000592413165452009-12-15T16:42:00.005+11:002009-12-15T16:54:52.205+11:00Filter & Sort DataTable using LINQ<span style="font-family:arial;">You need to add reference to System.Data.DataSetExtensions to be able to use AsEnumerable() on a datatable.<br /><br /></span><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 \cf1 Dim\cf0 dt \cf1 As\cf0 \cf1 New\cf0 DataTable\par ??dt.Load(myDataReader)\par ??\par ??\cf4 'get name of distinct root taxonomiess \par ??\cf1 Dim\cf0 root_names \cf1 As\cf0 IEnumerable(\cf1 Of\cf0 \cf1 String\cf0 ) = \cf1 From\cf0 myRow \cf1 In\cf0 dt.AsEnumerable() _\par ?? \cf1 Select\cf0 myRow.Field(\cf1 Of\cf0 \cf1 String\cf0 )(\cf5 "root_name"\cf0 ) \cf1 Distinct\par ??\par ??\cf4 'get a list of the root taxonomy objects based on the name\par ??\cf1 Dim\cf0 list \cf1 As\cf0 \cf1 New\cf0 List(\cf1 Of\cf0 Taxon.Taxon)\par ?? \cf1 For\cf0 \cf1 Each\cf0 root_name \cf1 As\cf0 \cf1 String\cf0 \cf1 In\cf0 root_names\par ??\cf1 Dim\cf0 t \cf1 As\cf0 Taxon.Taxon = Taxon.TaxonDAL.Instance.GetOnName(root_name)\par ?? \cf1 If\cf0 t \cf1 IsNot\cf0 \cf1 Nothing\cf0 \cf1 Then\par ??\cf0 list.Add(t)\par ?? \cf1 End\cf0 \cf1 If\par ??\cf0 \cf1 Next\par ??\cf4 'sort the root taxons by Product_DisplayOrder\par ??\cf0 list.Sort(\cf1 Function\cf0 (p1, p2) p1.Product_DisplayOrder.CompareTo(p2.Product_DisplayOrder))\par ??\par ??\cf1 For\cf0 \cf1 Each\cf0 t \cf1 As\cf0 Taxon.Taxon \cf1 In\cf0 list\par ??\par ??\cf1 Dim\cf0 currentRootName \cf1 As\cf0 \cf1 String\cf0 = t.name\par ??\par ??\cf4 'get the children of the current root taxon \par ??\cf1 Dim\cf0 drRows \cf1 As\cf0 IEnumerable(\cf1 Of\cf0 DataRow) = \cf1 From\cf0 myRow \cf1 In\cf0 dt.AsEnumerable() _\par ??\cf1 Where\cf0 myRow.Field(\cf1 Of\cf0 \cf1 String\cf0 )(\cf5 "root_name"\cf0 ) = currentRootName _\par ??\cf1 Order\cf0 \cf1 By\cf0 myRow.Field(\cf1 Of\cf0 \cf1 Double\cf0 )(\cf5 "Product_DisplayOrder"\cf0 ) \cf1 Ascending\cf0 _\par ??\cf1 Select\cf0 myRow\par ??\par ?? \cf1 For\cf0 \cf1 Each\cf0 rv \cf1 As\cf0 DataRow \cf1 In\cf0 drRows\par ??\cf1 Dim\cf0 ThisTaxonId \cf1 As\cf0 \cf1 String\cf0 = rv(\cf5 "taxon_id"\cf0 )\par ??\tab \tab \cf1 Next\par ??\cf0 \cf1 Next} --><br /><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> dt <span style="color:blue;">As</span> <span style="color:blue;">New</span> DataTable</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">dt.Load(myDataReader)</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;color:green;">'get name of distinct root taxonomies </span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> root_names <span style="color:blue;">As</span> IEnumerable(<span style="color:blue;">Of</span> <span style="color:blue;">String</span>) = <span style="color:blue;">From</span> myRow <span style="color:blue;">In</span> dt.AsEnumerable() _</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Select</span> myRow.Field(<span style="color:blue;">Of</span> <span style="color:blue;">String</span>)(<span style="color:#a31515;">"root_name"</span>) <span style="color:blue;">Distinct</span></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;color:green;">'get a list of the root taxonomy objects based on the name</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> list <span style="color:blue;">As</span> <span style="color:blue;">New</span> List(<span style="color:blue;">Of</span> Taxon.Taxon)</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">For</span> <span style="color:blue;">Each</span> root_name <span style="color:blue;">As</span> <span style="color:blue;">String</span> <span style="color:blue;">In</span> root_names</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> t <span style="color:blue;">As</span> Taxon.Taxon = Taxon.TaxonDAL.Instance.GetOnName(root_name)</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> t <span style="color:blue;">IsNot</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">list.Add(t)</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;color:blue;">Next</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;color:green;">'sort the root taxons by Product_DisplayOrder</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">list.Sort(<span style="color:blue;">Function</span>(p1, p2) p1.Product_DisplayOrder.CompareTo(p2.Product_DisplayOrder))</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">For</span> <span style="color:blue;">Each</span> t <span style="color:blue;">As</span> Taxon.Taxon <span style="color:blue;">In</span> list</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> currentRootName <span style="color:blue;">As</span> <span style="color:blue;">String</span> = t.name</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;color:green;">'get the children of the current root taxon </span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> drRows <span style="color:blue;">As</span> IEnumerable(<span style="color:blue;">Of</span> DataRow) = <span style="color:blue;">From</span> myRow <span style="color:blue;">In</span> dt.AsEnumerable() _</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Where</span> myRow.Field(<span style="color:blue;">Of</span> <span style="color:blue;">String</span>)(<span style="color:#a31515;">"root_name"</span>) = currentRootName _</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Order</span> <span style="color:blue;">By</span> myRow.Field(<span style="color:blue;">Of</span> <span style="color:blue;">Double</span>)(<span style="color:#a31515;">"Product_DisplayOrder"</span>) <span style="color:blue;">Ascending</span> _</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Select</span> myRow</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">For</span> <span style="color:blue;">Each</span> rv <span style="color:blue;">As</span> DataRow <span style="color:blue;">In</span> drRows</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> ThisTaxonId <span style="color:blue;">As</span> <span style="color:blue;">String</span> = rv(<span style="color:#a31515;">"taxon_id"</span>)</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;color:blue;">Next</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Next</span><br /></span></p></div></div><br /><p><span style="font-family:arial;">Reference:</span></p><p><a class="question-hyperlink" href="http://stackoverflow.com/questions/10855/linq-query-on-a-datatable"><span style="font-family:arial;">LINQ query on a DataTable</span></a></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-47189897926815317312009-12-09T21:58:00.004+11:002009-12-09T22:04:44.547+11:00While loop in sql<span style="font-family:arial;">Here is how to do while loop in sql:<br /><br /></span><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;}??\fs20 --script \cf3 to\cf0 copy UserOrganisationId record from User table \cf3 to\cf0 User_Organisation\par ??--this script \cf3 is\cf0 meant \cf3 to\cf0 be run ONLY ONCE \cf3 for\cf0 clients who want \cf3 to\cf0 upgrade \cf3 to\cf0 6.9\par ??\par ??\cf3 Declare\cf0 @UserID \cf3 as\cf0 int\par ??\cf3 Declare\cf0 @MaxUserID \cf3 as\cf0 int\par ??\cf3 Declare\cf0 @OrgID int\par ??\par ??\cf3 select\cf0 @UserID = MIN(intuserid) from [user]\par ??\cf3 select\cf0 @MaxUserID = MAX(intuserid) from [user]\par ??\par ??\cf3 WHILE\cf0 (@UserID <= @MaxUserID)\par ??BEGIN\par ??\tab \cf3 if\cf0 exists(\cf3 Select\cf0 * from [user] where intuserid=@UserID)\par ??\tab begin \par ??\tab \tab \cf3 select\cf0 @UserID=intuserid, @OrgID=UserOrganisationId\par ??\tab \tab from [user] where intuserid = @UserID\par ??\tab \tab \cf3 if\cf0 @orgID \cf3 is\cf0 \cf3 not\cf0 null \cf3 and\cf0 @orgID> 0 \par ??\tab \tab begin\par ??\tab \tab \tab INSERT INTO User_Organisation (UserID, OrganisationID, IsDefaultOrganisation)\par ??\tab \tab \tab VALUES (@UserID, @OrgID, 1)\par ??\tab \tab \cf3 end\par ??\cf0 \tab \cf3 end\par ??\cf0 \tab \cf3 set\cf0 @UserID=@UserID+1\par ??\cf3 END\par ??\par ??\cf0 -- alternative script\par ??INSERT INTO User_Organisation (UserID, OrganisationID, IsDefaultOrganisation)\par ??\cf3 SELECT\cf0 intUserID, UserOrganisationId, 1\par ??FROM [User]\par ??WHERE (UserOrganisationId \cf3 IS\cf0 \cf3 NOT\cf0 NULL) \cf3 AND\cf0 (UserOrganisationId> 0)} --><br /><div style="BACKGROUND: white;font-family:Arial;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;">--script <span style="color:blue;">to</span> copy UserOrganisationId record from User table <span style="color:blue;">to</span> User_Organisation</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Declare</span> @UserID <span style="color:blue;">as</span> int</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Declare</span> @MaxUserID <span style="color:blue;">as</span> int</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Declare</span> @OrgID int</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">select</span> @UserID = MIN(intuserid) from [user]</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">select</span> @MaxUserID = MAX(intuserid) from [user]</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">WHILE</span> (@UserID <= @MaxUserID)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">BEGIN</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">if</span> exists(<span style="color:blue;">Select</span> * from [user] where intuserid=@UserID)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">begin </span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">select</span> @UserID=intuserid, @OrgID=UserOrganisationId</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">from [user] where intuserid = @UserID</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">if</span> @orgID <span style="color:blue;">is</span> <span style="color:blue;">not</span> null <span style="color:blue;">and</span> @orgID > 0 </span></p><p style="MARGIN: 0px"><span style="font-family:arial;">begin</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">INSERT INTO User_Organisation (UserID, OrganisationID, IsDefaultOrganisation)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">VALUES (@UserID, @OrgID, 1)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">end</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">end</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">set</span> @UserID=@UserID+1</span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:blue;">END</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">-- alternative script</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">INSERT INTO User_Organisation (UserID, OrganisationID, IsDefaultOrganisation)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">SELECT</span> intUserID, UserOrganisationId, 1</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">FROM [User]</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">WHERE (UserOrganisationId <span style="color:blue;">IS</span> <span style="color:blue;">NOT</span> NULL) <span style="color:blue;">AND</span> (UserOrganisationId > 0)</span></p></div></div>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-41059376543852890932009-12-09T21:32:00.005+11:002009-12-09T22:11:22.354+11:00Get comma seperated string<span style="font-family:arial;">This is how you can get comma seperated string:<br /><br /></span><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;}??\fs20 \par ??\cf1 Public\cf0 \cf1 Function\cf0 GetUserOrganisationIds(\cf1 ByVal\cf0 userID \cf1 As\cf0 \cf1 Integer\cf0 ) \cf1 As\cf0 \cf1 String\par ??\par ??\cf0 \cf1 Dim\cf0 dbCon \cf1 As\cf0 \cf1 New\cf0 SqlConnection(ConnectionString)\par ?? dbCon.Open()\par ?? \cf1 Dim\cf0 cmd \cf1 As\cf0 \cf1 New\cf0 SqlCommand(\cf4 "GetUserOrganisationIds"\cf0 , dbCon)\par ?? cmd.CommandType = CommandType.StoredProcedure\par ??\par ?? \cf1 Try\par ??\cf0 \cf1 Dim\cf0 dr \cf1 As\cf0 SqlDataReader = cmd.ExecuteReader\par ?? \cf1 Dim\cf0 orgList \cf1 As\cf0 \cf1 New\cf0 List(\cf1 Of\cf0 \cf1 String\cf0 )\par ?? \cf1 While\cf0 dr.Read\par ?? orgList.Add(dr(\cf4 "OrganisationID"\cf0 ).ToString)\par ?? \cf1 End\cf0 \cf1 While\par ??\cf0 \cf1 If\cf0 orgList.Count> 0 \cf1 Then\par ??\cf0 \cf1 Return\cf0 \cf1 String\cf0 .Join(\cf4 ","\cf0 , orgList.ToArray())\par ?? \cf1 Else\par ??\cf0 \cf1 Return\cf0 \cf4 ""\par ??\cf0 \cf1 End\cf0 \cf1 If\par ??\par ??\cf0 \cf1 Catch\cf0 ex \cf1 As\cf0 Exception\par ?? \cf1 Throw\cf0 (ex)\par ?? \cf1 Finally\par ??\cf0 dbCon.Close()\par ?? \cf1 End\cf0 \cf1 Try\par ??\par ??End\cf0 \cf1 Function} --><br /><div style="BACKGROUND: white;font-family:Arial;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Public</span> <span style="color:blue;">Function</span> GetUserOrganisationIds(<span style="color:blue;">ByVal</span> userID <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>) <span style="color:blue;">As</span> <span style="color:blue;">String</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> dbCon <span style="color:blue;">As</span> <span style="color:blue;">New</span> SqlConnection(ConnectionString)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">dbCon.Open()</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> cmd <span style="color:blue;">As</span> <span style="color:blue;">New</span> SqlCommand(<span style="color:#a31515;">"GetUserOrganisationIds"</span>, dbCon)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">cmd.CommandType = CommandType.StoredProcedure</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:blue;">Try</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> dr <span style="color:blue;">As</span> SqlDataReader = cmd.ExecuteReader</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> orgList <span style="color:blue;">As</span> <span style="color:blue;">New</span> List(<span style="color:blue;">Of</span> <span style="color:blue;">String</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">While</span> dr.Read</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">orgList.Add(dr(<span style="color:#a31515;">"OrganisationID"</span>).ToString)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">While</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> orgList.Count > 0 <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Return</span> <span style="color:blue;">String</span>.Join(<span style="color:#a31515;">","</span>, orgList.ToArray())</span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:blue;">Else</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Return</span> <span style="color:#a31515;">""</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Catch</span> ex <span style="color:blue;">As</span> Exception</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Throw</span> (ex)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:blue;">Finally</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">dbCon.Close()</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Try</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Function</span></span></p></div><br /></div><br /><p><span style="font-family:arial;">Here is how to do it in sql:</span></p><p><span style="font-family:arial;"></p></span><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 --\cf3 declare\cf0 holder var\par ??\cf3 DECLARE\cf0 @list VARCHAR(8000)\par ??--build comma separated list\par ??\cf3 SELECT\cf0 @list = COALESCE(@list + \cf4 ', ', '') + CAST(DZName AS VARCHAR(5) )\par ??\cf0 FROM ClubDZlocation where ClubId = 201\par ??--show results\par ??\cf3 SELECT\cf0 @list \cf3 AS\cf0 \cf4 'list'} --><br /><div style="BACKGROUND: white;font-family:Arial;font-size:10pt;color:black;" ><p style="MARGIN: 0px">--<span style="color:blue;">declare</span> holder var</p><p style="MARGIN: 0px"><span style="color:blue;">DECLARE</span> @list VARCHAR(8000)</p><p style="MARGIN: 0px">--build comma separated list</p><p style="MARGIN: 0px"><span style="color:blue;">SELECT</span> @list = COALESCE(@list + <span style="color:green;">',', '') + CAST(DZName AS VARCHAR(5) )</span></p><p style="MARGIN: 0px">FROM ClubDZlocation where ClubId = 201</p><p style="MARGIN: 0px">--show results</p><p style="MARGIN: 0px"><span style="color:blue;">SELECT</span> @list <span style="color:blue;">AS</span> <span style="color:green;">'list'</span></p></div><br /></div><p></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-26787950520623764602009-11-18T16:28:00.006+11:002009-11-18T19:26:23.213+11:00JQuery Validation<span style="font-family:arial;">Just wanna share some JQuery validation sample code which might be useful:<br /><br /></span><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red163\green21\blue21;\red0\green128\blue0;}??\fs20 //\cf3 if\cf0 you put CssClass=\cf4 "required"\cf0 \cf3 on\cf0 the dropdownlist, \par ??//\cf3 then\cf0 JQuery\cf5 's default required field validator will valiate the dropdownlist, \par ??\cf0 //but it only returns \cf3 false\cf0 \cf3 if\cf0 there \cf3 is\cf0 no items \cf3 in\cf0 the dropdownlist \par ??// -------------------Custom \cf3 function\cf0 : Required Validator \cf3 for\cf0 Drop Down List----------------------------------------------------\par ??jQuery.validator.addMethod(\cf4 "Validate_ddls"\cf0 , \cf3 function\cf0 (value, element) \{\par ??\par ?? \cf3 if\cf0 (null != element) \{\par ?? var iValue = \cf3 new\cf0 Number(element[element.selectedIndex].value);\par ?? //\cf3 if\cf0 ddlColumns \cf3 is\cf0 \cf3 not\cf0 populated, selectedIndex will be -1\par ?? \cf3 if\cf0 (iValue <= 0 iValue == NaN) \{\par ?? \cf3 return\cf0 \cf3 false\cf0 ;\par ?? \}\par ?? \cf3 else\cf0 \{\par ?? \cf3 return\cf0 \cf3 true\cf0 ;\par ?? \}\par ?? \}\par ?? \cf3 else\cf0 \{\par ?? \cf3 return\cf0 \cf3 false\cf0 ;\par ?? \}\par ??\}, jQuery.validator.messages.required);\par ??\par ??\par ??// -------------------Custom \cf3 function\cf0 : Positive_Integer_Exclude_Zero----------------------------------------------------\par ??jQuery.validator.addMethod(\cf4 "Positive_Integer_Exclude_Zero"\cf0 , \cf3 function\cf0 (value, element) \{\par ?? \cf3 return\cf0 this.optional(element) /^[1-9]\\d*$/.test(value);\par ??\}, \cf4 "Positive Integer Only"\cf0 );\par ??\par ??// -------------------Custom \cf3 function\cf0 : Positive_Integer----------------------------------------------------\par ??jQuery.validator.addMethod(\cf4 "Positive_Integer"\cf0 , \cf3 function\cf0 (value, element) \{\par ?? \cf3 return\cf0 this.optional(element) /^[0-9]\\d*$/.test(value);\par ??\}, \cf4 "Positive Integer Only"\cf0 );\par ??\par ??// -------------------Custom \cf3 function\cf0 : Positive_Decimal_Exclude_Zero----------------------------------------------------\par ??jQuery.validator.addMethod(\cf4 "Positive_Decimal_Exclude_Zero"\cf0 , \cf3 function\cf0 (value, element) \{\par ??\cf3 return\cf0 this.optional(element) /(^\\d*\\.?\\d*[1-9]+\\d*$)(^[1-9]+\\d*\\.\\d*$)/.test(value);\par ??\}, \cf4 "Invalid Amount"\cf0 );\par ??\par ??// -------------------Custom \cf3 function\cf0 : Positive_Decimal----------------------------------------------------\par ??jQuery.validator.addMethod(\cf4 "Positive_Decimal"\cf0 , \cf3 function\cf0 (value, element) \{\par ?? \cf3 return\cf0 this.optional(element) /(^\\d*\\.?\\d*[0-9]+\\d*$)(^[0-9]+\\d*\\.\\d*$)/.test(value);\par ??\}, \cf4 "Invalid Amount"\cf0 );\par ??\par ??// -------------------Custom \cf3 function\cf0 : Validate_Email----------------------------------------------------\par ??jQuery.validator.addMethod(\cf4 "Validate_Email"\cf0 , \cf3 function\cf0 (value, element) \{\par ?? \cf3 return\cf0 this.optional(element) /^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]\{2,4\}$/.test(value);\par ??\}, \cf4 "Invalid Email address"\cf0 );} --><br /><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><br /><p style="MARGIN: 0px"><span style="font-family:arial;">//<span style="color:blue;">if</span> you put CssClass=<span style="color:#a31515;">"required"</span> <span style="color:blue;">on</span> the dropdownlist, </span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">//<span style="color:blue;">then</span> JQuery<span style="color:green;">'s default required field validator will valiate the dropdownlist, </span></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">//but it only returns <span style="color:blue;">false</span> <span style="color:blue;">if</span> there <span style="color:blue;">is</span> no items <span style="color:blue;">in</span> the dropdownlist </span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">// -------------------Custom <span style="color:blue;">function</span>: Required Validator <span style="color:blue;">for</span> Drop Down List----------------------------------------------------</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">jQuery.validator.addMethod(<span style="color:#a31515;">"Validate_ddls"</span>, <span style="color:blue;">function</span>(value, element) {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">if</span> (null != element) {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">var iValue = <span style="color:blue;">new</span> Number(element[element.selectedIndex].value);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">//<span style="color:blue;">if</span> ddlColumns <span style="color:blue;">is</span> <span style="color:blue;">not</span> populated, selectedIndex will be -1</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">if</span> (iValue <= 0 iValue == NaN) {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">return</span> <span style="color:blue;">false</span>;</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">else</span> {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">return</span> <span style="color:blue;">true</span>;</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">else</span> {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">return</span> <span style="color:blue;">false</span>;</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}, jQuery.validator.messages.required);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">// -------------------Custom <span style="color:blue;">function</span>: Positive_Integer_Exclude_Zero----------------------------------------------------</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">jQuery.validator.addMethod(<span style="color:#a31515;">"Positive_Integer_Exclude_Zero"</span>, <span style="color:blue;">function</span>(value, element) {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">return</span> this.optional(element) /^[1-9]\d*$/.test(value);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}, <span style="color:#a31515;">"Positive Integer Only"</span>);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">// -------------------Custom <span style="color:blue;">function</span>: Positive_Integer----------------------------------------------------</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">jQuery.validator.addMethod(<span style="color:#a31515;">"Positive_Integer"</span>, <span style="color:blue;">function</span>(value, element) {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">return</span> this.optional(element) /^[0-9]\d*$/.test(value);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}, <span style="color:#a31515;">"Positive Integer Only"</span>);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">// -------------------Custom <span style="color:blue;">function</span>: Positive_Decimal_Exclude_Zero----------------------------------------------------</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">jQuery.validator.addMethod(<span style="color:#a31515;">"Positive_Decimal_Exclude_Zero"</span>, <span style="color:blue;">function</span>(value, element) {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">return</span> this.optional(element) /(^\d*\.?\d*[1-9]+\d*$)(^[1-9]+\d*\.\d*$)/.test(value);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}, <span style="color:#a31515;">"Invalid Amount"</span>);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">// -------------------Custom <span style="color:blue;">function</span>: Positive_Decimal----------------------------------------------------</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">jQuery.validator.addMethod(<span style="color:#a31515;">"Positive_Decimal"</span>, <span style="color:blue;">function</span>(value, element) {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">return</span> this.optional(element) /(^\d*\.?\d*[0-9]+\d*$)(^[0-9]+\d*\.\d*$)/.test(value);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}, <span style="color:#a31515;">"Invalid Amount"</span>);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">// -------------------Custom <span style="color:blue;">function</span>: Validate_Email----------------------------------------------------</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">jQuery.validator.addMethod(<span style="color:#a31515;">"Validate_Email"</span>, <span style="color:blue;">function</span>(value, element) {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">return</span> this.optional(element) /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(value);</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}, <span style="color:#a31515;">"Invalid Email address"</span>);</span></p><br /></div><br /><br /></div><br /><p><span style="font-family:arial;"></span></p><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 // -------------------Validate the whole Form----------------------------------------------------\par ?? $(document).ready(\cf3 function\cf0 () \{\par ?? $(\cf4 "#aspnetForm"\cf0 ).validate(\{\par ?? rules: \{\par ?? <%=txtTag.UniqueID %>:\{\par ?? required: \cf3 true\par ??\cf0 \},\par ?? <%=ddlTables.UniqueID %>:\{\par ?? Validate_ddls: \cf3 true\par ??\cf0 \},\par ?? <%=ddlColumns.UniqueID %>:\{\par ?? Validate_ddls: \cf3 true\par ??\cf0 \}\par ?? \},\par ?? messages: \{\par ?? <%=txtTag.UniqueID %>:\{ \par ?? required: \cf4 "Tag is required"\par ??\cf0 \},\par ?? <%=ddlColumns.UniqueID %>:\{ \par ?? required: \cf4 "Field is required"\par ??\cf0 \}\par ?? \}\par ?? \});\par ?? \});\par ??} --><br /><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><br /><p style="MARGIN: 0px"><span style="font-family:arial;">// -------------------Validate the whole Form----------------------------------------------------</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">$(document).ready(<span style="color:blue;">function</span>() {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">$(<span style="color:#a31515;">"#aspnetForm"</span>).validate({</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">rules: {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><%=txtTag.UniqueID %>:{</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">required: <span style="color:blue;">true</span></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">},</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><%=ddlTables.UniqueID %>:{</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">Validate_ddls: <span style="color:blue;">true</span></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">},</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><%=ddlColumns.UniqueID %>:{</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">Validate_ddls: <span style="color:blue;">true</span></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">},</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">messages: {</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><%=txtTag.UniqueID %>:{ </span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">required: <span style="color:#a31515;">"Tag is required"</span></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">},</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><%=ddlColumns.UniqueID %>:{ </span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">required: <span style="color:#a31515;">"Field is required"</span></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">});</span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;">});</span></p><br /></div><br /><br /></div><p><span style="font-family:arial;"></span></p><p><span style="font-family:arial;"></span></p><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red163\green21\blue21;\red100\green100\blue185;\red132\green70\blue70;}??\fs20 <script type="\cf3" src="\cf3">\cf4 </\cf5 script\cf4 >\par ??\cf0 <script type="\cf3" src="\cf3">\cf4 </\cf5 script\cf4 >} --><br /><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><script type=<span style="color:#a31515;">"text/javascript"</span> src=<span style="color:#a31515;">"../include/APF_js/jquery-1.3.2.js"</span>><span style="color:#6464b9;"></</span><span style="color:#844646;">script</span><span style="color:#6464b9;">></span></span></p><br /><p style="MARGIN: 0px"><span style="font-family:arial;"><script type=<span style="color:#a31515;">"text/javascript"</span> src=<span style="color:#a31515;">"../include/APF_js/jquery.validate.js"</span>><span style="color:#6464b9;"></</span><span style="color:#844646;">script</span><span style="color:#6464b9;">></span></span></p><br /></div><br /><br /></div><p><span style="font-family:arial;"></span></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-83655786005390786282009-09-28T14:42:00.006+10:002009-09-28T15:06:18.257+10:00DesignMode - fix body tag as literal control<span style="font-family:arial;">Sometimes, you might want to put html body tag in a literal control. Then you lost intellisense from Visual Studio. You can use DesignMode property to fix it:<br /><br /></span><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><% <span style="color:blue;">If</span> DesignMode Then%><body></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><%</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">Else%></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><asp:Literal ID=<span style="color:#a31515;">"litBodyTag"</span> runat=<span style="color:#a31515;">"server"</span> /></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><% <span style="color:blue;">End</span> If%></span></p></div></div>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-30714693327885076392009-08-24T16:40:00.005+10:002009-09-11T16:41:26.306+10:00Load balancing issues for ASP.NET applications<span style="font-family:arial;">Rencently, our company started to change our hosting to adapt cluster servers and Mirrored SQL Servers. We just used DFS replication, we did not use a load balancer server.<br /><br />There are a couple of issues we need to address because of the change:<br /></span><br /><ul><li><span style="font-family:arial;">when put a object in session, make sure it is Serializable, so later in the clustered environment, if you manage session in sql server, it can be stored in sql server</span></li><li><span style="font-family:arial;">We were using a third party control for uploading images called I-Load which created some issues with temp image folder, luckily we fixed it. So when buying third-party control like ILoad, make sure it supports web farm</span></li><li><span style="font-family:arial;">We were using a third party control called DynamicControlsPlaceholder which does not work in cluster environment until we modified it a bit to make it work</span></li><li><span style="font-family:arial;">view state needs to have shared machinekey set up in web.config for web farm environment (in iis7 use sharekey section to share key with other web servers in the farm, then use iis7 to generate unique machinekey for every application in the iis, so that applications do not interrupt with each other)</span></li><li><span style="font-family:arial;">Cache & Application variable</span></li><ul><li><span style="font-family:arial;">The cached object is stored in process, which means it doesn’t persist if the application domain is restarted and it can’t be shared between computers in a web farm.</span></li><li><span style="font-family:arial;">Once a worker process recycles, the entire Cache is lost because it was within this process.</span></li><li><span style="font-family:arial;">Microsoft has tried to fix this a bit with SQL Server cache invalidation (ASP.NET Cache SqlCacheDependency), but it could have potential performance issue and according to </span><a href="http://simpable.com/about/"><span style="font-family:arial;">Scott Watermasysk</span></a><span style="font-family:arial;"> he has not seen many be successful with this approach.</span></li><li><span style="font-family:arial;">There are a couple third party tools to fix this: </span><a href="http://www.danga.com/memcached//omemcached:%20a%20distributed%20memory%20object%20caching%20system"><span style="font-family:arial;">Memcached</span></a><span style="font-family:arial;"> </span><a href="http://www.scaleoutsoftware.com//oScaleOut%20Software%20-%20Distributed%20Caching%20for%20Server%20Farms"><span style="font-family:arial;">ScaleOut</span></a><span style="font-family:arial;"> </span><a href="http://www.alachisoft.com/ncache//oDistributed%20Caching,%20ASP.NET%20Session%20State,%20NHibernate%20Caching,%20and%20Caching%20Application%20Blocking"><span style="font-family:arial;">NCache</span></a><span style="font-family:arial;"> </span><a href="http://www.sharedcache.com/cms/homepage.aspx/oindeXus.Net%20Shared%20Cache%20-%20distributed%20caching%20and%20replicated%20caching"><span style="font-family:arial;">SharedCache</span></a></li><li><span style="font-family:arial;">The good news is Microsoft is making some strides here. They are working on an out of processing caching component called </span><a href="http://simpable.com/code/velocity/"><span style="font-family:arial;">Velocity</span></a><span style="font-family:arial;">.</span></li><li><span style="font-family:arial;">We have to get rid of cache and application variables to address the problem</span></li></ul></ul><p><span style="font-family:arial;">References:</span></p><p><span style="font-family:arial;"><a href="http://simpable.com/software/asp-net-caching-is-too-easy/">ASP.Net Caching Is Too Easy</a></span></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-83320747808004281502009-08-14T16:08:00.004+10:002009-08-14T16:20:43.085+10:00Delete files older than certain period<div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Public</span> <span style="color:blue;">Shared</span> <span style="color:blue;">Sub</span> ClearTemporaryFiles(<span style="color:blue;">ByVal</span> folderPath <span style="color:blue;">As</span> <span style="color:blue;">String</span>, <span style="color:blue;">ByVal</span> maxLifeTime <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> timeLimit <span style="color:blue;">As</span> DateTime = DateTime.Now.AddMinutes(0 - maxLifeTime)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> filePaths() <span style="color:blue;">As</span> <span style="color:blue;">String</span> = System.IO.Directory.GetFiles(folderPath, <span style="color:#a31515;">"*.jpg"</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">For</span> i <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = 0 <span style="color:blue;">To</span> filePaths.Length - 1</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> filePath <span style="color:blue;">As</span> <span style="color:blue;">String</span> = filePaths(i)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> (System.IO.File.GetLastWriteTime(filePath) < timeLimit) <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Try</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">System.IO.File.Delete(filePath)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Catch</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Try</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Next</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p></div></div>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-20506837748833969182009-07-24T16:35:00.004+10:002009-07-24T16:57:53.912+10:00Handle file upload timeout<span style="font-family:arial;">Put the following in the Global.asax.vb, it will check the MaxRequestLength and send you to the error page before uploading the file.<br /><br /></span><div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Sub</span> Application_BeginRequest(<span style="color:blue;">ByVal</span> sender <span style="color:blue;">As</span> <span style="color:blue;">Object</span>, <span style="color:blue;">ByVal</span> e <span style="color:blue;">As</span> EventArgs)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:green;">' Fires at the beginning of each request, session not available here</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:green;">'check for file upload size, </span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:green;">'MaxRequestLength: Int32, the maximum request size in kilobytes. The default size is 4096 KB (4 MB).</span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:green;">'ContentLength: Int64, the number of bytes of data to send to the Internet resource. </span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> ConfigurationManager.GetSection(<span style="color:#a31515;">"system.web/httpRuntime"</span>).MaxRequestLength <span style="color:blue;">IsNot</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> maxSize <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = <span style="color:blue;">CInt</span>(ConfigurationManager.GetSection(<span style="color:#a31515;">"system.web/httpRuntime"</span>).MaxRequestLength)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">maxSize = maxSize * 1024</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> Request.ContentLength > maxSize <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:green;">'do not redirect to itself, it will create an infinite loop</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">Response.Redirect(<span style="color:#a31515;">"~/ErrorPage.aspx?ErrMsg=You have reached the maximum upload file limit, please try again!"</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p></div></div><br /><p><span style="font-family:arial;">Reference:<br /></span><a href="http://www.vikramlakhotia.com/Dealing_With_the_ASPNet_Upload_File_Size_Problem.aspx"><span style="font-family:arial;">Dealing With the ASP.Net Upload File Size Problem</span></a></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-48630624887639732172009-06-24T21:16:00.003+10:002009-06-24T21:25:34.989+10:00Javascript: Clear Form Elements<div style="BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; HEIGHT: 200px; OVERFLOW: scroll; BORDER-TOP: 1px solid; BORDER-RIGHT: 1px solid"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">function</span> ClearValue() {</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">var myContainer = document.getElementById(<span style="color:#a31515;">"myFormElement"</span>);</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">var options = myContainer.getElementsByTagName(<span style="color:green;">'input');</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">for</span> (i = 0; i < options.length; i++) {</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">var opt = options[i];</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">if</span> (opt.type == <span style="color:green;">'text') {</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">opt.value = <span style="color:#a31515;">""</span>;</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">else</span> <span style="color:blue;">if</span> (opt.type == <span style="color:green;">'checkbox' opt.type == 'radio') {</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">if</span> (opt.checked) {</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">opt.checked = <span style="color:blue;">false</span>;</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">var options = myContainer.getElementsByTagName(<span style="color:green;">'textarea');</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">for</span> (i = 0; i < options.length; i++) {</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">var opt = options[i];</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">opt.value = <span style="color:#a31515;">""</span>;</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">var options = myContainer.getElementsByTagName(<span style="color:green;">'select');</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">for</span> (i = 0; i < options.length; i++) {</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">var opt = options[i];</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">opt.selectedIndex = -1;</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">}</span></p></div></div><p><span style="font-family:arial;">Reference:<br /></span><a href="http://www.javascript-coder.com/javascript-form/javascript-reset-form.htm"><span style="font-family:arial;">Using JavaScript to reset or clear a form</span></a></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-2981487828062811822009-04-21T20:33:00.003+10:002009-04-21T20:39:13.786+10:00sort table randomly<span style="font-family:arial;">The following sample shows you on how to sort sql table randomly:<br /><br /></span><div style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; OVERFLOW: scroll; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; HEIGHT: 200px"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">select</span> * from(</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">select</span> top 20 firstname, lastname <span style="color:blue;">from</span> [user]</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">) <span style="color:blue;">as</span> result order by NEWID()</span></p></div></div>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-8609727729233987732009-04-21T20:18:00.004+10:002009-04-21T20:29:00.339+10:00identity_insert<span style="font-family:arial;">The following example shows you on how to insert explicit value into the identity column of a table:</span><span style="font-family:arial;"><br /><br /></span><span style="font-family:arial;"></span><div style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; OVERFLOW: scroll; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; HEIGHT: 200px"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">set</span> identity_insert [MyModuleElement] <span style="color:blue;">on</span> </span></p><p style="MARGIN: 0px"><span style="font-family:arial;">Insert into MyModuleElement</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">(intID, strName, strDescription, intControllerID)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">Values</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">(48, <span style="color:green;">'test name', 'desc', 46)</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">set</span> identity_insert [MyModuleElement] off</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p></div></div>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-24692733593050380142009-04-21T20:09:00.001+10:002009-04-21T20:12:52.055+10:00Get the current page name in javascript<div style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; OVERFLOW: scroll; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; HEIGHT: 200px"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><script type=<span style="color:#a31515;">"text/javascript"</span>></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> var sPath = window.location.pathname;</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> var sPage = sPath.substring(sPath.lastIndexOf(<span style="color:green;">'/') + 1);</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> alert(sPage);</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:#6464b9;"></</span><span style="color:#844646;">script</span><span style="color:#6464b9;">></span></span></p></div></div><p><span style="font-family:arial;">Reference:</span></p><p><a href="http://www.rgagnon.com/jsdetails/js-0085.html"><span style="font-family:arial;">Get the current page name</span></a></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-68069444615566622662009-04-21T19:45:00.003+10:002009-04-21T20:07:00.484+10:00Javascript refresh page<span style="font-family:arial;">If you want to use javascript to refresh the page, you can use<br /></span><ul><li><span style="font-family:arial;">location.reload(); => post request, same as click the refresh button on the browser</span></li><li><span style="font-family:arial;">window.location=window.location; => get request</span></li><li><span style="font-family:arial;">self.location=self.location; => get request</span></li></ul><span style="font-family:arial;">If you want to use javascript to redirect to another page, you can use<br /></span><ul><li><span style="font-family:arial;">location.replace('webform1.aspx'); => get request, does not create a history entry so browser back button disabled if no history before</span></li><li><span style="font-family:arial;">location.href='webform1.aspx'; => get request, create a history entry so browser back button works</span></li><li><span style="font-family:arial;">window.location='webform1.aspx'; => get request, create a history entry so browser back button works</span></li></ul><p><span style="font-family:arial;">IE buttons</span></p><ul><li><span style="font-family:arial;">Back button => get request</span></li><li><span style="font-family:arial;">Forward button => post request</span></li><li><span style="font-family:arial;">Refresh button => post request</span></li></ul><p><span style="font-family:arial;">If you get an IE error saying 'To display the webpage again, Internet Explorer needs to resend the information you've previously submitted', you can use window.location=window.location; to refresh the page instead of location.reload(); </span></p><p><span style="font-family:arial;">Sample code:</span><br /><div style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; OVERFLOW: scroll; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; HEIGHT: 200px"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><%@ Page Language=<span style="color:#a31515;">"vb"</span> AutoEventWireup=<span style="color:#a31515;">"false"</span> CodeBehind=<span style="color:#a31515;">"Default.aspx.vb"</span> <span style="color:blue;">Inherits</span>=<span style="color:#a31515;">"PostMethod_Test._Default"</span> %></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"><!DOCTYPE html <span style="color:blue;">PUBLIC</span> <span style="color:#a31515;">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span style="color:#a31515;">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span>></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><html xmlns=<span style="color:#a31515;">"http://www.w3.org/1999/xhtml"</span>></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><head runat=<span style="color:#a31515;">"server"</span>></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <title><span style="color:#6464b9;"></</span><span style="color:#844646;">title</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:#6464b9;"></</span><span style="color:#844646;">head</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><body></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <form id=<span style="color:#a31515;">"form1"</span> runat=<span style="color:#a31515;">"server"</span>></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <asp:Literal ID=<span style="color:#a31515;">"litJavascript"</span> runat=<span style="color:#a31515;">"server"</span>><span style="color:#6464b9;"></</span><span style="color:#844646;">asp:Literal</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> Post:</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <asp:TextBox ID=<span style="color:#a31515;">"txt"</span> runat=<span style="color:#a31515;">"server"</span>><span style="color:#6464b9;"></</span><span style="color:#844646;">asp:TextBox</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> Post Method:<asp:Button ID=<span style="color:#a31515;">"btnPost"</span> runat=<span style="color:#a31515;">"server"</span> Text=<span style="color:#a31515;">"PostBack"</span> /></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <hr /></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> Redirect:</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Get</span> Method:<asp:Button ID=<span style="color:#a31515;">"btnRedirect"</span> runat=<span style="color:#a31515;">"server"</span> Text=<span style="color:#a31515;">"ResponseRedirect"</span> /></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Get</span> Method:<a href=<span style="color:#a31515;">"javascript:location.replace('webform1.aspx');"</span>>JS Location Replace<span style="color:#6464b9;"></</span><span style="color:#844646;">a</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> (does <span style="color:blue;">not</span> create a history entry so browser back button disabled <span style="color:blue;">if</span> no history before)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Get</span> Method:<a href=<span style="color:#a31515;">"javascript:location.href='webform1.aspx';"</span>>JS Location Href<span style="color:#6464b9;"></</span><span style="color:#844646;">a</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> (create a history entry so browser back button works)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Get</span> Method:<a href=<span style="color:#a31515;">"javascript:window.location='webform1.aspx';"</span>>JS Window Location<span style="color:#6464b9;"></</span><span style="color:#844646;">a</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> (create a history entry so browser back button works)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Get</span> Method: IE Back button</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> Post Method: IE Forward button</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <hr /></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> Refresh:</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Get</span>/Post Method:<a href=<span style="color:#a31515;">"javascript:location.reload();"</span>>JS Location Reload<span style="color:#6464b9;"></</span><span style="color:#844646;">a</span><span style="color:#6464b9;">></span> (<span style="color:blue;">if</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> there <span style="color:blue;">is</span> any control <span style="color:blue;">in</span> the form <span style="color:blue;">then</span> after the form was submitted <span style="color:blue;">for</span> the first</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> time, <span style="color:blue;">if</span> you click this, it will be a post method coming <span style="color:blue;">with</span> the IE alert)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Get</span> Method:<a href=<span style="color:#a31515;">"javascript:window.location=window.location;"</span>>JS Window Location<span style="color:#6464b9;"></</span><span style="color:#844646;">a</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> (does <span style="color:blue;">not</span> create a history entry) (<span style="color:blue;">if</span> there <span style="color:blue;">is</span> any control <span style="color:blue;">in</span> the form <span style="color:blue;">then</span> after</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> the form was submitted <span style="color:blue;">for</span> the first time, <span style="color:blue;">if</span> you click this, it will still be a</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">get</span> method which means the form will <span style="color:blue;">not</span> be submitted again)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Get</span> Method:<a href=<span style="color:#a31515;">"javascript:self.location=self.location;"</span>>JS Self Location<span style="color:#6464b9;"></</span><span style="color:#844646;">a</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> (Same <span style="color:blue;">as</span> above)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Get</span>/Post Method: IE Refresh button - same <span style="color:blue;">as</span> location.reload()</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <hr /></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> Open <span style="color:blue;">New</span> Window:</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> No Method:<a href=<span style="color:#a31515;">"javascript:var a = window.open('webform1.aspx');"</span>>JS Window Open<span style="color:#6464b9;"></</span><span style="color:#844646;">a</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> (just open)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <div></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> Post Method <span style="color:blue;">for</span> parent page:<asp:Button ID=<span style="color:#a31515;">"btnOpen"</span> Text=<span style="color:#a31515;">"Open Window"</span> runat=<span style="color:#a31515;">"server"</span> /></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">div</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:#6464b9;"></</span><span style="color:#844646;">form</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:#6464b9;"></</span><span style="color:#844646;">body</span><span style="color:#6464b9;">></span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:#6464b9;"></</span><span style="color:#844646;">html</span><span style="color:#6464b9;">></span></span></p></div></div><br /><div style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; OVERFLOW: scroll; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; HEIGHT: 200px"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Partial</span> <span style="color:blue;">Public</span> <span style="color:blue;">Class</span> _Default</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Inherits</span> System.Web.UI.Page</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Protected</span> <span style="color:blue;">Sub</span> Page_Load(<span style="color:blue;">ByVal</span> sender <span style="color:blue;">As</span> <span style="color:blue;">Object</span>, <span style="color:blue;">ByVal</span> e <span style="color:blue;">As</span> System.EventArgs) <span style="color:blue;">Handles</span> <span style="color:blue;">Me</span>.Load</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> btnRedirect_Click(<span style="color:blue;">ByVal</span> sender <span style="color:blue;">As</span> <span style="color:blue;">Object</span>, <span style="color:blue;">ByVal</span> e <span style="color:blue;">As</span> System.EventArgs) <span style="color:blue;">Handles</span> btnRedirect.Click</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> Response.Redirect(<span style="color:#a31515;">"Default.aspx"</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> btnPost_Click(<span style="color:blue;">ByVal</span> sender <span style="color:blue;">As</span> <span style="color:blue;">Object</span>, <span style="color:blue;">ByVal</span> e <span style="color:blue;">As</span> System.EventArgs) <span style="color:blue;">Handles</span> btnPost.Click</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> btnOpen_Click(<span style="color:blue;">ByVal</span> sender <span style="color:blue;">As</span> <span style="color:blue;">Object</span>, <span style="color:blue;">ByVal</span> e <span style="color:blue;">As</span> System.EventArgs) <span style="color:blue;">Handles</span> btnOpen.Click</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Dim</span> mstrMessage <span style="color:blue;">As</span> <span style="color:blue;">New</span> StringBuilder(<span style="color:#a31515;">""</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> mstrMessage.AppendFormat(<span style="color:#a31515;">"<script type=""text/javascript"" >{0}"</span>, ControlChars.CrLf)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> mstrMessage.AppendFormat(<span style="color:#a31515;">" window.open(""webform1.aspx"");{0}"</span>, ControlChars.CrLf)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> mstrMessage.AppendFormat(<span style="color:#a31515;">" </script>"</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> litJavascript.Text = mstrMessage.ToString</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Class</span></span></p></div></div><p></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com1tag:blogger.com,1999:blog-3589812793240917524.post-46383381515093686202009-04-17T23:44:00.003+10:002009-04-17T23:51:10.843+10:00Reasons to Use Proxy Server<p><span style="font-family:arial;">Increased internet security since:</span></p><ul><li><span style="font-family:arial;">Users are authenticated</span></li><li><span style="font-family:arial;">Client requests are filtered</span></li><li><span style="font-family:arial;">All content, inbound and outbound, is validated</span></li><li><span style="font-family:arial;">All access is logged</span></li><li><span style="font-family:arial;">All of the internal network details are hidden</span></li><li><span style="font-family:Arial;">Internet access performance is improved</span></li></ul>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-29285115858354626512009-04-12T11:16:00.003+10:002009-04-15T09:36:46.547+10:00Create a screenshot image of a web page<span style="font-family:arial;">I use HtmlCapture to capture a screenshot of a web page:<br /><br /></span><div style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; OVERFLOW: scroll; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; HEIGHT: 200px"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Imports</span> HtmlCapture</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Module</span> Module1</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Sub</span> Main()</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">CreatePreviewImage()</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> CreatePreviewImage()</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> theURL <span style="color:blue;">As</span> <span style="color:blue;">String</span> = "<span style="color:#a31515;"><a href="http://www.google.com.au/">http://www.google.com.au/</a>"</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> snap <span style="color:blue;">As</span> <span style="color:blue;">New</span> SnapShooter</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> snapResult <span style="color:blue;">As</span> HtmlCaptureResult</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">snap.SetRegInfo(<span style="color:#a31515;">"username"</span>, <span style="color:#a31515;">"licencekey"</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">snap.SetDelayTime(6000)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">snap.SetTimeOut(15000)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">snap.SetMinBrowserSize(SnapDimensions.BrowserWidth, SnapDimensions.BrowserHeight)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">snap.SetClipRect(0, 0, SnapDimensions.ClipWidth, SnapDimensions.ClipHeight)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">snap.SetThumbSize(SnapDimensions.ThumbWidth, SnapDimensions.ThumbHeight, <span style="color:blue;">True</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">snap.SetJpegQuality(<span style="color:blue;">CByte</span>(SnapDimensions.JpegQuality))</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">snapResult = snap.SnapUrl(theURL)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> snapResult = HtmlCaptureResult.HCR_SUCCESS <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:green;">' success, save the image as a JPEG </span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">snap.SaveImage(<span style="color:#a31515;">"screenshot.jpg"</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Else</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:green;">' timeout, so throw error</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> ex <span style="color:blue;">As</span> <span style="color:blue;">New</span> Exception(<span style="color:#a31515;">"Timeout on capturing."</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Throw</span> ex</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Public</span> <span style="color:blue;">Enum</span> SnapDimensions <span style="color:blue;">As</span> <span style="color:blue;">Integer</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">ThumbWidth = 200</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">ThumbHeight = 150</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">BrowserWidth = 1024</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">BrowserHeight = 768</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">ClipWidth = 1024</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">ClipHeight = 768</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">JpegQuality = 70</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Enum</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Module</span></span></p></div></div><br /><p>Reference:</p><p><a href="http://www.polestarsoft.com/support.php">HtmlCapture Support</a></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-27662438259253881322009-04-12T10:57:00.003+10:002009-04-12T11:07:42.002+10:00Clear form values<span style="font-family:arial;">The following code will clear values entered in the form, currently only works with drop down list and textbox, you can expand it to cater for more controls.<br /><br /></span><div style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; OVERFLOW: scroll; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; HEIGHT: 200px"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> btnClear_Click(<span style="color:blue;">ByVal</span> sender <span style="color:blue;">As</span> <span style="color:blue;">Object</span>, <span style="color:blue;">ByVal</span> e <span style="color:blue;">As</span> System.EventArgs) <span style="color:blue;">Handles</span> btnClear.Click</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">ClearValues(<span style="color:blue;">Me</span>.FindControl(<span style="color:#a31515;">"MyControlID"</span>))</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> ClearValues(<span style="color:blue;">ByVal</span> container <span style="color:blue;">As</span> Control)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">For</span> <span style="color:blue;">Each</span> ctl <span style="color:blue;">As</span> Control <span style="color:blue;">In</span> container.Controls</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> textBox <span style="color:blue;">As</span> TextBox = <span style="color:blue;">TryCast</span>(ctl, TextBox)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> textBox <span style="color:blue;">IsNot</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> textBox.Text.Trim().Length <> 0 <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">textBox.Text = <span style="color:#a31515;">""</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> <span style="color:blue;">TypeOf</span> ctl <span style="color:blue;">Is</span> UI.WebControls.DropDownList <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> ddl <span style="color:blue;">As</span> UI.WebControls.DropDownList = <span style="color:blue;">DirectCast</span>(ctl, UI.WebControls.DropDownList)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Dim</span> li <span style="color:blue;">As</span> ListItem = ddl.Items.FindByText(<span style="color:#a31515;">"(none)"</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> li <span style="color:blue;">IsNot</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">ddl.SelectedValue = li.Value</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">li = ddl.Items.FindByText(<span style="color:#a31515;">"Select"</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> li <span style="color:blue;">IsNot</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">ddl.SelectedValue = li.Value</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> ctl.Controls.Count > 0 <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">ClearValues(ctl)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Next</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p></div></div>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-72812333844673556412009-04-12T10:48:00.002+10:002009-04-12T10:53:16.776+10:00Save a CSV File in Browser<div style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; OVERFLOW: scroll; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; HEIGHT: 200px"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> lbCSVTemplate_Click(<span style="color:blue;">ByVal</span> sender <span style="color:blue;">As</span> <span style="color:blue;">Object</span>, <span style="color:blue;">ByVal</span> e <span style="color:blue;">As</span> System.EventArgs) <span style="color:blue;">Handles</span> lbCSVTemplate.Click</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">If</span> File.Exists(Server.MapPath(<span style="color:#a31515;">"~/CSVUploaded/template.csv"</span>)) <span style="color:blue;">Then</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">Response.ContentType = <span style="color:#a31515;">"text/csv"</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:green;">'Some browsers open the file directly in browser, so this will cause a file "Save as" dialogue to appear.</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">Response.AppendHeader(<span style="color:#a31515;">"Content-Disposition"</span>, <span style="color:#a31515;">"attachment; filename=template.csv"</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">Response.TransmitFile(Server.MapPath(<span style="color:#a31515;">"~/CSVUploaded/template.csv"</span>))</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">Response.End()</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Else</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;">lblMsg.Text = MessagesDAL.Instance.Get_MessageByID(Messages.MessageCodes.CSVTemplateNotAvailable)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">lblMsg.ForeColor = Drawing.Color.Red</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">If</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p></div></div><p><span style="font-family:arial;">Reference:<br /></span><a href="http://www.west-wind.com/Weblog/posts/76293.aspx"><span style="font-family:arial;">Downloading a File with a Save As Dialog in ASP.NET</span></a></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-6517594952630199042009-04-10T17:14:00.007+10:002009-04-10T18:26:18.209+10:00robocopy<ul><li><span style="font-family:arial;">robocopy, or "Robust File Copy", is a command-line directory replication command. </span></li><li><span style="font-family:arial;">It was available as part of the Windows Resource Kit, and introduced as a standard feature of Windows Vista and Windows Server 2008.</span></li><li><span style="font-family:arial;">Robocopy is notable for capabilities above and beyond the built-in Windows copy and xcopy commands.</span></li><li><span style="font-family:Arial;">You can script the commands and save them as .cmd files to deploy asp.net files to staging site or live site.</span></li></ul><p><span style="font-family:arial;">Command Examples:</span></p><ul><li><span style="font-family:arial;">Copy test.dll from bin folder under folder1 to bin folder under folder2, if test.dll has already existed in folder2, then it will be overwritten. It will not delete any files in folder2 which do not extist in folder1.</span></li><ul><li><span style="font-family:arial;color:#000099;"><strong>robocopy C:\folder1\bin C:\folder2\bin test.dll</strong></span><span style="font-family:arial;"></li></ul><li><span style="color:#000000;">Copy content from test to test, including subfolders, excludes folders named .svn, excludes files with .vb extension. If test folder does not exist in folder2, it will create one for you. </li><ul><li><strong><span style="color:#000099;">robocopy C:\folder1\test C:\folder2\test /S /XD .svn /XF *.vb</span></strong><span style="color:#000099;"><span style="color:#000000;"></li></ul><li><span style="color:#000000;">Create test folder only <ul><li><strong><span style="color:#000099;">robocopy C:\folder1\test C:\folder2\test /xf *.*</span></strong></li></ul></span></li></span></span><span style="color:#000099;"></ul></span><p><span style="color:#000000;">Reference:</span></p><p><span style="color:#000000;"><a href="http://en.wikipedia.org/wiki/Robocopy">robocopy</a></span></span></span></li></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0tag:blogger.com,1999:blog-3589812793240917524.post-81292356988933449832009-04-10T12:25:00.002+10:002009-04-10T12:36:16.676+10:00Disable Table Constraints<span style="font-family:arial;">The following code will:<br /><br /></span><ul><li><span style="font-family:arial;">Disable/Enable All Table Constraints</span></li><li><span style="font-family:arial;">E.g. if you have an user table and an UserLog table, UserLog have a foreign key which reference the UserID in the user table.</span></li><li><span style="font-family:arial;">Now you can disable the foreign key Constraint on the UserLog table and insert a userid which does not exist in the user table into the UserLog table. After that you can enable the constraint.</span></li></ul><p><span style="font-family:arial;"><br /></span></p><p><span style="font-family:arial;"></span></p><div style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; OVERFLOW: scroll; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; HEIGHT: 200px"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;">CREATE PROCEDURE [dbo].[procDisableEnableAllTableConstraints]</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">@TblName VARCHAR(128),</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">@IsCheck BIT = 1</span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:blue;">AS</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">DECLARE</span> @SQLState VARCHAR(500)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">IF</span> @IsCheck = 0</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">BEGIN</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">SET</span> @SQLState = <span style="color:green;">'ALTER TABLE [' + @TblName + '] NOCHECK CONSTRAINT ALL'</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:blue;">END</span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:blue;">ELSE</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">BEGIN</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">SET</span> @SQLState = <span style="color:green;">'ALTER TABLE [' + @TblName + '] CHECK CONSTRAINT ALL'</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;color:blue;">END</span></p><p style="MARGIN: 0px"><span style="font-family:arial;">EXEC (@SQLState)</span></p></div></div><br /><div style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; OVERFLOW: scroll; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; HEIGHT: 200px"><div style="BACKGROUND: white;font-family:Courier New;font-size:10pt;color:black;" ><p style="MARGIN: 0px"><span style="font-family:arial;"><span style="color:blue;">Public</span> <span style="color:blue;">Shared</span> <span style="color:blue;">Sub</span> DisableEnableAllTableConstraints(<span style="color:blue;">ByVal</span> tableName <span style="color:blue;">As</span> <span style="color:blue;">String</span>, <span style="color:blue;">ByVal</span> isCheck <span style="color:blue;">As</span> <span style="color:blue;">Boolean</span>)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Dim</span> dbCon <span style="color:blue;">As</span> <span style="color:blue;">New</span> SqlConnection(clsConfig.BaseConnectionString)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> dbCon.Open()</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Dim</span> cmd <span style="color:blue;">As</span> <span style="color:blue;">New</span> SqlCommand(<span style="color:#a31515;">"[procDisableEnableAllTableConstraints]"</span>, dbCon)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> cmd.CommandType = CommandType.StoredProcedure</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> cmd.Parameters.AddWithValue(<span style="color:#a31515;">"@TblName"</span>, tableName)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> cmd.Parameters.AddWithValue(<span style="color:#a31515;">"@IsCheck"</span>, IIf(isCheck, 1, 0))</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Try</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> cmd.ExecuteNonQuery()</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Catch</span> ex <span style="color:blue;">As</span> Exception</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Throw</span> (ex)</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">Finally</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> dbCon.Close()</span></p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">End</span> <span style="color:blue;">Try</span></span></p><p style="MARGIN: 0px"><span style="font-family:arial;"></span> </p><p style="MARGIN: 0px"><span style="font-family:arial;"> <span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p></div></div><br /><p><span style="font-family:arial;">Reference:</span></p><p><a href="http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=303&lngWId=5"><span style="font-family:arial;">Stored Procedure to Disable/Reenable All Constraints on a Given Table</span></a><span style="font-family:arial;"> </span></p>Sam Fuhttp://www.blogger.com/profile/18016541109740131805noreply@blogger.com0