Monday, April 21, 2014

Intelligence not working in vs 2013

 

I have faced the issue where my VS 2013’s intelligence was not working. By applying the following setting it working for me.

 

1. TOOLS->Import and Export Settings Wizard->Reset all settings->select “No, just reset settings, overwriting my current settings”->Choose a Default Collection of settings

image

2. Restart Visual Studio Smile

if that didn’t work try the solution here.

Sunday, April 20, 2014

Affine Cipher in C#

 

The affine cipher is a type of monoalphabetic substitution cipher, wherein each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard substitution cipher with a rule governing which letter goes to which. As such, it has the weaknesses of all substitution ciphers. Each letter is enciphered with the function (ax+b)\mod(26), where b is the magnitude of the shift.

For more information on affine cipher visit here

Following code is written in c# which has the affine cipher algorithm

  1.  
  2. public static string AffineEncrypt(string plainText, int a, int b)
  3. {
  4.     string cipherText = "";
  5.  
  6.     // Put Plain Text (all capitals) into Character Array
  7.     char[] chars = plainText.ToUpper().ToCharArray();
  8.  
  9.     // Compute e(x) = (ax + b)(mod m) for every character in the Plain Text
  10.     foreach (char c in chars)
  11.     {
  12.         int x = Convert.ToInt32(c - 65);
  13.         cipherText += Convert.ToChar(((a * x + b) % 26) + 65);
  14.     }
  15.  
  16.     return cipherText;
  17. }
  18.  
  19.  
  20. ///
  21. /// This function takes cipher text and decrypts it using the Affine Cipher
  22. /// d(x) = aInverse * (e(x)  b)(mod m).
  23. ///
  24. public static string AffineDecrypt(string cipherText, int a, int b)
  25. {
  26.     string plainText = "";
  27.  
  28.     // Get Multiplicative Inverse of a
  29.     int aInverse = MultiplicativeInverse(a);
  30.  
  31.     // Put Cipher Text (all capitals) into Character Array
  32.     char[] chars = cipherText.ToUpper().ToCharArray();
  33.  
  34.     // Computer d(x) = aInverse * (e(x)  b)(mod m)
  35.     foreach (char c in chars)
  36.     {
  37.         int x = Convert.ToInt32(c - 65);
  38.         if (x - b < 0) x = Convert.ToInt32(x) + 26;
  39.         plainText += Convert.ToChar(((aInverse * (x - b)) % 26) + 65);
  40.     }
  41.  
  42.     return plainText;
  43. }
  44.  
  45. ///
  46. /// This functions returns the multiplicative inverse of integer a mod 26.
  47. ///
  48. public static int MultiplicativeInverse(int a)
  49. {
  50.     for (int x = 1; x < 27; x++)
  51.     {
  52.         if ((a * x) % 26 == 1)
  53.             return x;
  54.     }
  55.  
  56.     throw new Exception("No multiplicative inverse found!");
  57. }

Happy Coding !!!!

Thursday, April 17, 2014

Loading Processing Window in Asp.net

 

Some functionalities in our systems takes some seconds to execute. In such case if you want to gray out the page and load a pop up window and close it when server side processing is done following code might help you.

aspx Code
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3.     <title></title>
  4.     <link href="StyleSheet1.css" rel="stylesheet" type="text/css" />
  5.  
  6.     <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
  7.     <script type="text/javascript">
  8.         function eventInProgress() {
  9.  
  10.             $('#block').fadeIn();
  11.  
  12.             $('#container').fadeIn();
  13.         }
  14.  
  15.         function CloseInprogressWindow() {
  16.             $('#block').fadeOut();
  17.             $('#container').fadeOut();
  18.         }
  19.  
  20.         $(document).ready(function () {
  21.             $('ul').css({ width: $('#container').width() - 20, height: $('#container').height() - 90 });
  22.         });
  23.     </script>
  24. </head>
  25. <body>
  26.     <form id="form1" runat="server">
  27.         <div id="block"></div>
  28.         <div id="container">
  29.             <h1>test....</h1>
  30.  
  31.         </div>
  32.         <div>
  33.             <asp:Button ID="btnSave" runat="server" Text="Button" OnClientClick="eventInProgress();" OnClick="btnSave_Click" />
  34.  
  35.         </div>
  36.     </form>
  37. </body>
  38. </html>

 

Style Sheet
  1. * {
  2.     font-family: Trebuchet MS;
  3. }
  4.  
  5. #container {
  6.     width: 30%;
  7.     height: 10%;
  8.     display: none;
  9.     position: fixed;
  10.     margin-top: 5%;
  11.     margin-left: 5%;
  12.     background: #FFF;
  13.     border: 1px solid #666;
  14.     border: 1px solid #555;
  15.     box-shadow: 2px 2px 40px #222;
  16.     z-index: 999999;
  17. }
  18. /*#container iframe {display:none; width: 100%; height: 100%; position: absolute; border: none; }*/
  19. #block {
  20.     background: #000;
  21.     opacity: 0.6;
  22.     position: fixed;
  23.     width: 100%;
  24.     height: 100%;
  25.     top: 0;
  26.     left: 0;
  27.     display: none;
  28. }
  29.  
  30. ul {
  31.     padding: 10px;
  32.     background: #EEE;
  33.     position: absolute;
  34.     height: 200px;
  35.     overflow: scroll;
  36. }
  37.  
  38.     ul li {
  39.         color: #222;
  40.         padding: 10px;
  41.         font-size: 22px;
  42.         border-bottom: 1px solid #CCC;
  43.     }
  44.  
  45. h3 {
  46.     font-size: 26px;
  47.     padding: 18px;
  48.     border-bottom: 1px solid #CCC;
  49. }

 

 

Backend Code
  1. protected void btnSave_Click(object sender, EventArgs e)
  2.         {
  3.             System.Threading.Thread.Sleep(5000);
  4.             CloseInprogressWindow();
  5.         }
  6.  
  7.         private void CloseInprogressWindow()
  8.         {
  9.             string javaScript = "CloseInprogressWindow();";
  10.             if (!ClientScript.IsStartupScriptRegistered("CloseInprogressWindow"))
  11.             {
  12.  
  13.                 ScriptManager.RegisterStartupScript(this, this.GetType(), "CloseInprogressWindow", javaScript, true);
  14.             }
  15.         }

Output will Look Like below.

image

Hope this helps.

Happy Coding !!!!

Wednesday, April 16, 2014

PadLeft & PadRight in C#

 

PadLeft and PadRight are two string methods available in c# to align a string value.

Let’s explain this with examples.

PadLeft

It returns a new string that right-aligns the characters in this instance by padding them on the left with a specified Unicode character, for a specified total length.

  1. string text = "John";
  2.  
  3.             Console.WriteLine(text.PadLeft(10,'_'));
  4.             Console.WriteLine(text.PadLeft(10, ' '));
  5.             Console.WriteLine(text.PadLeft(10, '0'));
  6.             Console.WriteLine(text.PadLeft(10, '*'));
  7.  
  8.             Console.ReadLine();
Out put

image

PadRight

It returns a new string that Left-aligns the characters in this instance by padding them on the right with a specified Unicode character, for a specified total length.

  1. string text = "John";
  2.  
  3.             Console.WriteLine(text.PadRight(10,'_'));
  4.             Console.WriteLine(text.PadRight(10, ' '));
  5.             Console.WriteLine(text.PadRight(10, '0'));
  6.             Console.WriteLine(text.PadRight(10, '*'));
  7.  
  8.             Console.ReadLine();

Out put

image

Usage

Certain systems may require a string to be of a fixed length, say 25 characters. If you only have 4 then you need to add 21 characters. Padleft will place your 4 characters on the very right hand of the string, adding enough pad characters to make it 25 characters (so, 21 spaces).

Happy Coding !!!!

Thursday, April 10, 2014

Get Controls by ID

 

I have recently come across a requirement in one of the webform asp.net project where we wanted to get the controls by ID rather accessing the control object it self. Following is the solution we came up with.

I wrote a generic method which will search and return the control.

 

  1. private T GetControl<T>(string controlName, int tabIndex ,Control parentControl)
  2.              where T : Control
  3.  
  4.         {
  5.             string name = controlName + "_TabPanel" + tabIndex.ToString();
  6.             Control control = parentControl == null ? FindControl(name) : parentControl.FindControl(name);
  7.  
  8.  
  9.             return (T)control;
  10.         }

Following is the code where we use the method.

  1. GetControl<TextBox>("tbNewForecast", selectedTabIndex, ForecastTab.ActiveTab).Text = "";

 

Hope this helps you.

Happy Coding !!!!

Tuesday, March 18, 2014

Load MVC view to jquery color box

 

You can use the following set of code to load a MVC view to a jquery color box.

You can get more details on jquery colobox here.

HTML code

  1. <table cellpadding="0" cellspacing="0" border="0" class="display" id="ClientConfigurationList" style="width: 100%" >
  2.         <thead>
  3.             <tr>
  4.                              
  5.                 <th>Work Order No</th>
  6.                 <th>Work Order Status</th>
  7.                 <th>Created On</th>
  8.                 <th>Completed On</th>
  9.                 <th>Asset Description</th>
  10.             </tr>
  11.         </thead>
  12.         <tbody>
  13.             @foreach (var item in Model)
  14.             {
  15.                 <tr class="leftAlign">
  16.                                     
  17.                     <td>
  18.                            <a href="#DIV_WorkOrderDetailsContent"  onclick="new WorkOrderManagement().ViewWorkOrderDetails(@item.Id)" class="colorbox">@Html.DisplayFor(modelItem => item.WorkOrderNo)</a>
  19.                     </td>
  20.                     <td class="leftAlign">
  21.                         @Html.DisplayFor(modelItem => item.WorkOrderStatus)
  22.                     </td>
  23.                     <td>
  24.                         @Html.DisplayFor(modelItem => item.CreatedOn)
  25.                     </td>
  26.                     <td>
  27.                         @Html.DisplayFor(modelItem => item.CompletedOn)
  28.                     </td>
  29.                     <td>
  30.                         @Html.DisplayFor(modelItem => item.AssetDescription)
  31.                     </td>
  32.                 </tr>
  33.             }
  34.         </tbody>
  35.     </table>

 

Javascript Code

  1. this.ViewWorkOrderDetails = function (workOrderId) {
  2.     debugger;
  3.     $('#DIV_WorkOrderDetailsContent').html($("#DIV_WorkOrderDetailsLoadingMessage").html());
  4.     $('#DIV_WorkOrderDetailsContent').load("/Workorder/WorkOrderDetails?workOrderId=" + workOrderId, new function () { });
  5.  
  6.     $('.colorbox').colorbox({ inline: true, height: "80%", width: "50%" });
  7.  
  8. };

Happy Coding !!!!!!!!

Wednesday, March 12, 2014

Dynamically Load External JS , CSS file paths in webconfig

 

If you want to load JavaScript paths or CSS paths saved in web config app settings you can use the following solution.

Step 1:

Create a API controller method which will return the appsettings

  1. public class ConfigurationController : ApiController
  2.  {
  3.   
  4.      [HttpGet]
  5.       public string LoadSettings(Domain.Entities.Configuration model)
  6.  {
  7.      string value = ConfigurationManager.AppSettings.Get(model.Key);
  8.      return value;
  9.  }
  10.  }

 

Step 2:

Create a javascript function which will load javascript or css paths dynamically.

  1. function loadjscssfile(filename, filetype) {
  2.       if (filetype == "js") { //if filename is a external JavaScript file
  3.           var fileref = document.createElement('script');
  4.           fileref.setAttribute("type", "text/javascript");
  5.           fileref.setAttribute("src", filename);
  6.       }
  7.       else if (filetype == "css") { //if filename is an external CSS file
  8.           var fileref = document.createElement("link");
  9.           fileref.setAttribute("rel", "stylesheet");
  10.           fileref.setAttribute("type", "text/css");
  11.           fileref.setAttribute("href", filename);
  12.       }
  13.       if (typeof fileref != "undefined")
  14.           document.getElementsByTagName("head")[0].appendChild(fileref);
  15.   }

 

Step 3:

Create javascript function to call that Api method and call that function on page load.

  1. function loadFiles() {
  2.     debugger;
  3.     var configuration = {
  4.         Key: "IdentityProviderScriptPath"
  5.  
  6.     };
  7.  
  8.  
  9.     $.ajax({
  10.  
  11.         url: "../Api/Configuration/LoadSettings" + productName,
  12.         //data: { name: productName },
  13.         type: "GET",
  14.         dataType: "jsonp",
  15.         error: function (request, status, error) {
  16.             alert(request.responseText);
  17.         },
  18.         success: function (path) {
  19.             loadjscssfile(path, "js");
  20.         }
  21.     });
  22.  
  23.     
  24.  
  25. }

 

  1. <body onload="loadScriptFiles();">

Happy Coding