Monday, 2 December 2024

How to Create a Generic Method to Get the Enum Description in C#

        public static SelectList GetSelectListForEnumWithDescription(Type enumType)
        {
            var list = new List<Object>();
            Array enumValues = Enum.GetValues(enumType);
         
            foreach (Enum enumValue in enumValues)
            {
                list.Add(new
                              {
                                 ID = (int)Enum.Parse(enumType, enumValue.ToString()),
                                 Value = GetEnumDescription(enumValue)});
                               }
              return new SelectList(list, "ID", "Value");
        }

        public static string GetEnumDescription(Enum value)
        {

            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false);

            if (attributes != null && attributes.Length > 0)

                return attributes[0].Description;

            else return value.ToString();
        }
    }

Developer and Reviewer Checklist - Keep Handy while Developing Code

  1. Does the code work as expected? 
  2. Does the code follow the StyleCop/FxCop/JSLint/SonarLint rules?
  3. Review static analysis warning suppressions if any.
  4. Do you see any code duplication?
  5. Please refer the Portal Standardization sheet as well while implementation
  6. Is there any copy-pasted code from any other source?
  7. Does the code follow a modular structure?
  8. Validate code metrics.
  9. Is there any code that is commented out?
  10. Are there any TODO markers or any incomplete code?
  11. Is proper authentication and authorization in place? Validate across personas
  12. Adequate logging (Verbose, Warning, Error etc.)?
  13. Does the code handle all exception scenarios?
  14. · What happens if call to external system fail?
  15. · What happens if the call to database fails?
  16. Can the number of database queries be optimized?
  17. Proper use of cache?
  18. Review transactions
  19. Do methods and functions validate inputs before using it?
  20. Are comments present and are they appropriate?
  21. Do loops have proper terminating conditions?
  22. Do methods have too many parameters?
  23. Do methods, classes etc. have meaningful names?
  24. Does the code perform excessive string concatenation?
  25. For custom exception classes, does the code implement the standard exception pattern?
  26. For classes that need to implement, do they implement the standard dispose pattern?
  27. Does the code use dynamic SQL?
  28. Does the code implement guidelines to avoid XSS attack
  29. Does the code implement technique to avoid CSRF attack
  30. Each UI control should have corresponding ID for automation purpose.
  31. For a control type(ex : dropdown), across the pages it should have same control properties.
  32. TFS task status update
  33. Link TFS ID with check-in
  34. Deployment Scripts, RM updated if applicable
  35. Verified the successful deployment in Dev integration environment

Tuesday, 11 March 2014

MetadataException: Unable to load the specified metadata resource in Visual Studio 2012 or in Entity Frame work

When ever you are getting this error , just be sure that this property was bind in you Entity Framework.

Just open your  .edmx file , go to the properties and then set the property of the "Metadata Artifact Processing"  to "Embed in Output Assembly". That's all your problem is fixed.


Tuesday, 18 February 2014

How to open a new tab On click of a hyper-linked column in Telerik Razor Grid

Today i got a requirement to open a external URL in a tab when the linked column is clicked.
This  can be achieved by simply adding the client template/ template to the linked column.

1) Using Template Attribute in the Telerik Grid
column.Bound(a => a.Website)
      .Template(@<text><a href="@Url.Content(item.Website)" target="_blank">@item.Website</a></text>)
      .HeaderTemplate(
        @<text>
    <a href="javascript:void(0)" class="t-link" orderby="Website">Website</a>
@Html.TextBoxFor(m => m.Filter.Website)         
</text>);

2) Using Client Template Attribute in the Telerik Grid


column.Bound(a => a.Website)
    .ClientTemplate("<a href='<#= Website #>' target='_blank'><#= Website #></a>")
    .HeaderTemplate(
        @<text>
    <a href="javascript:void(0)" class="t-link" orderby="Website">Website</a>
@Html.TextBoxFor(m => m.Filter.Website)         
</text>);

Output:


But the really annoying is that when you click the link it is throwing error  as
 "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable"



This is because the IIS is unable to resolve the path.

Solution to the above issue:

The above issue can be fixed by a simple trick, just you need to append the http:// to the website in the client template or in the template attribute. See the code below:

column.Bound(a => a.Website)
      .Template(@<text><a href="@Url.Content("http://"+item.Website)" target="_blank">@item.Website</a></text>)
      .HeaderTemplate(
        @<text>
    <a href="javascript:void(0)" class="t-link" orderby="Website">Website</a>
@Html.TextBoxFor(m => m.Filter.Website)         
</text>);

Note: Similarly if it is a mail ID column then, use mailto: as the prefix.

Thats it, Enjoy!

Sunday, 12 January 2014

How to Add Re-ordering of the columns in Telerik MVC or Razor Grid.

Some times it is required that the column  had to be re-ordered in a grid as, we are using a Telerik grid , as adding the property reorder will do the task.


Here is the code below to do it:

 .Reorderable(reorder => reorder.Columns(true))

  Just add this line to your Telerik Razor grid, it will do the task.


Happy Coding!

How to show the Pager in Telerik Grid both in top and bottom.

While working in a project , my client wants to show the paging in the grid in both in the top of the grid as well as in the bottom of the grid also. As I am using the Telerik grid, it has a property to set while creating the grid. 

So here is the code for it:


.Pageable(p =>
      {
         p.Style(GridPagerStyles.NextPreviousAndNumeric | GridPagerStyles.PageSizeDropDown);    
         p.Position(GridPagerPosition.Both); // this line will do the Magic
         p.PageSize(int.Parse(ViewBag.PageSize ?? "50"), new[] { 10, 20, 50 });
       }).Sortable()
Happy Coding!

Monday, 23 September 2013

How to Insert space before capital letters using Jquery

Assume you have a string "HelloWorld" and you need to show as "Hello World". How you can achieve it using Jquery.

It can be done in a very simpler way as below:

"Your string".replace( /([a-z])([A-Z])/g, "$1 $2")

so here it will be "HelloWorld".replace( /([a-z])([A-Z])/g, "$1 $2") will produce "Hello World"

Enjoy!

Wednesday, 17 July 2013

Differentiate between 'All' and 'Any' Operators in LINQ.

In LINQ, All operator is  to check whether all the elements met the specific condition or not. While Any operator checks whether there is any element exist in the collection or not.


For Example:

int array={1,2,3,4,5};

bool result=array.All(i=>i < 6); here it will check the elements and returns true.

Similarly,

bool result= array.Any( ); // here it checks whether the collection has any elements and returns true.


Hope you Enjoyed!

Monday, 15 July 2013

Compile Time View Checking In Asp.net MVC

You might have noticed that in MVC3/MVC4 if you had done some thing wrong in the view files it will not throw any error during compilation and it will build successfully. The error in the file is only noticed during executing/rendering the view file. So in order to trap the error during the compilation time you can follow the method below:

Step-1 :
Open your solution project file in any XML Editor or in the Visual Studio itself  and you will find a tag named
<MVCBuildViews>, here it false and just make it true and save it.

<MVCBuildViews>false</MVCBuildViews>  
 change it to 
<MVCBuildViews>true</MVCBuildViews>


Thats all, so now when your view has any error then it will throw error during the compilation.


The Use of ALL Operator in LINQ

Today, I came across the All operator in LINQ. This operator returns bool value rather than records after condition is satisfied. The operator will be highly helpful when you need some kind of validation to find unique data. for e.g. Checking the existing email  or UserID registered and so on.


Lets Go with a Example to do the Task:

int [] array = { 1,2,3,4,5 };

bool result = array.All(value => value > 2 );

Console.WriteLine(result);  // Here it will print False since it will check all the values and                                         // all the values must be grater than 2.


 result = array.All(value => value < 6 );
Console.WriteLine(result); // Here it will print True since it will check all the values and                                          // found all values are less than 6.


Hope it helps you!