Tuesday, June 24, 2014

Error Logging in a File

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;

namespace DEParser.Logger
{
    public class FileLogger
    {
        #region Public Functions

        public  void Log(string exception)
        {
            string date = DateTime.Today.ToString("yyyy-MM-dd");
            String FilePath = ConfigurationManager.AppSettings["logPath"] + date + @"-ErorLog.txt";
            bool fileExists = File.Exists(FilePath);
            if (fileExists)
            {
                using (StreamWriter w = File.AppendText(FilePath))
                {
                    MakeLog(exception, w);
                }
            }
            else
            {
                using (StreamWriter w = File.CreateText(FilePath))
                {
                    MakeLog(exception, w);
                }
            }
           
            //using (StreamReader r = File.OpenText(ConfigurationSetting.LOG_FILEPATH))
            //{
            //    DumpLog(r);
            //}
        }

        #endregion

        #region Private Functions

        private void MakeLog(string logMessage, TextWriter w)
        {
            w.Write("\r\nLog Entry : ");
            w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());
            w.WriteLine("  :");
            w.WriteLine("  :{0}", logMessage);
            w.WriteLine("-------------------------------");
        }

        private void DumpLog(StreamReader r)
        {
            string line;
            while ((line = r.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }

        #endregion
    }

}


try
                        {
                           
                        }
                        catch (Exception ex)
                        {
                           
                            new Logger.FileLogger().Log(ex.ToString());
                        }

Wednesday, June 11, 2014

Validation in Angular JS

For validation in Angular Js, first of all use 'required' attribute in the input control like..

Now restrict the save button so that it is clickable only when all the validation succeeds. Use  
ng-disabled="myform.$invalid" where myform is the name of the form. Placing the input controls and save button under form tag is must

Doing this is sufficient to validate the form. If you want to give the user a required message then use a span like this..



So * will be shown when you leave a required field empty


One thing to remember is that for this required message will be shown only when the input control (textbox) should have a name attribute and name in 'ng-show' attribute of the span should also be there.


Wednesday, June 4, 2014

Binding to an Array

What if you want to bind to an array and have html elements repeated for each item in the array? This is easy with ng-repeat. Before that we need to initialize some data in our model. Although we’d do this in a controller, but as an initial demo we’re going to use the rarely used ng-init directive to add an array to our model. ng-init lets you execute angular expressions when the page first loads. Doing this with controller will be discussed after this

Advanced Tangent: Notice that we have a “user” on our model from the Hello World sample above, and yet inside the ng-repeat, the correct “user” object–from the users array–was used even though there was some ambiguous use of the term user. This is because the ng-model directive uses it’s own isolated scope. Don’t worry too much about this, just keep it in the back of your mind.

First Step to AngularJs

Just Create a simple Html page like the following..


<html>

<head>
<script src="~/Scripts/Angularjs/angular.min.js"></script>
</head>
<body>
<form>
<input type="number" required />
<input type="submit" />
Hello {{1 + 1}} the World!
</form>
</body>
</html>

If u see this code in browser then you will see the output like







Now if u add  ng-app anywhere so that the Hello {{1 + 1}} the World! becomes the child of that element, what happens then?

There is your first Angular magic! You can see that the {{1 + 1}} expression got evaluated and replaced with “2″. So, what happened here is that the page was downloaded and then Angular scanned the page for ng-app and processed any expressions that it found (in this case just the {{1 + 1}} expression) and replaced the content with the evaluated expression. If you would like to prove to yourself that the location of ng-app matters, try moving ng-app onto a div that does not include the {{1 + 1}} expression and you will see that the expression does not get evaluated (but any expressions inside the ng-app div do).


 
<body ng-app>
    <form>
         <input type="number" required />
         <input type="submit" />
         Hello {{1 + 1}} the World!
   </form>
</body>

and the output is







So this is how the AngularJs works. It's pretty simple