Monday, June 30, 2014

What Makes a Good Programmer?


Thursday, June 26, 2014

How to implement Dependency Injection by Autofac in MvcApplication class (ASP.NET MVC Global.asax)?

There are a lot of code examples for how to implement Dependency Injection for controller in ASP.NET MVC, but not too much about MvcApplication class in ASP.NET MVC.

Following is my implementation for Dependency Injection in MvcApplication class

public class MvcApplication : System.Web.HttpApplication

        protected void Application_Start()
            //pass service into other class that need this service
            var c = new OtherClassNeedService(_IYourService);

        private AutofacDependencyResolver IoCcontainer { get; set; }
        private IYourService _IYourService
                if (IoCcontainer == null) return null;
                return IoCcontainer.ApplicationContainer.Resolve();

        public void RegisterDependencies()
            var builder = new ContainerBuilder();

                .As(); //.InstancePerHttpRequest(); if added InstancePerHttpRequest, will get error: No scope with a Tag matching 'httpRequest' is visible from the scope in which the instance was requested.

            IContainer container = builder.Build();
            var aResolver = new AutofacDependencyResolver(container);
            IoCcontainer = aResolver;

        protected void Application_End(object sender, EventArgs e)

Thursday, June 19, 2014

How to get dropdown list from html by Html Agility Pack

private List GetDropdownList(string HtmlSource, string selectName)
            HtmlDocument doc = new HtmlDocument();
            return doc.DocumentNode.SelectNodes(string.Format("//select[@name='{0}']//option", selectName)).Select(v => v.Attributes["value"].Value).ToList();
Need to add HtmlNode.ElementsFlags.Remove("option");
before loading html source.

Wednesday, June 18, 2014

How to solve, Entity Framework return duplicated records from a view if there is no key in the view

1. Make sure the view should be a key there. And marked as Entity key
2. If do not have a key, add one by Row number

Do not forget: marked that field as Entity key in Edmx file


Monday, June 16, 2014

How to solve problem, An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.

When saw this error, go to web.config file, disable the customer error. Then you will see the real error.   Set to RemoteOnly if in the production environment, so only real error in local.
<customErrors mode="Off" >


Friday, June 6, 2014

How to get list of div that contains specific class name in Html Agility?

            var DivListContainSpeciicClassName = htmlDoc.DocumentNode.Descendants("div").Where(
                d => d.Attributes.Contains("class")

Wednesday, June 4, 2014

How to kill all current connections to a database in SQL Server ?

In case of you want to restore an database.

There are many ways to do so.
See link on Stackoverflow

Or there is another simple way:
1. In SQL Server studio, click Activity Monitor button
2. Filter by database
3. Right click to kill all of connection to that database

Monday, June 2, 2014

How to solve problem, Invalid column name 'Discriminator'?

Get this error while upgrade Entity Framework from 5.0 to 6.x.
Install Entity Framework by Nuget. The version got is 6.1

Then get this weird error: Invalid column name 'Discriminator'
All of fields for that table are in the database table, so [NotMapped] is not applicable in here

The solution to me is to revert to 6.02, problem solved.

How to solve problem, "No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'."

Entity framework is using older version 5.0 and on .Net 4.0, need to install a new one.

Go to Package Manager Console:
Install-Package EntityFramework