Latest Posts

Sunday, June 21, 2015

Angular Js $index, $odd and $even with ng-if directive

<!DOCTYPE html>
<html>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

    <div ng-app="myApp" ng-controller="customersCtrl">

        <table>
            <tr ng-repeat="x in names">
                <td ng-if="$odd" style="background-color:#a1a1a1; color:#ffffff">
                    {{$index + 1  }}
                </td>
                <td ng-if="$even">
                    {{ $index + 1  }}
                </td>
                <td ng-if="$odd" style="background-color:#a1a1a1; color:#ffffff">
                    {{ x.Name }}
                </td>
                <td ng-if="$even">
                    {{ x.Name }}
                </td>
                <td ng-if="$odd" style="background-color:#a1a1a1; color:#ffffff">
                    {{ x.Country }}
                </td>
                <td ng-if="$even">
                    {{ x.Country }}
                </td>
            </tr>
        </table>

    </div>

    <script>
        angular.module('myApp', [])
           .factory('customersService', function ($http, $q) {
               var deferred = $q.defer();

               return {
                   getPerson: function () {
                       return $http.get("/api/Home/getperson")
                           .then(function (response) {
                               // promise is fulfilled
                               deferred.resolve(response);
                               return deferred.promise;
                           }, function (response) {
                               deferred.reject(response);
                               return deferred.promise;
                           })
                       ;
                   }
               }

           })
           .controller("customersCtrl", function ($scope, $q, customersService) {

               customersService.getPerson().then(
                       function (result) {
                           $scope.names = result.data;
                       },
                       function (error) {
                           // handle errors here
                           alert('error');
                       }
                   );
           });

    </script>

</body>
</html>

Tuesday, June 16, 2015

Angular Js Factory and $q

<!DOCTYPE html>
<html>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

    <div ng-app="myApp" ng-controller="customersCtrl">

        <ul>
            <li ng-repeat="x in names">
                {{ x.Name + ', ' + x.Country }}
            </li>
        </ul>

    </div>

    <script>
        angular.module('myApp', [])
           .factory('customersService', function ($http, $q) {
               var deferred = $q.defer();

               return {
                   getPerson: function () {
                       return $http.get("/api/Home/getperson")
                           .then(function (response) {
                               // promise is fulfilled
                               deferred.resolve(response);
                               return deferred.promise;
                           }, function (response) {
                                deferred.reject(response);
                              return deferred.promise;
                           })
                       ;
                   }
               }

           })
           .controller("customersCtrl", function ($scope, $q, customersService) {

               customersService.getPerson().then(
                       function (result) {
                           $scope.names = result.data;
                       },
                       function (error) {
                           // handle errors here
                           alert('error');
                       }
                   );
           });

    </script>

</body>
</html>

Friday, June 12, 2015

Retrieve Data in Angular Js from Web Api as HttpResponseMessage

<!DOCTYPE html>
<html>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

    <div ng-app="myApp" ng-controller="customersCtrl">

        <ul>
            <li ng-repeat="x in names">
                {{ x.Name + ', ' + x.Country }}
            </li>
        </ul>

    </div>

    <script>
        var app = angular.module('myApp', []);
        app.controller('customersCtrl', function ($scope, $http) {
            $http.get("/api/Home/getperson").success(function (data) {
                alert(JSON.stringify(data));
                $scope.names = data;
            });
        });
    </script>

</body>
</html>






    public class records
    {
        public string Name { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }
    public class HomeController : ApiController
    {
        public HttpResponseMessage getperson()
        {
            var data = new List();
            records item1 = new records();
            item1.Name = "Name1";
            item1.City = "City1";
            item1.Country = "Country1";
            data.Add(item1);
            records item2 = new records();
            item2.Name = "Name2";
            item2.City = "City2";
            item2.Country = "Country2";
            data.Add(item2);
            records item3 = new records();
            item3.Name = "Name3";
            item3.City = "City3";
            item3.Country = "Country3";
            data.Add(item3);

            return Request.CreateResponse(HttpStatusCode.OK, data);
        }
    }

Thursday, June 11, 2015

Angular Js Upper Case And Lower Case

<!DOCTYPE html>
<html>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

    <div ng-app="myApp" ng-controller="personCtrl">

        <p>The name is {{ firstName | uppercase }}</p>
        <p>The name is {{ lastName | lowercase }}</p>

    </div>

    <script type="text/javascript">
        angular.module('myApp', []).controller('personCtrl', function ($scope) {
            $scope.firstName = "jobin";
            $scope.lastName = "jonny";
        });
    </script>
</body>
</html>

Monday, April 6, 2015

File Download using Java Script


<script>
        function saveTextAsFile() {
            var textToWrite = document.getElementById("inputTextToSave").value;
            var textFileAsBlob = new Blob([textToWrite], { type: 'text/plain' });
            var fileNameToSaveAs = "myNewFile.txt";
            var downloadLink = document.createElement("a");
            downloadLink.download = fileNameToSaveAs;
            downloadLink.innerHTML = "My Hidden Link";
            window.URL = window.URL || window.webkitURL;
            downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
            downloadLink.onclick = destroyClickedElement;
            downloadLink.style.display = "none";
            document.body.appendChild(downloadLink);
            downloadLink.click();
        }

        function destroyClickedElement(event) {
            document.body.removeChild(event.target);
        }
    </script>
</head>
<body>
    <textarea id="inputTextToSave" style="width:512px;height:256px"></textarea>
    <button onclick="saveTextAsFile()">Save Text to File</button>
</body>

Saturday, March 28, 2015

Encryption and Decryption in javascript and c#

javascript
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js "></script>
    <script type="text/javascript">

        function SubmitsEncry() {

            debugger;
            var txtUserName = "Please enter UserName";

            var key = CryptoJS.enc.Utf8.parse('8080808080808080');
            var iv = CryptoJS.enc.Utf8.parse('8080808080808080');

            var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(txtUserName), key,
            {
                keySize: 128 / 8,
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
            var decrypted = CryptoJS.AES.decrypt(encrypted, key,
               {
                   keySize: 128 / 8,
                   iv: iv,
                   mode: CryptoJS.mode.CBC,
                   padding: CryptoJS.pad.Pkcs7
               });
            alert(decrypted.toString(CryptoJS.enc.Utf8));
        }
        SubmitsEncry();

    </script>

c#

 public ActionResult Index()
        {
            string s = "The brown fox jumped over the green frog";
            //string k = "urieurut";
            var encry = EncryptStringAES(s);
            var sss = DecryptStringAES(encry);
            return View();
        }
        public static string DecryptStringAES(string cipherText)
        {
            var keybytes = Encoding.UTF8.GetBytes("8080808080808080");
            var iv = Encoding.UTF8.GetBytes("8080808080808080");

            var encrypted = Convert.FromBase64String(cipherText);
            var decriptedFromJavascript = DecryptStringFromBytes(encrypted, keybytes, iv);
            return string.Format(decriptedFromJavascript);
        } 
        private static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv)
        {
            // Check arguments.  
            if (cipherText == null || cipherText.Length <= 0)
            {
                throw new ArgumentNullException("cipherText");
            }
            if (key == null || key.Length <= 0)
            {
                throw new ArgumentNullException("key");
            }
            if (iv == null || iv.Length <= 0)
            {
                throw new ArgumentNullException("key");
            }

            // Declare the string used to hold  
            // the decrypted text.  
            string plaintext = null;

            // Create an RijndaelManaged object  
            // with the specified key and IV.  
            using (var rijAlg = new RijndaelManaged())
            {
                //Settings  
                rijAlg.Mode = CipherMode.CBC;
                rijAlg.Padding = PaddingMode.PKCS7;
                rijAlg.FeedbackSize = 128;

                rijAlg.Key = key;
                rijAlg.IV = iv;

                // Create a decrytor to perform the stream transform.  
                var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                try
                {
                    // Create the streams used for decryption.  
                    using (var msDecrypt = new MemoryStream(cipherText))
                    {
                        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {

                            using (var srDecrypt = new StreamReader(csDecrypt))
                            {
                                // Read the decrypted bytes from the decrypting stream  
                                // and place them in a string.  
                                plaintext = srDecrypt.ReadToEnd();

                            }

                        }
                    }
                }
                catch
                {
                    plaintext = "keyError";
                }
            }

            return plaintext;
        }

        public static string EncryptStringAES(string plainText)
        {
            var keybytes = Encoding.UTF8.GetBytes("8080808080808080");
            var iv = Encoding.UTF8.GetBytes("8080808080808080");

            var encryoFromJavascript = EncryptStringToBytes(plainText, keybytes, iv);
            return Convert.ToBase64String(encryoFromJavascript);
        }  


        private static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv)
        {
            // Check arguments.  
            if (plainText == null || plainText.Length <= 0)
            {
                throw new ArgumentNullException("plainText");
            }
            if (key == null || key.Length <= 0)
            {
                throw new ArgumentNullException("key");
            }
            if (iv == null || iv.Length <= 0)
            {
                throw new ArgumentNullException("key");
            }
            byte[] encrypted;
            // Create a RijndaelManaged object  
            // with the specified key and IV.  
            using (var rijAlg = new RijndaelManaged())
            {
                rijAlg.Mode = CipherMode.CBC;
                rijAlg.Padding = PaddingMode.PKCS7;
                rijAlg.FeedbackSize = 128;

                rijAlg.Key = key;
                rijAlg.IV = iv;

                // Create a decrytor to perform the stream transform.  
                var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.  
                using (var msEncrypt = new MemoryStream())
                {
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (var swEncrypt = new StreamWriter(csEncrypt))
                        {
                            //Write all data to the stream.  
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }
            // Return the encrypted bytes from the memory stream.  
            return encrypted;
        }

Monday, January 5, 2015

Tree View Structure From DataBase

        [HttpGet]
        public List getValue(int id)
        {

            var dataList = nodeList().Where(x => x.parentID == 0).ToList();
            List nList = new List();
            foreach (var item in dataList)
            {
                node i = new node();
                i.name = item.name;
                FillChild(i, item.ID);
                nList.Add(i);
            }

            return nList;
        }

        public static List nodeList()
        {
            var nodeList = new List();
            var node1 = new nodeDB();
            node1.ID = 1;
            node1.name = "node1";
            node1.parentID = 0;
            nodeList.Add(node1);
            var node2 = new nodeDB();
            node2.ID = 2;
            node2.name = "node2";
            node2.parentID = 1;
            nodeList.Add(node2);
            var node3 = new nodeDB();
            node3.ID = 3;
            node3.name = "node3";
            node3.parentID = 2;
            nodeList.Add(node3);
            var node4 = new nodeDB();
            node4.ID = 4;
            node4.name = "node4";
            node4.parentID = 0;
            nodeList.Add(node4);
            var node5 = new nodeDB();
            node5.ID = 5;
            node5.name = "node5";
            node5.parentID = 4;
            nodeList.Add(node5);
            return nodeList;
        }
        public int FillChild(node parent, int ID)
        {
            var data = nodeList().Where(x => x.parentID == ID).ToList();
            if (data.Count > 0)
            {
                foreach (var item in data)
                {
                    node child = new node();
                    child.name = item.name;
                    int tempID = item.ID;
                    parent.nodes.Add(child);
                    FillChild(child, tempID);
                }
                return 0;
            }
            else
            {
                return 0;
            }
        }