Monday, December 11, 2017

Get end day of the month ( jquery / javascript)


This is a simple workout to find the end day of the month for selected date in javascript. Below is the workout on how you can do it.

Lets suppose for May 2017, you want to find the end of the month. On you javascript code you can write the following.

var _Year = 2017;
var _Month = 5;
var _date = (new Date(_Year, _Month, 0, 0, 0, 0));
var endOfMonth = _date.getDate();

endOfMonth is the end day of the month for given year and given month. You can change the year and month yourself or make it dynamic based on arguments by creating a function.

For end of month in sql check the link : http://iamfixed.blogspot.com/2017/11/get-end-of-month-in-mssql-2008-r2-2012.html

jquery ajax cache false ( MVC / c# / jquery / ajax )


Not reaching debug point on jquery ajax call



It may happen that we are using jquery ajax call and the program does not reach debug point in your controller action. This happens when ajax cache is enabled. Disabling of the cache during your ajax call can solve this problem. Below you can see an example.


$.ajax({
                url: _your_url,              
                cache: false,
                success: function (result) {
                    // _success_
                },
                error: function (req, err) {
                   // _error_
                }
            });

Enable the cache only when your ajax call returns the same result even on multiple requests. But if you need to get fresh data from controller action each time on your ajax call, better disable the cache.

Friday, December 8, 2017

Zero (or any character) Padding in javascript / jquery


This is a small manual function written in javascript to pad a certain character to a string. In practical, the string usually can a number which needs to be formatted in order to display properly. These numbers can be days, months or year for a date viewer or any number which follows a padding pattern before some operations.

    // obj : string you want to pad.
    // _lenght : length of you padded result string
    // paddingChar : the padding character which you want to add
    function Padding(obj, _length, paddingChar) {

        var str = String(obj);             
        var returnStr = "";
        if (str.length < _length) {
            for (var i = str.length ; i < parseInt(_length) ; i++) { returnStr += paddingChar; }
        }

        return returnStr + str;
    }

Call the function like this :

var result = Padding(12,5,'0');

It should return '00012'.

Monday, December 4, 2017

CRM Dynamics - ExecuteMultipleRequest error


The request channel timed out while waiting for a reply after 00:01:59.8499880. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.



Set the time out for OrganizationServiceProxy . By default, the timeout is 2 minutes. i.e :

_yourOrganizationProxy.Timeout = new TimeSpan(0,10,0); // sets timeout for 10 minutes


To understand further about executing multiple requests from CRM SDK, check the following link :
https://msdn.microsoft.com/en-us/library/jj863631.aspx

Thursday, November 30, 2017

Simple function to check if the number is prime (c# / .NET)


The function is for the beginners as a sample function to check if the input number is a prime number. It takes a number as argument and returns a boolean result. I have used a very basic algorithm to check if the number is prime. Also, negative numbers are neglected.

public bool IsPrime(int number)
        {
            if (number < 2)
                return false;
            else if (number <= 3)
                return true;

            for (int i = 2; i < number; i++)
            {
                if (number % i == 0)
                    return false;
            }
            return true;
        }

Further, you can use the following facts and optimize above function to find if the number is prime.
  • The number divisible by itself and by 1 is a prime number.
  • 2 is only the even prime number. 
  • if the sum of individual digits of the number is divisible by 3, then the number is divisible by 3
  • We do not consider 0 and 1 as a prime number.
  • Except 5, no numbers ending with 5 is prime.


Override 'Equals(object obj)' Function in a class (c# / .NET)


Lets suppose 'MyClass' is the name of class for which you want to override 'Equals(object obj)' function. In order to override this function, we also need to override 'GetHashCode()' function. For further information you can check the links below :

https://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.object.equals(v=vs.110).aspx
https://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overridden


You can override the function as below.

public class MyClass
    {

        // This function will override the Equals(arg) function when called for MyClass Object.
        public override bool Equals(object obj)
        {

            bool yourReturnBoolean = base.Equals(obj); // write your logic to define this boolean value here.

            return yourReturnBoolean;
        }

        // This function will override the GetHashCode() function
        public override int GetHashCode()
        {
            return base.GetHashCode(); // logic to calculate the hash code
        }
       
    }


Now let's create an object for the class and call 'Equals(object obj)' function.

MyClass obj = new MyClass();           
bool booleanResult = obj.Equals(destinationObject);


Try it yourself and see the result.

Tuesday, November 28, 2017


Generate fibonacci Series (c# / .NET)


This function is for the beginners as a sample function to generate the Fibonacci series in c#. It takes a number 'n' as an argument ( i.e count of series ) and returns a dictionary with index and values. The basic algorithm is used to calculate the series. Also, the limitations and comments are written within the code.

// returns a dictionary with index and value of Fibonacci series for 'n' counts.
// Currently, the value should not be greater than 9223372036854775807, as we are using Int64 a value type. 
        public Dictionary<int,Int64> GetFibonacciSeries(int n)
        {
            // declaration of variables
            Int64 a = 0, b = 1, temp = 0;

            // declare returnlist as dictionary. It returns a dictionary with index and value of Fibonacci series.
            Dictionary<int, Int64> returnList = new Dictionary<int, Int64>();

            // by default add 0 and 1 to the dictionary.
            returnList.Add(1, a);
            returnList.Add(2, b);

            // perform calculations for remaining counts.
            for (int i = 3; i <= n; i++)
            {
                temp = a + b;
                returnList.Add(i, temp);
                a = b; b = temp;
            }

            // returns the populated dictionary.
            return returnList;
        }