Thursday, February 9, 2017

C# WCF Tips: Fix error “object reference is not set to an instance object” while adding WCF service reference

When we adding service reference to project sometimes we encounter “object reference is not set to an instance object” error. Sometimes it fix after follow following steps.


Step 1: Check are there any errors in WCF service and if there were errors fix it and retry.

Step 2: If there were existing service reference then delete it and add again. 

Step 3: If step 2 not success then delete reference from service reference folder in project location (from hard disc) and try to add reference again.

Step 4: If first three steps not working go to the app config or web config file and remove relevant endpoint settings and try to add again.

Step 5: Host WCF service in iis and add service reference from iis url

Sunday, January 1, 2017

Kendo Grid Tips: Navigate Kendo Grid CellsUsing Tab Key Based on Row and Column

Some time we have requirements to move user to pre determine cells in the kendo grid based on conditions. As example there were kendo grid after user save data it disable correct cells and enable cells which have errors. User has requirement to move only error cells using tab key.

For such kind of requirements can use following code snippets to navigate user.

  $(document).ready(function () {
        $("#Grid").on("keyup", 'tr', function (event) {
            navigateColorGrid(event);

        });     
    });



function navigateColorGrid(e) {

    if (e.keyCode == 9) {

        event.stopPropagation();

        event.stopImmediatePropagation();

        event.preventDefault(); //Prevent default tab key function

        var eCell = $(e.target).closest('td');
        var currentIndex = eCell.index();
        var currentIndexRow = $(e.target).closest('tr').index();
        var dataGrid = $('#Grid').data().kendoGrid;
        var currentRow = $(e.target).closest('tr');
        eCell = $(currentRow).find('td:eq(' + currentIndex + ')');

        if (invalidCells.length > 0) {
            tabToNextCell(currentIndexRow, currentIndex);
        } else {
            setTimeout(function () {
                var grid = $("#Grid").data("kendoGrid");
                if (eCell != null) {
                    grid.editCell(eCell);
                    var inputText = eCell.find("input").length;
                    var cPosition = inputText - 1;
                }

            }, 10);

        }

    }

}


function tabToNextCell(currentIndexRow, currentIndex) {

    var eCell = $("#Grid").children().find("tbody").find("tr:eq(" + currentIndexRow + ")").find("td:eq(" + currentIndex + ")");
    var cellLocation = getNextinvalidCell(currentIndexRow, currentIndex);
    if (cellLocation.length > 0) {
        var rowNumber = cellLocation[0].rowIndex;
        var cellNumber = cellLocation[0].cellIndex;

        eCell = $("#Grid").children().find("tbody").find("tr:eq(" + rowNumber + ")").find("td:eq(" + cellNumber + ")");
    }

    setTimeout(function () {
        var grid = $("#Grid").data("kendoGrid");
        if (eCell != null) {
            grid.editCell(eCell);
            eCell.select();
        }

    }, 10);

}



Following function used array called “InvalidCells”, when updating cells with results save invalid cells in array use that array to determine nearest invalid cell in the grid.

function getNextinvalidCell(currentIndexRow, currentIndex) {

    //Current row contains another invalid cell

    var cellLocation = [];

    var invalidCell = $.grep(invalidCells, function (cell) {

        return cell.rowIndex == currentIndexRow && cell.cellIndex > currentIndex;

    });

    if (invalidCell.length > 0) {
        var sortedInvalidCells = invalidCell.sort(function (a, b) {

            return a.cellIndex - b.cellIndex;

        });
        cellLocation.push({ rowIndex: sortedInvalidCells[0].rowIndex, cellIndex: sortedInvalidCells[0].cellIndex })
    }

return cellLocation;

}