Monday, 25 September 2017

expressjs API with tedious and SQL server connection

In below example I have used node.js, express js and tedious.
for more details about tedious go to tedious

/*
* Install below packages using node package manager
* 1) $ npm install express --save
* 2) $ npm install tedious --save
*
* Run Application using
* $ node index.js
/
                (function () {
    var express = require('express')
    var app = express()
    
    app.get('/', function (req, res) {
    
        // tedious package is used for SQL server connection
        var Connection = require('tedious').Connection;
        var Request = require('tedious').Request;
    
        // add SQL server configurations here
        var config = {
            server: '[server_name]',
            userName: '[user_name]',
            password: '[password]',        
            options: {            
                instanceName: '[instance_name]',
                database: '[database_name]'            
            }
        }
    
        var connection = new Connection(config);
    
        connection.on('connect', function (err,resp) {
            if (err) {
                console.log(err);
            } else {
                executeStatement(resp);            
            }
        });
    
        function executeStatement(resp) {
            // add SQL query here
            request = new Request('select * from [table_name] with (nolock)', function (err, rowCount) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(rowCount + ' rows');
                }
                connection.close();
                // return rowCount from API
                res.json(rowCount);
            });
    
            // row iteratore for result
            request.on('row', function (columns) {
                columns.forEach(function (column) {
                    if (column.value === null) {
                        console.log('NULL');
                    } else {
                        console.log(column.value);
                    }
                });
            });
            
            // execute query
            connection.execSql(request);        
        }
    });
    
    var server = app.listen(5000, function () {
        console.log('Server is running..');
    });
})();

Tuesday, 19 September 2017

Best Loader in Angularjs

Add Loader to your application using http request interceptor.
Here for Loader will continue till any of your http request is running.
As soon as all requests are return, loader will disapear.
Here we have used Interceptors for HTTP requests. For more details visit here

index.html

<div ng-class="{'modal-color': isShowLoader}"></div>
<!-- add background modal color-->
<div ng-class="{'loader': isShowLoader}"></div>
<!-- add loader-->


style.css

Add style for classes .loader and .modal-color for laoder design.

app.controller.js

$rootScope.$on('loading:progress', function () {
// show loading gif
  $scope.isShowLoader = true;
});

$rootScope.$on('loading:finish', function () {
// hide loading gif
  $scope.isShowLoader = false;
});


app.interceptor.js

app.factory('httpRequestInterceptor', ['$rootScope', '$q', function ($rootScope, $q) {

var loadingCount = 0;

return {

  request: function (config) {
   if (++loadingCount === 1) {
    $rootScope.$broadcast('loading:progress');
   }
   return config || $q.when(config);
  },

  response: function (response) {
   if (--loadingCount === 0) {
    $rootScope.$broadcast('loading:finish');
   }
   return response || $q.when(response);
  },

  responseError: function (response) {
   if (--loadingCount === 0) {
    $rootScope.$broadcast('loading:finish');
   }
   return $q.reject(response);
  }
 };
}]);

Parent-Child class declaration and initialization

using System; namespace CSharpDemo {     public class A     {         public void print()         {             Console.Wr...