Skip to main content

Posts

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

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

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];    ...

SSIS 2008 Loading Flat Text File - First row is missing

I encountered a very strange problem: when I upgraded my SSIS package from SQL Integration Service 2005 to SQL Integration Service 2008, and tried to load flat files, the first row was always missing (With Column Names in First Row checked off and Header rows to skip as 0). I did lots of search and finally found out it is Microsoft SQL Server 2008 Integration Service issue. By applying the SQL Server 2008 Service Pack 3, the problem is gone.

Export Excel with formatting

The formatting you can use: mso-number-format:"0" No Decimals mso-number-format:"0\.00" 2 Decimals mso-number-format:"mm\/dd\/yy" Date format mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM" D -T AMPM mso-number-format:"Short Date" 05/06/-2008 mso-number-format:"Medium Date" 05-jan-2008 mso-number-format:"Short Time" 8:67 mso-number-format:"Medium Time" 8:67 am mso-number-format:"Long Time" 8:67:25:00 mso-number-format:"Percent" Percent - two decimals mso-number-format:"0\.E+00" Scientific Notation mso-number-format:"\@" Text mso-number-format:"\#\ ???\/???" Fractions - up to 3 digits (312/943) mso-number-format:"\0022£\0022\#\,\#\#0\.00" £12.76 mso-number-format:"\#\,\#\#0\.00_ \;\[Red\]\-\#\,\#\#0\.00\ " 2 decimals, negative numbers in red and signed (1.86 -1.66) mso-number-format:”\\#\\,\\#\\#0\\.00_\\)\\;\\[Black\\]\\\\(\\#\\,\\#\\#0\\....

VSTO - Use RichTextBox with Range

In VSTO project, we can use WinForm dialog with RichTextBox to allow user to input rich formatted text. The Rtf property of RichTextBox returns the string representing the content in Rtf format. However, there is no direct way to insert the Rtf content into Word Range object. The Text property of Range object only accept plain text and there is no Rtf property in Range object. After the long search and experiment, I found out that the only way to communicate between RichTextBox and Range object is via Clipboard. From RichTextBox to Range: Private Sub InsertRtf(ByRef strRtfMessage As String, ByRef oRange As Word.Range) With oRange.FormattedText .Style = "Body Single" Clipboard.Clear() Clipboard.SetData(DataFormats.Rtf, strRtfMessage) .Paste() End With End Sub From Range to RichTextBox: Private Function TransformRangeToRtf(ByRef oRange As Word.Range, ByRef rtfMsg As RichTextBox) As String oRange.Copy() rtbMsg...