Quick reference for different SQL Joins

Different SQL Joins

 

 

 

 

 

 

 

 

 

source: Visual Representation of SQL Joins from codeproject.com

Posted in SQL, SQL Server | Tagged , , | Leave a comment

C# Reflection Code to Search Objects for Fields Properties and Methods and Get Values

2016-07-06

Code below will search for a given named property, field, or method inside a given object.

It will search in below order:

  1. Properties
  2. Fields
  3. Methods

It will continue to search until it finds a match that holds or returns (for methods) a non-null value.

E.g. If you searched for “Name” and if there is a property called Name that returns null then the code will continue looking for fields and methods.

SearchTillNotNull  Parameter

If you set the parameter SearchTillNotNull to false then the method will return as soon as it finds a hit even if the resolved value is null.

Searching for methods

Code searches for a method with either the same name as the given SearchFor or a method name with “Get” and SearchFor phrase. (e.g. if SearchFor=”Name” then a methods “GetName”, “getName”, and “Name” will all qualify).

Parameters for methods

Code will only execute matching methods if either the method doesn’t require any parameters or the number of parameters given to the code is same as the number of parameters required by the particular method.

Notes:

  • All searches are are case insensitive and uses OrdinalIgnoreCase string comparison.
  • Uses reflection
  • Implemented in C#.Net
  • Tested to work on .Net framework 2.0 and above

Below is the code, you can also download the .cs file from here


using System;

namespace CMRSoft.Reflection
{
 class ObjectReader
 {
 /// <summary>
 /// Searches the given object until it finds a value for the given SearchFor string
 /// </summary>
 /// <param name="Obj">Object to search inside</param>
 /// <param name="SearchFor">Name of the value to look for</param>
 /// <param name="SearchTillNotNull">If set to true, will continue searching if it finds a match but received null value from the match</param>
 /// <returns>Found value or null</returns>
 public object GetValueFromObject(object Obj, string SearchFor, bool SearchTillNotNull = true, params object[] MethodParameters)
 {
    var t = Obj.GetType();

    // check all properties
    foreach (var prop in t.GetProperties())
    {
       if (prop.Name.Equals(SearchFor, StringComparison.OrdinalIgnoreCase))
       {
          var tmp = prop.GetValue(Obj, null);
          if (tmp != null || !SearchTillNotNull)
          {
             return tmp;
          }
       }
    }


    // check all fields
    foreach (var fld in t.GetFields())
    {
       if (fld.Name.Equals(SearchFor, StringComparison.OrdinalIgnoreCase))
       {
          var tmp = fld.GetValue(Obj);
          if (tmp != null || !SearchTillNotNull)
          {
             return tmp;
          }
       }
    }


    // search methods
    foreach (var mtd in t.GetMethods())
    {
       if (mtd.Name.Equals(SearchFor, StringComparison.OrdinalIgnoreCase) || mtd.Name.Equals("get" + SearchFor, StringComparison.OrdinalIgnoreCase))
       {
          var parameters = mtd.GetParameters();
          if (parameters.Length == 0 || (MethodParameters != null && parameters.Length == MethodParameters.Length))
          {
             var tmp = mtd.Invoke(Obj, MethodParameters);
             if (tmp != null || !SearchTillNotNull)
             {
                return tmp;
             }
          }
       }
    }

    return null;
    }
   }
}

Example use:

namespace CMRSoft.Reflection
{
class Program
{
   static void Main(string[] args)
   {
      ObjectReader _r = new ObjectReader();

      var testObj = new Test() { Value1 = 100, Value2=3 };

      Console.WriteLine(_r.GetValueFromObject(testObj, "value1"));
      // outputs 100

      Console.WriteLine(_r.GetValueFromObject(testObj, "pi", MethodParameters: 4)); 
      // Notice the search phrase does not contain "get". The Object Reader looks for methods with get prefix to what's being searched for.
      // outputs 3.1416

      Console.ReadLine();
   }
}

class Test : ITest
{
   public double Value1 { get; set; }
   public double Value2 { get; set; }

   public double getPi(int DecimalPlaces)
   {
      return Math.Round(Math.PI, DecimalPlaces);
   }
}

interface ITest
{
   double getPi(int DecimalPlaces);
}
}

Please feel free to comment your suggestions.

Menol

Posted in C#.Net | Tagged , , , , , , , , , , , , , | Leave a comment

When (or not) to Hyphenate Compound Adjectives

2016-07-06

Use hyphen only when the compound adjective comes before the noun.

e.g.

1 – The candidate provided an up-to-date curriculum vitae.
2 – The curriculum vitae provided by the candidate was not up to date.

References:

dailywritingtips.com/adverbs-and-hyphens

english.stackexchange.com

Posted in English Language | Tagged , | Comments Off on When (or not) to Hyphenate Compound Adjectives

C# extension method to check if a string matches any string in given list

2016-06-16

/// <summary>
/// Note: this uses ordinal comparison
/// </summary>
/// <param name="?"></param>
/// <param name="Options"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static bool IsOneOf(this string source, IEnumerable<string> Options, bool ignoreCase = false)
{
   return Options.Contains(source, ignoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);
}

 
Posted in C#.Net | Tagged , , , , , | Leave a comment

Trigger window.resize event via javascript

2016-06-15

Use below code to trigger window.resize event.

This can be helpful to refresh some libraries like Chartist.js when a panel  or a div the control is contained in is expanded.


window.dispatchEvent(new Event('resize'));

P.S.

The above method isn’t supported by Internet explorer (failed on IE 11), therefore use below method for wider browser support.


    var fireRefreshEventOnWindow = function () {
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent('resize', true, false);
        window.dispatchEvent(evt);
    };

   // call the fireRefreshEventOnWindow(); function to fire refresh event.
   // ...
Posted in Javascript | Tagged , , , | Leave a comment

Preserving milliseconds when passing dates around front end

2016-06-08

Instead of passing the datetime pass the datetime.Ticks which is a (long) numeric value so the complete datetime value will be preserved.

Then convert ticks back to datetime in the server (controller \ action method).


public ActionResult GetChartLightBox(int EmpId, long RangeStartTicks, long RangeEndTicks)
{
   var RangeStart = new DateTime(RangeStartTicks);
   var RangeEnd = new DateTime(RangeEndTicks);

   // ...

}

Posted in ASP.Net, ASP.NET MVC, C#.Net, Web Development | Tagged , , , | Leave a comment

JavaScript Null Coalesce

2016-06-07

Use || similar to ?? in C#

example (mvc)


var previousRangeSelection = @(Html.Raw(JsonConvert.SerializeObject(TempData["rangeType"]))) || "default"; 

Posted in Javascript | Tagged , | Leave a comment

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

Posted in Uncategorized | 1 Comment

Congfigure IIS to use later .net frameworks

2016/03/07

When IIS is first installed you will most probably get several errors when trying to launch your asp.net website.

This is more prevalent when your application needs a .net framework that is higher than the .net framework version supported by the IIS out of the box.

To fix these errors you need to follow the steps below:

1 – download and install the required .net framework version
2 – add asp.net features to IIS by:

i – open “turn windows features on/off” window from the control panel
ii – expand the IIS node
iii – expand the “world wide web” node
iv – expand “application development features” node
v – check the check box for “ASP.NET”

3 – register your newly installed .net framework to work with ISS:

i – when this was written, the tool that can do this is aspnet_regiis.exe. this file is shipped with the .net framework and located in the version folder.
ii – simply run this tool with the -i parameter to configure IIS to use this version of .net framework.
e.g. c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
you may want to use the Framework64 directory instead depending on the configuration of your computer and IIS. However, I just configured a 64 bit computer for IIS 7 with the normal version and it worked. Perhaps IIS 7 is working as an x86 application; let me know if not.

Note: you will also need to point your asp.net application pool to use the correct .net framework version by:
1 – click “application pools” in IIS manager
2 – select the relevant application pool and click on “basic settings” link on the action pane
3 – select the appropriate .net framework from the .net framework version drop-down.

P.S. some errors you may get are listed below (do comment if you come across other errors that get fixed this way):

  • Handler “ExtensionlessUrlHandler-Integrated-4.0” has a bad module “ManagedPipelineHandler” in its module list
  • The requested page cannot be accessed because the related configuration data for the page is invalid error
Posted in Software Deployment | Tagged , , , , , | Leave a comment

Robert E. Rubin quote

“Some people are more certain of everything than I am of anything.”
― Robert E. Rubin

Posted in General, Quotes | Leave a comment