Friday, March 5, 2010

ASP.NET Caching

The ASP.NET 2.0 Framework supports the following types of caching:
  • Page Output Caching
  • Partial Page Caching
  • DataSource Caching
  • Data Caching
    • 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.
    • 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.
    • You can add almost any object to the cache. For example, you can add custom components, DataSets, DataTables, ArrayLists, and Lists to the cache.
    • 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.
    • 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.
    • 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.
Code Sample:




private string GetDataFromCache()


{


var data = context.Cache["data"];


if (data == null)


{


lock (lockString)


{


if (data == null)


{


data = GetData();


context.Cache.Insert("data", data, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);


}


}


}


return data.ToString();


}






References:

Thursday, March 4, 2010

Kentico BizForm Custom Validation

Here is how you can custom validate Kentico BizForm fields:

protected void Page_Init(object sender, EventArgs e)


{


BizForm.FormName = KenticoHelper.GetDocumentValueByKey("BizFormName");


BizForm.OnBeforeSave += BizForm_OnBeforeSave;


}



protected void BizForm_OnBeforeSave()

{


CMS.FormControls.FormEngineUserControl em1 = (CMS.FormControls.FormEngineUserControl)BizForm.BasicForm.FieldControls["EmailAddress"];


CMS.FormControls.FormEngineUserControl em2 = (CMS.FormControls.FormEngineUserControl)BizForm.BasicForm.FieldControls["ConfirmEmailAddress"];


string EmailAddress = em1.Value.ToString();


string ValidateEmailAddress = em2.Value.ToString();


if (EmailAddress != ValidateEmailAddress)


{


CMS.ExtendedControls.LocalizedLabel ictrl = (CMS.ExtendedControls.LocalizedLabel)BizForm.BasicForm.FieldErrorLabels["ConfirmEmailAddress"];


ictrl.Visible = true;


ictrl.Text = "The e-mail addresses do not match, please check it.";


this.BizForm.StopProcessing = true;


}


CheckBox cbAgreeTerms = (CheckBox)BizForm.BasicForm.FieldControls["AgreeTerms"];


if (!cbAgreeTerms.Checked)


{


CMS.ExtendedControls.LocalizedLabel agreeTerms_ErrorLabel = (CMS.ExtendedControls.LocalizedLabel)BizForm.BasicForm.FieldErrorLabels["AgreeTerms"];


agreeTerms_ErrorLabel.Visible = true;


agreeTerms_ErrorLabel.Text = "Field Required.";


this.BizForm.StopProcessing = true;


}


CheckBox cbUnderstandConfidential = (CheckBox)BizForm.BasicForm.FieldControls["UnderstandConfidential"];


if (!cbUnderstandConfidential.Checked)


{


CMS.ExtendedControls.LocalizedLabel understandConfidential_ErrorLabel = (CMS.ExtendedControls.LocalizedLabel)BizForm.BasicForm.FieldErrorLabels["UnderstandConfidential"];


understandConfidential_ErrorLabel.Visible = true;


understandConfidential_ErrorLabel.Text = "Field Required.";


this.BizForm.StopProcessing = true;


}


CheckBox cbUnderstandChecks = (CheckBox)BizForm.BasicForm.FieldControls["UnderstandChecks"];


if (!cbUnderstandChecks.Checked)


{


CMS.ExtendedControls.LocalizedLabel understandChecks_ErrorLabel = (CMS.ExtendedControls.LocalizedLabel)BizForm.BasicForm.FieldErrorLabels["UnderstandChecks"];


understandChecks_ErrorLabel.Visible = true;


understandChecks_ErrorLabel.Text = "Field Required.";


this.BizForm.StopProcessing = true;


}

}



Monday, January 18, 2010

WCF Basics

Service Contract

  • 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.
  • 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.
  • 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.
  • 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.

Data Contracts

  • 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.

References:

WCF Basics - Service Contracts

WCF Basics - Data Contracts

Microsoft Windows Communication Foundation Step by Step

Thursday, January 7, 2010

Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.

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.

Use 'COLLATE DATABASE_DEFAULT' to resolve the collation conflict, e.g.

SELECT * FROM TableA
WHERE ID COLLATE DATABASE_DEFAULT
IN (SELECT TableAID FROM #temp)

References:
SQL SERVER – Cannot resolve collation conflict for equal to operation
Collation Conflict

Find the name of the server where the site is hosted on

How to find out which website is assigned to which server on your network?

1. Start a command prompt windows (start menu, click on run, type in CMD and press OK/run)

2. Type in the following command and press enter

Nslookup www.yoursite.com.au
It will return an ip – xxx.xx.xx.xxx

3. Now – we type the same command – with the ip above

Nslookup xxx.xx.xx.xxx

It will return the name of the server where the site is hosted on.

Give it a shot……… knock yourself out!