Tuesday, 16 December 2014

Dependency Injection with Unity


Install Microsoft.Practices.Unity.dll using package manager.

Install-Package Unity -Version 3.5.1404

Create an Interface :

    Public interface ITestInterface
    {
        void Insert(string Name);
        string GetName(string PassedName);
    }

Create class :

    Public class clsTestDI : ITestInterface
    {
        public void Insert(string Name)
        {
           
        }

        public string GetName(string PassedName)
        {
            return "Hello " + PassedName;
        }
    }

Create Dependancy Injection  registrattion  & resolver class :

    using Microsoft.Practices.Unity
    using System.Web.Mvc;

    public class DIResolver
    {
        public void Initialise()
        {           
            var container = new UnityContainer();

            // Register Dependancy
            RegisterTypes(container);

            // Resolve Dependancy
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));           
        }
       
        private void RegisterTypes(UnityContainer container)
        {
            // Register Dependancy
            container.RegisterType<ITestInterface, clsTestDI>();
        }
    }

    public class UnityDependencyResolver : IDependencyResolver
    {
        readonly IUnityContainer _container;
        public UnityDependencyResolver(IUnityContainer container)
        {
            this._container = container;
        }

        public object GetService(Type serviceType)
        {
            try
            {               
                return _container.Resolve(serviceType);
            }
            catch
            {
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            try
            {
                return _container.ResolveAll(serviceType);
            }
            catch
            {
                return new List<object>();
            }
        }
    }


Register dependency on App_Start event in Global.asax.

        protected void Application_Start()
        {          
            new DIResolver().Initialise();
        }


Now you are able to get access of class’ methods without creating instance (e.g. new class () ;)

  public class ProfileController : Controller
    {
        ITestInterface _Itest;
        public ProfileController(ITestInterface ITest)
        {
            _Itest = ITest;
        }
        //
        // GET: /Profile/
        public new ActionResult Profile()
        {
            string str = _Itest.GetName("Ajay");
            return View();
        }

}

Thursday, 6 November 2014

JQuery issue after partial postback

I came to know this theory after much R&D. I was not aware why JQuery is not get fired after partial postback. Then from google, I found solution as below. We have to register JQuery events after end of partial postback.

<asp:ScriptManager ID="smngr" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="upanel" runat="server">
            <ContentTemplate>
                <asp:Button ID="btn" Text="Test" runat="server" OnClick="btn_Click" />
                <input type="button" value="check" id="btncheck" />
            </ContentTemplate>
        </asp:UpdatePanel>

<script>

        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandler)
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler)

        function beginRequestHandler() {
            var btn = document.getElementById('<%= btn.ClientID %>');
            btn.disabled = "disabled";
        }

        function endRequestHandler() {
            var btn = document.getElementById('<%= btn.ClientID %>');
            btn.disabled = "";
            register();
        }

        $(document).ready(function () {
            register();
        });

        function register()
        {
            $("#btncheck").click(function () {
                alert("Hello");
            });
        }


    </script>

Monday, 3 November 2014

Generate Excel file using C#


using Excel = Microsoft.Office.Interop.Excel;

protected void ExportToExcell(object sender, EventArgs e)
{           
            string data = null;
            int i = 0;
            int j = 0;

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

           

            for (i = 0; i <= 10; i++)
            {
                for (j = 0; j <= 5; j++)
                {
                    data = "Ajay " + i.ToString();
                    xlWorkSheet.Cells[i + 1, j + 1] = data;
                    xlWorkSheet.Cells[1, j + 1].Font.Bold = true;
                    xlWorkSheet.Cells[1, j + 1].Interior.ColorIndex = 10;
                }
            }

            xlWorkBook.SaveAs(Server.MapPath("~/Docs/") + "csharp.net-informations.xls",
           Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, 
           Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
  
}



private void releaseObject(object obj)
{
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;       
            }
            finally
            {
                GC.Collect();
            }           

}