Wednesday, December 26, 2018

SiteCore Basket missing details Blank


When created shopping cart using commerce accelerator, I found it blank:

  • Items header without any item
  • Summary/Totals appear labels only
After I checked error in browser found the following urls gave me 404:

I found these urls work fine with delivery page.
After investigation found difference in AntiForgery.
Finally, I found problem missing (Commerce metadata control) which add AntiForgery hidden field.

Thursday, October 25, 2018

Publishing Error:System.AggregateException: One or more exceptions occurred while processing the subscribers to the 'item:moving' event.

If you installed Sitecore 9.* and SXA 1.7, you may see error Publishing Error: System.AggregateException: One or more exceptions occurred while processing the subscribers to the 'item:moving' event.

I found patch  for solving this error:

Link for the patch: github.com/.../1.7.0.0

Download the zip file, extract it, and copy its contents into the sitecore website folder.

Tuesday, July 1, 2014

Knockout discover validation problem

Sometimes when you use knockout to build form with validation, You will find errors more than 0 although no message appeared.
Issue: You added validation without binding the contolor
To find property which cause this problem you need to iterate through object properties to find property cause this error using the following code:

var allValidatablesAreValid = true;
for (var property in self)
{
    if (self.hasOwnProperty(property) && self[property]["isValid"]) {
        allValidatablesAreValid = allValidatablesAreValid && self[property].isValid();
    }
    // You can add an early bail out here:
    // if (!allValidatablesAreValid) { break; }
}

Wednesday, December 18, 2013

How can I get K2 worklistitems without open Item

I need to get work flow Item by serial number without opening Item.

When I used below code does not return any  thing:

WorklistCriteria criteria = new WorklistCriteria();
criteria.AddFilterField(WCField.SerialNumber, WCCompare.Equal, serialNumber);
SourceCode.Workflow.Management.WorklistItems wl = cnnWMS.GetWorklistItems(criteria);

Work Field doesn't contain field called serialnumber

I use this code instead:

WorklistCriteriaFilter criteria = new WorklistCriteriaFilter();
RegularFilter rfPID = new RegularFilter();
RegularFilter rfActID = new RegularFilter();
// why doesn't setting this prevent me from knowing alias? Are there constants or enums to use?
//rfPID.TableName = "K2Server.dbo._ProcInst";
rfPID.ColumnName = "PI.ID";
rfPID.ParameterValue = serialNumber.Substring(0, serialNumber.IndexOf('_'));
rfPID.DbType = DbType.Int32;
rfPID.Comparison = Comparison.Equals;
rfPID.ParameterName = "@ICE_ProcInstID";
criteria.FilterCollection.Add(rfPID);

rfActID.Condition = RegularFilter.FilterCondition.AND;
// why doesn't setting this prevent me from knowing alias? Are there constants or enums to use?
//rfActID.TableName = "K2Server.dbo._WorklistHeader";
rfActID.ColumnName = "WLH.ActInstDestID";
rfActID.ParameterValue = serialNumber.Substring(serialNumber.IndexOf('_') + 1);
rfActID.Comparison = Comparison.Equals;
rfActID.ParameterName = "@ICE_ActInstID";
rfActID.DbType = DbType.Int32;
criteria.FilterCollection.Add(rfActID);

SourceCode.Workflow.Management.WorklistItems wl = cnnWMS.GetWorklistItems(criteria);

Install to global assembly cache without gacutil

In Windows Server 2012 you can't install a DLL to the GAC by drag/drop, but you can do it using GACUTIL.exe.
GacUtil.exe is not present on the server by default.
You can do it using the following script instead of Gacutil.exe
Run your powershell and use command like sample below:
.\Add-AssemblyToGlobalAssemblyCache.ps1 FullPath\itegration.dll
This will applied in legacy windows versions

In Windows 2012 you can use the following PowerShell to install dll to GAC

Set-location "c:\temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("c:\temp\MyDLL.dll")
iisreset

 In Windows 2012 you can use the following PowerShell to remove dll from GAC:

Set-location "c:\temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacRemove("c:\temp\MyDLL.dll")
iisreset

Sunday, October 6, 2013

Replace HtmlTextWriter before render

If we want to change HTML of control before rendering you can use the following code:
protected override void Render(HtmlTextWriter writer)
{

MemoryStream stream = new MemoryStream();
StreamWriter memWriter = new StreamWriter(stream);
HtmlTextWriter myWriter = new HtmlTextWriter(memWriter);
base.Render(myWriter);
myWriter.Flush();
stream.Position = 0;
using (StreamReader sr = new StreamReader(stream))
{
string renderedHtml = sr.ReadToEnd();
renderedHtml = renderedHtml.Replace("It looks like your browser does not have JavaScript enabled. Please turn on JavaScript and try again.", "");
writer.Write(renderedHtml);
writer.Close();
myWriter.Close();
}
//base.Render(writer);
}

Tuesday, April 16, 2013

How do I disable administrator prompt in Windows 8/Windows 2012?

I want disable administrator prompt in Windows 8 and Windows 2012, For this I did the following tasks:

  1. Go to Administrative Tools → Local Security Policy
    For windows 8 run secpol.msc
  2. Find Local Policies --> Security Options, in the left side of the window, and scroll down to the button.
  3. User Account Control: Admin Approval Mode for the Built-in Administrator account Disabled. (Default) The built-in Administrator account runs all applications with full administrative privilege.

SQL Server 2008 database engine login failed for administrator user in windows 7/8


I am administrator in my machine windows 8, But I cannot login to SQL using my administrator user.
Problem I installed SQL using other user and I didn't remember its password to solve this problem I followed the following steps:
  1. Open the command prompt (Right-Click on and select "Run As Administrator")
  2. From the command prompt type net stop MSSQLSERVER
  3. Next type net start MSSQLSERVER /m
  4. Open SQL Server Management Studio. Do not login, cancel the login dialog.
  5. From the file Menu select New->Database engine query, and login (Make sure you use the host name and not localhost).
  6. Execute the query ALTER LOGIN sa WITH PASSWORD = ''; to reset the password (if the sa is not enabled then type ALTER LOGIN sa ENABLE to do so)
  7. Login with the sa user and add the Administrator user.

Failed to install Lync in Windows 8


When tried to install Lync in Windows 8 I faced error Another Installation Already in Progress (Although I didn't run previous installation). After some investigation I discovered I need to call setup as administrator.

But in Windows 8, right-clicking on a program in the Start screen won’t display Run as administrator option. While Run as administrator option is available when you right-click on a program shortcut on the desktop, the same option doesn’t appear when you right-click on program shortcut in the Start screen.

Step 1: Switch to the Metro Start screen and start typing the program’s name to see your program

Step 2: Right-click on the program name to see Advanced option.
Step 3: Click on Advanced and then select Run as administrator option.
After these steps, I can install Lync

Sunday, December 23, 2012

How to Make ODP.NET 4.0 (64 bit) working on 64 bit machine


When you Install ODP.NET 64 bit and try to use it in Visual Studio 2010 and the project is of type ASP.NET Website. it give the error from the web.config file during compile time.


"Could not load file or assembly 'Oracle.DataAccess, Version=4.112.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. The system cannot find the file specified"

The best possibility to handle this is to use the x86 version locally with Visual Studio and x64 version on the server with IIS. To do this you have to download both versions - copy one in folder lib\x86 and the other in lib\x64 After this you have to modify the project file(s) - visual studio supports conditional references. Add following section to your project file:


 <PropertyGroup>
     <ReferencesPath Condition=" '$(Platform)' == 'x86' ">..\Lib\x86</ReferencesPath>   
     <ReferencesPath Condition=" '$(Platform)' == 'x64' ">..\Lib\x64</ReferencesPath>     
 </PropertyGroup>


After this reference the odp.net assmebly like this:


  <Reference ... processorArchitecture=$(Platform)">
   <SpecificVersion>False</SpecificVersion>
   <HintPath>$(ReferencesPath)\Oracle.DataAccess.dll</HintPath>          
   <Private>True</Private>
  </Reference>

Thursday, October 18, 2012

OpenXml Reset Numbering Level


I want to create Word Document using OpenXml and create numbering level. I faced a problem when I want to start numbering Level.
To Solve this problem you need to create new instance from abstract number using function as below:

   
      static Numbering AddAbstractNumbering(Numbering numbering1, int numberId,
             int abstractNumber)
      {
          NumberingInstance numberingInstance6 = new NumberingInstance ()
          {NumberID = numberId};          
          AbstractNumId abstractNumId6 = new AbstractNumId() { Val = 1 };
          numberingInstance6.Append(abstractNumId6,
             new LevelOverride() {
                    StartOverrideNumberingValue = new StartOverrideNumberingValue() { Val = 1 }
             } );
          numbering1.Append(numberingInstance6);
          return numbering1;
      }


and use this instance to add numbering level to your paragraph:

   
      Paragraph paragraph2 = new Paragraph();
      ParagraphProperties paragraphProperties1 = new ParagraphProperties();
      ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "ListParagraph" };
      NumberingProperties numberingProperties1 = new NumberingProperties();
      NumberingLevelReference numberingLevelReference1 =  
                                            new NumberingLevelReference() { Val = 0 };
      NumberingId numberingId1 = null;
      if (isNumberingStart)
      {
             numberId++;
             numbering1 = AddAbstractNumbering(numbering1, numberId);
             isNumberingStart = false;
       }
       numberingId1 = new NumberingId() { Val = numberId };
       numberingProperties1.Append(numberingLevelReference1);
       numberingProperties1.Append(numberingId1);

       paragraphProperties1.Append(paragraphStyleId1);
       paragraphProperties1.Append(numberingProperties1);

        paragraph2.Append(paragraphProperties1);

Wednesday, September 5, 2012

Extract pages from pdf to new pdf using iTextSharp

This is sample of code to extract pages from pdf file to new file
public static void Main(string[] args)
        {
            try
            {
                string sourcePath = args[0];
                string outputPath = args[1];
                PdfReader reader = new PdfReader(sourcePath);
                Document document = new Document(reader.GetPageSizeWithRotation(1));
                PdfCopy copier = new PdfCopy(document, new FileStream(outputPath, FileMode.Create));
                //PdfCopy copier = PdfCopy.GetInstance(document, new FileStream(args[1], FileMode.Create));
                int startpage = int.Parse(args[2]);
                int endpage = int.Parse(args[3]);
                document.Open();
                for (int pageCounter = startpage; pageCounter <= endpage && pageCounter < reader.NumberOfPages + 1;
                    pageCounter++)
                {
                    PdfImportedPage page = copier.GetImportedPage(reader, pageCounter);
                    //byte[] page = reader.GetPageContent(pageCounter);
                    copier.AddPage(page);
                }
                document.Close();
                reader.Close();

            }
            catch (DocumentException de)
            {
                System.Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                System.Console.Error.WriteLine(ioe.Message);
            }
        }

Wednesday, May 30, 2012

Getting Date from Excel using openXml

When iterated through excel and read cell contains date it returned int value. you can read cell contains date and convert to simple date format using code below:
  Cell cellDate;            
    string newDate;            
    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open("2.xlsx", false))            
   {
                var sheets = spreadsheetDocument.WorkbookPart.Workbook.Descendants();

                foreach (Sheet sheet in sheets)
                {
                    var sheets = spreadsheetDocument.WorkbookPart.Workbook.Descendants();
                    foreach (Sheet sheet in sheets)
                   {
                       List news = new List();
                       uint rowIndex = 1;
                       uint colIndex = 1;
                       WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(sheet.Id);

                       Worksheet worksheet = worksheetPart.Worksheet;
                       SheetData sheetData = worksheet.GetFirstChild();
                       IEnumerator rows = sheetData
                                            .Elements().GetEnumerator();
                      while (rows.MoveNext())
                      {
                        Row row = rows.Current;
                        cellDate = GetCell(sheetData, "C" + rowIndex.ToString());
                        if (cellDate.DataType != null && cellDate.DataType == CellValues.SharedString)
                        {
                             newDate = GetSharedStringItemById(spreadsheetDocument.WorkbookPart, int.Parse(cellDate.CellValue.InnerText));
                        }
                        else
                        {
                           newDate = DateTime.FromOADate(double.Parse(((CellValue)cellDate.FirstChild).Text)).ToString();
                        }
              }
           }
        }
     }

   public static Cell GetCell(SheetData sheetData, string fullAddress)
   {
            return sheetData.Descendants()
                .Where(c => c.CellReference == fullAddress)
                .FirstOrDefault();
   }

    private static Cell GetCell(Worksheet worksheet, string columnName, uint rowIndex)
    {
          Row row = GetRow(worksheet, rowIndex);

          if (row == null)
                return null;

          return row.Elements().Where(c => string.Compare
                   (c.CellReference.Value, columnName +
                   rowIndex, true) == 0).FirstOrDefault();
    }


    // Given a worksheet and a row index, return the row.
    private static Row GetRow(Worksheet worksheet, uint rowIndex)
    {
          return worksheet.GetFirstChild().
              Elements().Where(r => r.RowIndex == rowIndex).FirstOrDefault();
    }

    public static string GetSharedStringItemById(WorkbookPart workbookPart, int id)
    {
          return workbookPart.SharedStringTablePart.SharedStringTable.Elements()
                .ElementAt(id).InnerText;
    }


Friday, May 11, 2012

Database Performance Considerations

While designing your database, keep performance in mind. You can't really tune performance later when your database is in production.
  1. Normalize and De normalize Tables.
  2. Create Table Columns with maximum row size.(If number of columns larger than row size, split table into multiple tables with relations one to one). For column with extended size like binary, ntext, and...etc We prefer to put in separate table.
  3. Create a primary key on each table. Each primary key is clustered index.
  4. Add calculated columns which you need to avoid complicated queries.
  5. Create an index on any column that is a foreign key.
  6. Create indexes for your query, Indexes will eliminate rows scanning and locking problems:
    • Plan your indexes. Indexes enhance performance but increasing them cause drawback. Choose indexes make big filtration of your records.
    • Create simple index (indexes have only one column) for Where condition.
    • For sort create composite indexes(indexes have multiple columns).
  7. Create Indexed View if Possible.
  8. Use owner qualify of your objects (as much as possible) when you reference them in TSQL. Use owner.table instead of just table. 
  9. Use set nocount on at the top of each stored procedure and set nocount off at the bottom. 
  10. If needn't locking your database table, you take a chance on a dirty read? Ways of dirty work:
    • Use the NOLOCK hint.
    • Use SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED at the top of the procedure, then reset to READ COMMITTED at the bottom.
  11. Return the columns and the rows you need.use select column1, coulmn2,.. instead of select *
  12. Use transactions when appropriate, decrease transaction time.
  13. Avoid temp tables as much as you can.
  14. Indexes will not work with Functions for example, where LTRIM(Name) = 'Products'.
  15. Avoid using OR in your query as much as possible (Cause unexpected plan), Use join instead of it.
  16. Avoid negative query NOT IN (Use outer join),<> (Indexes will not work with Negative query). Same thing when you use like as like '%val'.
  17. If you use dynamic sql (executing a concatenated string), use named parameters and sp_executesql (rather than EXEC). 
  18. Reduce the number of round trips to the server.

SQL Server Naming Conventions

  1. Databases, Files, and File Paths 

    •  The database name should accurately reflect the database content and function. All database names must be prefixed with the originating component's acronym--e.g., CBC_Medicare_Compare or EDG_Database_Tracking. Use both upper and lower case letters as appropriate. Do not use spaces in the name. 
    • File names must match the database name. 
    • If the database is being developed off site from CMS's central office, please create your databases in the default Microsoft directory structure. E.g.,
      C:\Program Files\Microsoft SQL Server\MSSQL\data\OIS_Personnel_Master_Data.MDF 
  2. Tables and Views 

    •  Table names should accurately reflect the table's content and function. Do not use spaces in the name. 
    • View names follow the same conventions as table names, but should be prefixed with the literal 'VW'. E.g.,
      vw1999NewRegulations 
  3. Columns

    The standards below are applicable to all column names:
    • Each column name must be unique within its table.
    • Each column name must be derived from the business name identified during the business/data analysis process. For more information on deriving column names from business names, see Creating Physical Names for Elements and Columns in the Data Administration standards. If the column was not identified during the analysis of business data, it must still be given a spelled-out logical name and an abbreviated physical name. 
    • Do not use reserved or key words as object names. 
    In addition, if the data is going to be brought in-house to interact with other CMS computer systems, the following standards are applicable: 
    • The name can have a maximum of 18 characters. 
    • The name must include acceptable class and modifying words as specified in CMS's Data Administration standards. 
  4. Indexes

    Indexes are named to indicate the table they are attached to and the purpose of the
    index.
    • Primary keys have a suffix of '_PK'.
    • Foreign keys have a suffix of '_FKx' where x is a number that is incrementally assigned.
    • Clustered indexes have a suffix of '_IDX'.
    • All other indexes have a suffix of '_NDXx' where x is incrementally assigned.
    Only one suffix per index may be appended. The application of the appropriate suffix should follow the following hierarchy: primary key, clustered index, foreign key, other index. E.g., an index that is both a primary key and clustered should have a suffix of '_PK'.  It is good practice to index columns that are frequently used in a query's selection criteria. 
  5. Stored Procedure

    • System level stored procedures are named using a prefix 'SP__' (two underscores) and a description of what the stored procedure does. 
    • All application level and user defined stored procedures are prefixed with the constant 'USP' with a description of what the stored procedure does. E.g., UspGetLastModifiedDate 
  6. Triggers

    Triggers are named to indicate the table they are for and the type of trigger. The
    purpose of the trigger is identified in the prefix to the name. All triggers should be
    prefixed with the letter 'T', a letter(s) designating the type, an underscore, and the
    table name. The type should be designated as 'I' = insert, 'U' = update, 'D' = delete.
    E.g., ti_Orders (Insert trigger)
  7. Variables

    Variable identifiers for datatypes should consist of two parts: 
    • The base, which describes the content of the variable; 
    • The prefix, which describes the datatype of the variable 
    Correct prefixes for each datatype are shown in the table below. 

    Datatype Prefix Example
    Char chr @chrFirstName
    Varchar chv @chvActivity
    Nchar chrn @chrnLastName
    Nvarchar chvn @chvnLastName
    Text txt @txtNote
    Ntext txtn @txtnComment
    Datetime dtm @dtmTargetDate
    Smalldatetime dts @dtsCompletedDate
    Tinyint iny @inyActivityID
    Smallint ins @insEquipmentTypeID
    Integer int @intAsset
    Bigint inb @inbGTIN
    Numeric or Decimal dec @decProfit
    Real rea @reaVelocity
    Float flt @fltLength
    Smallmoney mns @mnsCost
    Money mny @mnyPrice
    Binary bin @binPath
    Varbinary biv @bivContract
    Image img @imgLogo
    Bit bit @bitOperational
    Timestamp tsp @tspOrderID
    Uniqueidentifier guid @guidPrice
    sql_variant var @varInventory
    Cursor cur @curInventory
    Table tbl @tblLease

Monday, May 7, 2012

How to Access your Email box using Telnet


Checking Mail

If you installed telnet client and want read popup email do the following steps:
  1. Connect to popup server using command:
    telnet popserver 110Message will appear:
    +OK sigiri POP3 Server (Version 1.004) ready.
  2. Enter your username using command:
    USER username
    Message will appear:
    +OK please send PASS command
  3. Enter your password:
    PASS userpassword
    Message will appear:
    +OK 2 messages ready for manages in ..
  4. Now you can use commands of popup:
    list This will display the total number of messages and size.
    retr #no Displays the message-no including headers. 
    top #no lines Displays the display top lines from message-no including headers.
    dele #no Deletes the message-#no
    quit ends your session.

Sending Mail

  If you installed telnet client and want read smtp email do the following steps:
  1. Connect to popup server using command:
    telnet popserver 25
  2. Introduce  yourself using command:
    HELO hostname
  3. Specify the sender using command:
    MAIL FROM: sender
  4. Specify recipient using command:
    RCPT TO: recipient
  5. Write message using command:
    DATA e-mail message.
    End message dot followed by newline.
  6. You  can specify new sender using command:
    RSET Resets the system. 
  7. You can end smtp session command:
    QUIT Exit sendmail (SMTP)

Convert to Base64

Base64 encoding used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with textual data. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML.

Below way of conversion to Base64 using csharp or javascript:
Conversion from/to base64 using csharp:
    static public string EncodeTo64(string toEncode)
    {
      byte[] toEncodeAsBytes
            = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
      string returnValue
            = System.Convert.ToBase64String(toEncodeAsBytes);
      return returnValue;
    }

    static public string DecodeFrom64(string encodedData)
    {
      byte[] encodedDataAsBytes
          = System.Convert.FromBase64String(encodedData);
      string returnValue =
         System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
      return returnValue;
    }


Conversion from/to base64 using javascript:
var Base64 = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

// public method for encoding
encode : function (input) {
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;

    input = Base64._utf8_encode(input);

    while (i < input.length) {

        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

        output = output +
        Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) +
        Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4);

    }

    return output;
},

// public method for decoding
decode : function (input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    while (i < input.length) {

        enc1 = Base64._keyStr.indexOf(input.charAt(i++));
        enc2 = Base64._keyStr.indexOf(input.charAt(i++));
        enc3 = Base64._keyStr.indexOf(input.charAt(i++));
        enc4 = Base64._keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output = output + String.fromCharCode(chr1);

        if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }

    }

    output = Base64._utf8_decode(output);

    return output;

},

// private method for UTF-8 encoding
_utf8_encode : function (string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

        var c = string.charCodeAt(n);

        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }

    }

    return utftext;
},

// private method for UTF-8 decoding
_utf8_decode : function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while ( i < utftext.length ) {

        c = utftext.charCodeAt(i);

        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        }
        else if((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i+1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else {
            c2 = utftext.charCodeAt(i+1);
            c3 = utftext.charCodeAt(i+2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }

    }
    return string;
}
}

Also there is jquery library for base64:
https://github.com/carlo/jquery-base64

Serialization in Silverlight

When I want to serialize/deserialize class in silverlight 4/5, I found XmlSerializeris not supported.

We can use class DataContractSerializer which is supported by silverlight. Below methods using DataContractSerializer to serialize/deserialize classes:

Serialize method:

        public string Serialize<T>(T data)
        {
            using (var memoryStream = new MemoryStream())
            {
                var serializer = new DataContractSerializer(typeof (T)); 
                serializer.WriteObject(memoryStream, data);

                memoryStream.Seek(0, SeekOrigin.Begin);

                var reader = new StreamReader(memoryStream);
                string content = reader.ReadToEnd();
                return content;
            }
        }

Deserialize method: 



        public T Deserialize<T>(string xml)
        {
            using( var stream = new MemoryStream(Encoding.Unicode.GetBytes(xml)) )
            {
                var serializer = new DataContractSerializer(typeof (T));
                T theObject = (T)serializer.ReadObject(stream);
                return theObject;
            }
        }

Thursday, April 26, 2012

How can you create Multiple instances of same windows service

Many times you want to create windows service with multiple instances in the same server. We must create windows service with different name for each instance.

You can add parameters for different configuration of windows service when you try to install.

Here is an example of installing windows service:
installutil -i /name=LinkDevSch2 /account=localsystem LinkDev.Timer.Service.exe
Here is an example of uninstalling windows service:
installutil /u /name=LinkDevSch1  LinkDev.Timer.Service.exe

You can use these parameters to modify server name, way of authentication. the ProjectInstaller has 4 methods you can override OnBeforeInstall, OnBeforeunInstall, Install, and Uninstall which enables us to make changes to the service installer at the at run time.

  
     public string GetContextParameter(string key)
     {
            string sValue = "";
            try
            {
                sValue = this.Context.Parameters[key].ToString();
            }
            catch
            {
                sValue = "";
            }
            return sValue;
     }


      protected override void OnBeforeInstall(IDictionary savedState)
      {
          
            base.OnBeforeInstall(savedState);

            bool isUserAccount = false;
          
            // Decode the command line switches
            string name        = GetContextParameter("name");
            serviceInstaller.ServiceName = name;

            // What type of credentials to use to run the service
            // The default is User
            string acct        = GetContextParameter("account");
          
            if (0 == acct.Length) acct = "user";

            // Decode the type of account to use
            switch (acct)
            {
                case "user":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.User;
                    isUserAccount = true;
                    break;
                case "localservice":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;
                    break;
                case "localsystem":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                    break;
                case "networkservice":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.NetworkService;
                    break;
                default:
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.User;
                    isUserAccount = true;
                    break;
            }

            // User name and password
            string username = GetContextParameter("user");
            string password = GetContextParameter("password");

            // Should I use a user account?
            if (isUserAccount)
            {
                // If we need to use a user account, set the user name and password
                processInstaller.Username = username;
                processInstaller.Password = password;
            }
        }


        protected override void OnBeforeUninstall(IDictionary savedState)
        {
            base.OnBeforeUninstall(savedState);

            // Set the service name based on the command line
            serviceInstaller.ServiceName = GetContextParameter("name");
        }

Now you can created NET Windows Service with multiple instance any time you want.

Tuesday, April 24, 2012

Tips Comment ASPNET Controls

Some times ASPNET Developer do mistake by commenting server controls
as html comment like below:
<!--<aspnet:TextBox runat="server" id="txt"/>

-->

This comment will affect only by rendering tags as commented html, but doesn't affect its server side events and validations. which
will cause many problems. It is displaying sensitive information, it is vulnerable to cross-site scripting,  failure in firing some scripts like script validation, unexpected behaviour of scripts, and render unwanted tags(affect the performance).

To ignore any of the above problems use server side:
<%--<aspnet:TextBox runat="server" id="txt"/>

--%>