Skip to main content

Posts

Showing posts from 2013

EntityDataSetController with Kendo UI Grid create record issue

When binding Kendo UI Grid to odata data source implemented by .NET EntityDataSetController, I find out the creating function did not work well: The creation of record is successfully at the back end but the Kendo grid does not update the record id returned from creation call. Here is the code for CreateEntity() function:          [HttpPost]         protected override SupplierEntity CreateEntity(SupplierEntity se)         {             Supplier supplier = new Supplier()             {                 SupplierCode = se.SupplierCode,                 SupplierName = se.SupplierName,                 SupplierAddress = se.SupplierAddress,                 SupplierCountry = se.SupplierCountry,                 ModifiedOn = DateTime.Now             };             db.Suppliers.AddObject(supplier);             db.SaveChanges();             se.SupplierID = supplier.SupplierID;             return se;         } Here is the data source definition in front end:         var supplierModel = {     

javascript escape (unescape) pound sign - utf-8 encoding

In UTF-8 encoding, pound sign (£) is two bytes: 0xC2 0xA3. However, if you use javascript escape() function, the output is %A3. In server side, if you use the HttpUtility.UrlDecode("%A3") to decode it,  the result will be three bytes sequence: ef bf bd, which display like  �. In order to get the correct UTF-8  pound character (£), you need to use javascript encodeURIComponent('£') function. The output will be expected %C2%A3.

OData CRUD operations implementation tips

Suppose I have collection of SupplierEntity need to expose as OData: 1. Inherit from EntitySetController < SupplierEnt i ty , int> 2. For getting collections of SupplierEntity object s, override IQueryable < SupplierEnt i ty > Get () method in base class public override IQueryable < SupplierEnt i ty> Get () 3. For getting single SupplierEntity object, override GetEntityByKey(int key) method   protected override SupplierEnt i ty GetEntityByKey ( int key )   4. For creating SupplierEntity object, override both CreateEntity(SupplierEntity entity) and GetKey(SupplierEntity entity) methods protected override SupplierEntity CreateEntity ( SupplierEntity entity ) protected override int GetKey ( SupplierEntity entity )   Note: In CreateEntity method, when the entity has been created in database and the key have been assigned by database, we need to assign this key to entity parameter and return it back to client. 5. For updating SupplierE

Implement Pivot function in SQL

Pivoting data is a common task in reporting. In this article, we will talk about using SQL statement to implement simple pivoting function. Here is a scenario: We have a survey. For each question, use can choose from following answers: 1. Strongly Agree, 2. Agree 3. Disagree 4. Strongly Disagree 5. Not Applicable The table to store user's answers is: CREATE TABLE SurveyAnswer (     EmployeeID int NOT NULL,     QuestionID int NOT NULL,     AnswerID int NULL ) The AnswerID is 1 to 5 corresponding to the answer list above. We would like to display the survey summary as following: Question | Strongly Agree | Agree | Disagree | Strongly Disagree | Not Applicable The SQL to get the results is: SELECT     QustionID,     ISNULL(SUM(CASE WHEN AnswerID = 1 THEN AnswerCount END), 0) AS StronlyAgreeCount,     ISNULL(SUM(CASE WHEN AnswerID = 2 THEN AnswerCount END), 0) AS AgreeCount,     ISNULL(SUM(CASE WHEN AnswerID = 3 THEN AnswerCount END), 0) AS DisagreeCount,

Use X509 certificate to encrypt and decrypt

1. To make a test certificate, use the makecert.exe tool makecert -n "CN=My Company" -ss "MyCompany.com" -pe -sr LocalMachine -sky Exchange test.cer Here the -sky Exchange parameter is very important, without this, the generated certificate can only be used for signing, but not for encrypting/decrypting. 2. Write C# code as following:     public class CertificateSSO     {         private X509Certificate2 GetCertificate()         {             X509Store store = new X509Store(" MyCompany .com", StoreLocation.LocalMachine);             store.Open(OpenFlags.OpenExistingOnly);             X509Certificate2 cert = store.Certificates.Find(X509FindType.FindBySubjectName, "My Company", false)[0];             store.Close();             return cert;         }         public string Encrypt(string strPlainText)         {             X509Certificate2 cert = GetCertificate();             using (RSACryptoServiceProvider provider = (RSACrypto