Posts

Showing posts from 2014

Injecting a service into a view in MVC 6

Image
  Asp.net MVC 6 supports injecting service to view using a class. Example :- When country combo box value get changed , region com Only restriction is that class should be Public Non-nested Non-Abstract 1. Create a Asp.Net Project in Visual Studio 2015     2. Create a folder name services and add a service class. using System; using System.Threading.Tasks;   namespace ViewInjection.Services {      public class EmployeeService      {          public async Task < int > GetEmployeesOnLeaveCount()          {              return await Task .FromResult(5);          }            public async Task < int > GetEmployeesOnSiteCount()          {              return await Task .FromResult(                  10);          }            public async Task < int > GetEmployeesOnWorkCount()          {              return await Task .FromResult(97);          }      } }   2. Create a View to ...

Visual Studio 2014–New features

Image
  Visual studio 2014 CTP is now available for download. You can download it from here . With the new release there are some new language & IDE features introduced. I will discuss some of IDE improvements in this post. IDE Improvements 1. Colored quick info when you hover over on something in your code now the quick info window has information with colors. also if you collapse your code & hover over to see the code inside so you can see the code with all colored formatting in the tool tip. 2. Hint with un-used references When we create a class there are few references which are automatically added. Also there are some cases we add some piece of code which need some other references & sometime back we remove that code but we don’t remove the reference. Now Visual studio grey out those references & give option to remove those. 3. Hint with redundant qualifiers In your code if you have already added the reference in your code & have used the namespace ...

C# Design Patterns

  Please find the presentation and the sample code I presented on Sri Lanka .Net forum on August 2014 Meet up. August 2014

Cannot read property 'fnSetData' of undefined

  I have a Jquery Datatable in one of my MVC view. When I tried to load the Datatable I got the following error. Cannot read property 'fnSetData' of undefined Problem in my case was my table header and the table body content was not matching. ex: < table cellpadding ="0" cellspacing ="0" border ="0" class ="display" id ="TBL_WorkAreaManagementSearchResults" style ="width: 100%" > < thead > < tr > < th ></ th > < th > WorkArea </ th > < th > Region </ th > </ tr > </ thead > < tbody > @foreach (var workArea in Model) { < tr >...

Editable List box in HTML

  I have recently came across a requirement where  a list of objects need to be added and edited. That object has only two fields which is name & id. So there is no point of using a editable data table as well. Following is the solution I came across. Create a List box Add a text box and a button which will add new items to the list box. On click on list box item load a pop up which allows user to edit the items.   Code for generating Listbox at the View < div >                         < select size ="10" id ="workAreas" multiple >                          @ foreach ( var item in Model.WorkAreas)                          {                                             < option id ="option_ @ item.Id " value =" @ item.Id " onclick ="new RegionManagement().OnWorkAreaEdit( @ item.Id );" class ="TextBoxLarge">                                  @ item.Name                           ...

Azure Exception after scaling

Image
System.Security.Cryptography.CryptographicException: Key not valid for use in specified state. I have recently scaled my application deployed in Azure for 2 instances. After scaling that I got the above exception when loading the application. After searching for hours I came across the solution for it. What you have to do is add the following code snippet in your configuration. < system.identityModel >    < identityConfiguration >      < securityTokenHandlers >        < add type = " System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler,                 System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 " />        < remove type = " System.IdentityModel.Tokens.SessionSecurityTokenHandler,               System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 " />      </ securityTokenHandlers >    </ identityConf...

Retrieve Sql Out parameter in c#

Recently I got an requirement of developing a Stored procedure which searches some data in our database including paging also it should return the total number of records as well. Following is the solution I came up. 1. Create a SP which returns the results set and have an output parameter in it which returns the total records count. when deciding how to get total records from the SP which has paging as well , came across with following solutions. Use COUNT(*) OVER in the select statement which will add a new column to your results set which has the total number of records as the value for each record. Create a temp table with the select statement without paging, return the results set applying paging to that results set, and out the Total count taking the total count in the temp table Use an out parameter for total ,rather creating a temp table run two separate queries and get the result set and count. after reading few articles found out 3rd option is best as it has best perfo...

Asynchronous Programming Using Delegates

Image
Delegated allow you to call any synchronous method asynchronously. In this post I will discuss way of doing this. BeginInvoke() : When a delegate is called using BeginInvoke it has same parameters as the method that you want to execute and two other parameters. AsyncCallback :delegate that references a method to be called when the asynchronous call completes Sender Object : a user-defined object that passes information into the callback method lets see how this could be done step by step. Step 1: Define delegate & call it asynchronously As the first step you can do the followings. Define a delegate with same signature as the method to be called asynchronously Assign the method to be called asynchronously to the delegate Invoke the delegate asynchronously See the sample code here: //Define a delegate with same signature as the method to be called asynchronously    private delegate void UpdateWorkLogHandler ( int hours, string workType);    static void Ma...