Tuesday, May 30, 2017

Helping Hand for Flood Relief 2017

Helping Hand for Flood Relief 2017

Dear Friends,
It's time to get together and provide a helping hand to our flood affected brothers and sisters. 
We have planned to collect goods & money for this donation drive. You could contribute by supplying  
  • Baby products (milk powder, soap, diapers etc....), 
  • Clothes (New ones or used ones in very good condition), 
  • Sanitary napkins, 
  • Women's Underwear, 
  • Medicine (OTC) and dry rations 
(Items are ordered in the priority).

We are trying to find a transportation channel and we expect your cooperation on that too. 
Otherwise we'll hand over the goods to Rathmalana Air Force Camp or one of the donation programs arranged by radio channels.

Our collection center is at "Level 10, Access Towers, Union Place, Colombo 02

If it's hard for you to visit? us, 
you may transfer your donations to a bank account 
so that we purchases the above items from a wholesale agent in Fort. 
(Please request bank details.) 

We do share the updates and maintain 100% transparency of your donations.


Let's give them a hand.
The best way to find yourself is to lose yourself in the service of others. Mahatma Gandhi

Tuesday, January 17, 2017

Saving Bulk Records in SQL Server

Database operations are costly. Especially if we deal with big number of data. Sometimes even timeout may happen in your application.

There is a better way we can handle this from code level which we can use SqlBulkCopy class to insert bulk data into SQL Server Tables.

You can read more about SqlBulkCopy class here.

The "WriteToServer" method can be used with a DataTable filled with all your data from the application.

Lets get to work;
 DataTable dtFiles = new DataTable();  
 string _conStringRead = string.Empty;  
 string _strPathToScan = string.Empty;  
 _strPathToScan = ConfigurationManager.AppSettings["folderPath"]; // reading folderpath  
 _conStringRead = ConfigurationManager.ConnectionStrings["Read"].ConnectionString; //reading connection string from App.Config file  
 dtFiles.Columns.Add("lbFileName"); // Adding Column to DataTable  
 foreach (var file in Directory.EnumerateFiles(_strPathToScan, "*", SearchOption.TopDirectoryOnly).Select(Path.GetFileName).ToList()) // Get files in folder  
           { //append them to DataTable  
             var toInsert = dtFiles.NewRow();  
             toInsert["lbFileName"] = file;  
             dtFiles.Rows.Add(toInsert);  
           }  
 using (SqlConnection sqlConRead = new SqlConnection(_conStringRead))  
           {//Bulk Insert  
              sqlConRead.Open();  
             var bulkCopy = new SqlBulkCopy(sqlConRead);  
             bulkCopy.DestinationTableName = "Your Table Name";  
             bulkCopy.ColumnMappings.Add("lbFileName", "lbFileName");  
             bulkCopy.WriteToServer(dtFiles);  
           }  

So the App.Config file looks like below;

 <connectionStrings>  
   <add name="Read" connectionString="Data Source=xxxx;Initial Catalog=DB;Persist Security Info=True;User ID=development;Password=development; Current Language=French;" providerName="System.Data.SqlClient"/>    
 </connectionStrings>  
 <appSettings>    
   <add key="folderPath" value="X:\FolderPath\"/>    
 </appSettings>  

Here I insert the list of files in the folder to a given table by matching it's columns with the DataTable columns at once. This is a great solution for dealing with large amount of data.

Try and you will enjoy this.

Happy Coding...

Wednesday, November 23, 2016

Phone numbers - make it easy to read with JQuery

When entering and displaying phone numbers, sometimes its annoying to read all numbers together. Because it may be hard to read. If we can add a space in between few numbers to make it masked, it would be ideal for the user to read it quickly.

It is easy with JQuery. Look at the below function.

 function addSpace(control, flPaste) {    
   if (flPaste == false) {  
     var txtVal = $("#" + control).val();  
     if (txtVal.length == 2) {  
       $("#" + control).val($("#" + control).val() + " ");  
       flSpaceAdded = true;  
     }  
     else if (txtVal.length == 5) {  
       $("#" + control).val($("#" + control).val() + " ");  
       flSpaceAdded = true;  
     }  
     else if (txtVal.length == 8) {  
       $("#" + control).val($("#" + control).val() + " ");  
       flSpaceAdded = true;  
     }  
     else if (txtVal.length == 11) {  
       $("#" + control).val($("#" + control).val() + " ");  
       flSpaceAdded = true;  
     }  
   }  
   else if (flPaste == true) {  
     setTimeout(function () {  
       var txtVal = $("#" + control).val();  
       var txtValNew = "";  
       for (var i = 0, len = txtVal.length; i < len; i++) {  
         txtValNew += txtVal[i];  
         if (i == 1) {  
           txtValNew += " ";  
         }  
         else if (i == 3) {  
           txtValNew += " ";  
         }  
         else if (i == 5) {  
           txtValNew += " ";  
         }  
         else if (i == 7) {  
           txtValNew += " ";  
         }  
       }  
       $("#" + control).val(txtValNew);  
     }, 100);  
   }  
 }  


This can be applied to the textboxes by binding the events below;

 //for typing  
 $("#txtadPhone").keyup(function () {  
     addSpace("txtadPhone",false);  
   });    
 // for pasting  
   $("#txtadPhone").bind('paste', function (e) {  
     addSpace("txtadPhone", true);  
   });  

So the textbox will look like this;


So you will have 14 characters for a 10 digit numbers here. Therefore you may need to cater it in the database or else you need to remove the space using a method in the codebehind. For example,


 public static string removeSpace(string str)  
     {  
       if (str == null)  
         return null;  
       var vVal = string.Empty;  
       foreach (var v in str.ToCharArray())  
       {  
         if ((int)v == 32)  
           continue;  
         vVal += v.ToString();  
       }  
       return vVal;  
     }  

This is a static method that remove the space. However, you may not need to apply this but if there is a restriction in the DB side, you can always use this.

Happy Coding... :)

Tuesday, November 1, 2016

Solved : ASP.Net form submit and page expire when navigate back

Each page we create in ASP.Net has at least one form. Those form can be submitted in many ways. Either a postback from a server control or an html button can submit a form using JavaScript.

Look at the below image.




















The page has expired due to a submission and user tries to navigate backward which the server needs the page to be submitted. Simulating the issue in a live environment would be like below;

1. You can have a filtering criteria as this.
2. The "Search" is an anchor tag that has a onClick event bind to it.




3. When user tick on each of the check boxes, the id values are stored in a hidden field and on "javascript:submitForm()" function the page (form) is resubmitted to the server to filter the product list. So the "submitForm" function looks like below;

 <input id="hdnFilterCriteria" name="hdnFilterCriteria" value="4339435,4339434,4339427," type="hidden">  


4. This causes the page to post back with the filtering criteria appended to the hidden field.
5. After the postback, if the user move in to another page and hit browser back button to see the previous page, the following error will display due to the document expiration.


















It is time to see how we can avoid this error. It is not that hard and all you need is to think outside the box. Simply avoid page postbacks / submissions. In this case we uses JQuery AJAX. Let's focus about the solution.

1. Create a class with the all properties included.
 public class SearchAttribute  
 {  
   public string idAttribute { get; set; }    
   public string strFilterCriteria { get; set; }    
   // You can have all the attributes as properties here  
 }  

2. Use the Cache / Session to maintain the search history. You can use Ajax to update the cache. See the web method below;

   [System.Web.Script.Services.ScriptMethod]  
   [WebMethod(EnableSession = true)]  
   public string catProductHiddenValueCaching(int idSearch,string idAttribute, string strFilterCriteria)  
   {  
     var cacheName = HttpContext.Current.Session.SessionID.ToString() + "_SearchAttribute";  
     Dictionary<int, SearchAttribute> searchHistory = new Dictionary<int, SearchAttribute>();  
     if (idSearch > 1)  
     {        
       var cacheSA = HttpContext.Current.Cache.Get(cacheName);  
       if (cacheSA != null)  
         searchHistory = (Dictionary<int, SearchAttribute>)cacheSA;  
     }  
     SearchAttribute sa = new SearchAttribute()  
     {  
       idAttribute = idAttribute,  
       strFilterCriteria = strFilterCriteria  
     };  
     searchHistory.Add(idSearch, sa);  
     HttpContext.Current.Cache.Insert(cacheName, searchHistory);      
     JavaScriptSerializer js = new JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue };  
     return js.Serialize(idSearch.ToString());  
   }  

  • Use Session ID + Cache name to avoid conflicts with shared cache location.
  • Use Dictionary to store search attribute instances of each search
  • Use the Cache.Insert to overwrite the existing cache value.

3. Inside the onclick event of the "Search" button, call this function asynchronously.

     function submitForm(){  
          try{  
         var j = jQuery.noConflict();  
         var inSearch = getParameterByName('inSearch'); // to track the no of search histories  
         if(inSearch){  
             inSearch = parseInt(inSearch);  
             inSearch++;  
           }  
         else  
           inSearch = 1;  
         var urlAjax = 'http://'+window.location.hostname+'/your_service.asmx/setFilderValueCache'; // get the service path with full URL  
         var idAttribute = j("#idAttribute").val();  
         var strFilterCriteria = j("#strFilterCriteria").val();  
         var objPara = {  
           idSearch:inSearch,  
           idAttribute: idAttribute,  
           strFilterCriteria: strFilterCriteria  
         };  
         j.ajax({  
           type: "POST",  
           url: urlAjax,  
           data: JSON.stringify(objPara),  
           contentType: "application/json; charset=utf-8",  
           dataType: "json",  
           success: function (msg) {  
             var ret = JSON.parse(msg.d);     
             var url = window.location.href;    
             var newUrl = queryStringUrlReplacement(url, 'inSearch', inSearch); // append the inSearch as a query string parameter.  
             window.location.href = newUrl;               
           },  
           error: function (err) {  
             alert(err.responseText);  
           }  
         });  
          }catch(e){}  
     }  

  • idSearch is used to update the query string and reload the page with query string parameter.

 function queryStringUrlReplacement(url, param, value)   
     {  
       var re = new RegExp("[\\?&]" + param + "=([^&#]*)"), match = re.exec(url), delimiter, newString;  
       if (match === null) {          
         var hasQuestionMark = /\?/.test(url);   
         delimiter = hasQuestionMark ? "&" : "?";  
         newString = url + delimiter + param + "=" + value;  
       } else {  
         delimiter = match[0].charAt(0);  
         newString = url.replace(re, delimiter + param + "=" + value);  
       }  
       return newString;  
     }  

4. When "Search" is clicked, the relevant cache object is accessed and passed the search criteria to it's parameters inside "Page Load"

 protected void Page_Load(object sender, EventArgs e)  
   {      
     string strFilterCriteria= ((Request.Form["strFilterCriteria"] ?? Request.QueryString["strFilterCriteria"]) ?? string.Empty);  
     string inSearch = Request.QueryString["inSearch"];  
     if (!string.IsNullOrEmpty(inSearch))  
     {  
       int iSearch = int.Parse(inSearch);  
       var cacheName = HttpContext.Current.Session.SessionID.ToString() + "_SearchAttribute";  
       Dictionary<int, SearchAttribute> searchHistory = new Dictionary<int, SearchAttribute>();  
       var cacheSA = HttpContext.Current.Cache.Get(cacheName);  
       if (cacheSA != null)  
         searchHistory = (Dictionary<int, SearchAttribute>)cacheSA;  
       if (searchHistory.Count > 0)  
       {  
         SearchAttribute sa = searchHistory[iSearch];  
         if (sa == null)  
           return;  
         if (!string.IsNullOrEmpty(sa.idAttribute))  
           idAttribute = int.Parse(sa.idAttribute);  
         strFilterCriteria= sa.strFilterCriteria;               
       }  
     }  
    // Use 'strFilterCriteria' variable to store the currently filtered id list and pass it to the relevant method to get the result.  
 }  


Now each time the "Search" is clicked, it stores the search criteria in the cache and reload with inSearch as a query string parameter. There is no page postback and no document expiration happens. Each time it loads a fresh copy with the idSearch in the query string as "?inSearch=1" or "?inSearch=2" ..etc. The number increases on each Click to Search button. Navigate backward will retrieve the relevant result from the cached dictionary.

So, it is easy. Just a simple solution. :)

Happy coding... :)

Monday, October 31, 2016

Sticky notes in your webpage... Part 2 with Database

In part 1, we discussed most of the functions in postitall. Here we discuss how we can save them in the database so that we can show them as and when needed. Let's see how we need to do it.

1. Add a new save button for a new personal note.

 var r = $('<a id="pia_save_0" class="PIAicon" href="#" style="opacity: 1; display: inline;"> <i class="fa fa-floppy-o note-icon-save" aria-hidden="true" title="Save" onclick="SaveNote();"> </i> </a>');  
         $("#idPIAIconBottom_0").append(r);  //0 is the id for new note. only one note can be opened at first and upon saving it, another can be opened.

2. "onclick="SaveNote();" this will call the function below;

 // The id is 0 for the new personal note. This is called inside your new personal note popup  
 function SaveNote() {  
   try{  
     var p = jQuery.noConflict();   
     cstPersonalNoteUpd(0, '', p('#pia_editable_0').html(), true, p('#minicolors_bg_0').val(), p('#minicolors_text_0').val(), '', '');  
     p.PostItAll.destroy('#PIApostit_0');  //0 is the id
   }  
   catch (err) {  
     alert(err.message);  
   }  
 }  


3. "cstPersonalNoteUpd" is a JS function that uses JQuery AJAX to handle DB operations (Insert / Update)


 function cstPersonalNoteUpd(idPersonalNote, lbPersonalNote, flActive, lbBackGroundColor, lbFontColor, dtCallBack, idPNDescr) {  
   try {  
     if (lbPersonalNote.length == 0)   
       return;  
     var objPara = {  
       idPersonalNote: idPersonalNote,        
       lbPersonalNote: lbPersonalNote,  
       flActive: flActive,  
       lbBackGroundColor: lbBackGroundColor,  
       lbFontColor: lbFontColor,  
       dtCallBack: dtCallBack  
     };  
     $.ajax({  
       type: "POST",  
       url: 'yourservice.asmx/cstPersonalNoteUpd',  
       data: JSON.stringify(objPara),  
       contentType: "application/json; charset=utf-8",  
       dataType: "json",  
       success: function (msg) {  
         var ret = JSON.parse(msg.d);  
         $('#hdnIdPersonalNote').val(ret); // get the id to a hidden field          
       },  
       error: function (err) {  
         alert(err.responseText);  
       }  
     });  
   }  
   catch (err) {  
     alert(err.message);  
   }  
 }  

4. Write your own service with "cstPersonalNoteUpd" webmethod.

     [WebMethod(EnableSession = true)]  
     public string cstPersonalNoteUpd(int idPersonalNote, string lbTitlePersonalNote, string lbPersonalNote, bool flActive, string lbBackGroundColor, string lbFontColor, string dtCallBack)  
     {  
       if (HttpContext.Current.Session["LoggedInPerson"] == null) return new JavaScriptSerializer().Serialize(string.Empty);  
       JavaScriptSerializer js = new JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue };  
       PersonEntity person = (PersonEntity)HttpContext.Current.Session["LoggedInPerson"];  
       PersonalNoteEntity pn = new PersonalNoteEntity();  
       pn.idPersonalNote = idPersonalNote;         
       pn.idPerson = person.idPerson;         
       pn.lbPersonalNote = string.IsNullOrEmpty(lbPersonalNote.Trim()) ? null : lbPersonalNote.Trim();   
       pn.flActive = flActive;  //boolean
       pn.lbBackGroundColor = string.IsNullOrEmpty(lbBackGroundColor.Trim()) ? null : lbBackGroundColor.Trim();   
       pn.lbFontColor = string.IsNullOrEmpty(lbFontColor.Trim()) ? null : lbFontColor.Trim();   
       if (!string.IsNullOrEmpty(dtCallBack))  //for hiding
       {  
         DateTime dtCB = DateTime.ParseExact(dtCallBack, "dd/MM/yyyy",CultureInfo.InvariantCulture);  
         pn.dtCallBack = dtCB;  
       }        
       new bllPersonalNote().cstPersonalNoteUpd(pn);  
       return js.Serialize(pn.idPersonalNote.ToString());  //return the id
     }  

5. The "cstPersonalNoteUpd" function should be called whenever a text is changed or a new personal note is created to save them in the database. If new, the id is returned and otherwise it updates. (You need to handle it in your procedure as below;)

 IF ISNULL(@idPersonalNote, 0) = 0 AND @idPerson > 0      
  BEGIN     
  -- Mode Insert    
  END  
 ELSE  
  BEGIN   
  -- update more  
  END  
 END  

6. Now you have done both the server side as well as client side function call. A new note will be saved. But to update an existing one, you can use "onChange" event of the postitall.

 onChange: function (id) {  
       cstPersonalNoteUpd(pid, '', p('#pia_editable_' + pid).html(), true, p('#minicolors_bg_' + pid).val(), p('#minicolors_text_' + pid).val(), '', idPNDescr);  
       return undefined;  
     },  

The pid is the id of the note where you can pass to update the existing record in the DB.

7. Same as above, you can have your own service call to return the list of notes available in the DB and show them in the web application.

Let's see how it is shown in the application.

Adding a new note.



To show the list in your dashboard. ( clicking on a single note can open it in the note editor.






HTML Special Characters in your web application

HTMLEncode and HTMLDecode are common methods which can be in HttpUtility or WebUtility to encode and decode html tags or templates in your web application. Usually, HTML templates are updated by the Admin which uses the same system with admin rights. 
But what if the application you develop is accessing different databases that are updated from various admin portals or platforms. Hence the templates may contain many tags or characters and the above HTMLDecode will not work for them. 
Following method represents the conversion of those special characters to the HTML Encoded values so that it can be understood by the browser to show the correct symbol.
To do so, you will need to first get the ASCII of each of the character and if the values falls in to a special character, replace it with a properly formatted string. Simply it looks like below;

 public static string GetSpecialCharactersEncoded(string text)  
     {  
       if (text == null)  
         return text;  
       string returnString = text;  
       Dictionary<int, string> dicList = new Dictionary<int, string>();  
       StringBuilder sb = new StringBuilder();  
       foreach (var c in text.ToCharArray())  
       {  
         sb.Append(GetEncodedValue(c.ToString()));  
       }  
       return sb.ToString();  
     }  

     private static string GetEncodedValue(string text)  
     {  
       string encTxt = text;  
       int iAscii = (int)Convert.ToChar(text);  
       if (iAscii == 8230)  
         encTxt = "&hellip;";  
       else if (iAscii == 8211)  
         encTxt = "&ndash;";  
       else if (iAscii == 8212)  
         encTxt = "&mdash;";  
       else if (iAscii == 8216)  
         encTxt = "&lsquo; ";  
       else if (iAscii == 8217)  
         encTxt = "&rsquo;";  
       else if (iAscii == 8218)  
         encTxt = "&lsquor;";  
       else if (iAscii == 8220)  
         encTxt = "&ldquo; ";  
       else if (iAscii == 8221)  
         encTxt = "&rdquo;";  
       else if (iAscii == 8222)  
         encTxt = "&ldquor;";  
       else if (iAscii == 8224)  
         encTxt = "&dagger;";  
       else if (iAscii == 8225)  
         encTxt = "&Dagger;";  
       else if (iAscii == 8226)  
         encTxt = "&bull;";  
       else if (iAscii == 8240)  
         encTxt = "&permil;";  
       else if (iAscii == 8364)  
         encTxt = "&euro;";  
       else if (iAscii == 8482)  
         encTxt = "&trade;";  
       else if (iAscii == 402)  
         encTxt = "&fnof;";  
       else if (iAscii == 376)  
         encTxt = "&yuml;";  
       else if (iAscii == 353)  
         encTxt = "&scaron;";  
       else if (iAscii == 352)  
         encTxt = "&Scaron;";  
       else if (iAscii == 339)  
         encTxt = "&oelig;";  
       else if (iAscii == 338)  
         encTxt = "&OElig;";  
       else if (iAscii == 255)  
         encTxt = "&yuml;";  
       else if (iAscii == 254)  
         encTxt = "&thorn;";  
       else if (iAscii == 253)  
         encTxt = "&yacute;";  
       else if (iAscii == 252)  
         encTxt = "&uuml;";  
       else if (iAscii == 251)  
         encTxt = "&ucirc;";  
       else if (iAscii == 250)  
         encTxt = "&uacute;";  
       else if (iAscii == 249)  
         encTxt = "&ugrave;";  
       else if (iAscii == 248)  
         encTxt = "&oslash;";  
       else if (iAscii == 247)  
         encTxt = "&divide;";  
       else if (iAscii == 246)  
         encTxt = "&ouml;";  
       else if (iAscii == 245)  
         encTxt = "&otilde;";  
       else if (iAscii == 244)  
         encTxt = "&ocirc;";  
       else if (iAscii == 243)  
         encTxt = "&oacute;";  
       else if (iAscii == 242)  
         encTxt = "&ograve;";  
       else if (iAscii == 241)  
         encTxt = "&ntilde;";  
       else if (iAscii == 240)  
         encTxt = "&eth;";  
       else if (iAscii == 239)  
         encTxt = "&iuml;";  
       else if (iAscii == 238)  
         encTxt = "&icirc;";  
       else if (iAscii == 237)  
         encTxt = "&iacute;";  
       else if (iAscii == 236)  
         encTxt = "&igrave;";  
       else if (iAscii == 235)  
         encTxt = "&euml;";  
       else if (iAscii == 234)  
         encTxt = "&ecirc;";  
       else if (iAscii == 233)  
         encTxt = "&eacute;";  
       else if (iAscii == 232)  
         encTxt = "&egrave;";  
       else if (iAscii == 231)  
         encTxt = "&ccedil;";  
       else if (iAscii == 230)  
         encTxt = "&aelig;";  
       else if (iAscii == 229)  
         encTxt = "&aring;";  
       else if (iAscii == 228)  
         encTxt = "&auml;";  
       else if (iAscii == 227)  
         encTxt = "&atilde;";  
       else if (iAscii == 226)  
         encTxt = "&acirc;";  
       else if (iAscii == 225)  
         encTxt = "&aacute;";  
       else if (iAscii == 224)  
         encTxt = "&agrave;";  
       else if (iAscii == 223)  
         encTxt = "&szlig;";  
       else if (iAscii == 222)  
         encTxt = "&THORN;";  
       else if (iAscii == 221)  
         encTxt = "&Yacute;";  
       else if (iAscii == 220)  
         encTxt = "&Uuml;";  
       else if (iAscii == 219)  
         encTxt = "&Ucirc;";  
       else if (iAscii == 218)  
         encTxt = "&Uacute;";  
       else if (iAscii == 217)  
         encTxt = "&Ugrave;";  
       else if (iAscii == 216)  
         encTxt = "&Oslash;";  
       else if (iAscii == 215)  
         encTxt = "&times;";  
       else if (iAscii == 214)  
         encTxt = "&Ouml;";  
       else if (iAscii == 213)  
         encTxt = "&Otilde;";  
       else if (iAscii == 212)  
         encTxt = "&Ocirc;";  
       else if (iAscii == 211)  
         encTxt = "&Oacute;";  
       else if (iAscii == 210)  
         encTxt = "&Ograve;";  
       else if (iAscii == 209)  
         encTxt = "&Ntilde;";  
       else if (iAscii == 208)  
         encTxt = "&ETH;";  
       else if (iAscii == 207)  
         encTxt = "&Iuml;";  
       else if (iAscii == 206)  
         encTxt = "&Icirc;";  
       else if (iAscii == 205)  
         encTxt = "&Iacute;";  
       else if (iAscii == 204)  
         encTxt = "&Igrave;";  
       else if (iAscii == 203)  
         encTxt = "&Euml;";  
       else if (iAscii == 202)  
         encTxt = "&Ecirc;";  
       else if (iAscii == 201)  
         encTxt = "&Eacute;";  
       else if (iAscii == 200)  
         encTxt = "&Egrave;";  
       else if (iAscii == 199)  
         encTxt = "&Ccedil;";  
       else if (iAscii == 198)  
         encTxt = "&AElig;";  
       else if (iAscii == 197)  
         encTxt = "&#197;";  
       else if (iAscii == 196)  
         encTxt = "&Auml;";  
       else if (iAscii == 195)  
         encTxt = "&Atilde;";  
       else if (iAscii == 194)  
         encTxt = "&Acirc;";  
       else if (iAscii == 193)  
         encTxt = "&Aacute;";  
       else if (iAscii == 192)  
         encTxt = "&Agrave;";  
       else if (iAscii == 191)  
         encTxt = "&iquest;";  
       else if (iAscii == 190)  
         encTxt = "&frac34;";  
       else if (iAscii == 189)  
         encTxt = "&frac12;";  
       else if (iAscii == 188)  
         encTxt = "&frac14;";  
       else if (iAscii == 187)  
         encTxt = "&raquo;";  
       else if (iAscii == 186)  
         encTxt = "&ordm;";  
       else if (iAscii == 185)  
         encTxt = "&sup1;";  
       else if (iAscii == 184)  
         encTxt = "&cedil;";  
       else if (iAscii == 183)  
         encTxt = "&middot;";  
       else if (iAscii == 182)  
         encTxt = "&para;";  
       else if (iAscii == 181)  
         encTxt = "&micro;";  
       else if (iAscii == 180)  
         encTxt = "&acute;";  
       else if (iAscii == 179)  
         encTxt = "&sup3;";  
       else if (iAscii == 178)  
         encTxt = "&sup2;";  
       else if (iAscii == 177)  
         encTxt = "&plusmn;";  
       else if (iAscii == 176)  
         encTxt = "&deg;";  
       else if (iAscii == 175)  
         encTxt = "&macr;";  
       else if (iAscii == 174)  
         encTxt = "&reg;";  
       else if (iAscii == 173)  
         encTxt = "&shy;";  
       else if (iAscii == 172)  
         encTxt = "&not;";  
       else if (iAscii == 171)  
         encTxt = "&laquo;";  
       else if (iAscii == 170)  
         encTxt = "&ordf;";  
       else if (iAscii == 169)  
         encTxt = "&copy;";  
       else if (iAscii == 168)  
         encTxt = "&uml;";  
       else if (iAscii == 167)  
         encTxt = "&sect;";  
       else if (iAscii == 166)  
         encTxt = "&brvbar;";  
       else if (iAscii == 165)  
         encTxt = "&yen;";  
       else if (iAscii == 164)  
         encTxt = "&curren;";  
       else if (iAscii == 163)  
         encTxt = "&pound;";  
       else if (iAscii == 162)  
         encTxt = "&cent;";  
       else if (iAscii == 161)  
         encTxt = "&iexcl;";  
       else if (iAscii == 160)  
         encTxt = "&nbsp;";  
       else if (iAscii == 34)  
         encTxt = "&quot;";  
       return encTxt;  
     }  

To call this method;

string res = GetSpecialCharactersEncoded(str);

For further reading, visit here (http://www.ascii.cl/htmlcodes.htm)

The character entity reference chart is here. (https://dev.w3.org/html5/html-author/charref)

Using this method will show the special characters without any issues.

Happy coding... :)


Monday, October 24, 2016

Sticky notes in your webpage...

Sticky notes are a good way to keep things noted such as "To Do Lists". There are many ways we can have sticky notes in a desktop or notebook. But if we have a portal or a website and needed to provide this facility, we have a good JQuery plug in.

http://postitall.txusko.com/plugin.html

You may find there is a Chrome plugin too but what you need is the JQuery one. Download it and add it to your project. After that, provide relevant references.
 <!-- CSS -->  
 <link rel="stylesheet" href="dist/jquery.postitall.css">  
 <!-- JS -->  
 <script src="libs/jquery/dist/jquery.min.js"></script>  
 <script src="dist/jquery.postitall.js"></script>  

You also may find some plug ins such as JQuery draggabletrumbowyg editor and minicolors for color picker.

You can find relevant examples and "How to" in the documentation here.

Now let's see how we can use this in our own website with more features.

 function addPersonalNote() {  
   var p = jQuery.noConflict();  // to avoid multiple JQuery references
   p.PostItAll.new({  //create new note
     content: 'Default content to show',  //
     id: 0,  // this has to be unique 
     posX: $(window).width() / 2,  // center of the page
     posY: '10',  //top of the page
     features: { addNew: false },  // adding new upon another is prohibited
     onCreated: function (id, options, obj) { return undefined; },  //fires at the creation time
     onChange: function (id) {  return undefined;  },  // fires when content is changed
     onSelect: function (id) { return undefined; },  // fires when selected
     onDelete: function (id) { return undefined; }  // fires when deleted
   });  
 }  

This is a simple way of adding a new note. The function "addPersonalNote()" creates a new sticky note as below.

 You can maximize it, minimize or even close it. or even can add new icons too. Also you can have the option to change the color through color picker too.



In our next part, we will talk about how to save this in the database. Until that, practice what we have done so far...

Happy Coding... :)