Sunday, November 10, 2013

Instance Failure

This rather "Strange and UnCommon" error comes up mostly while connecting do database via your C# application.

Reason: - In your connection string, when there is a escape sequence (\), C# converts it to "\\" so that it could be treated the way it needs to.

Therefore if you provide a connection string as "DataSource=ServerName\SQL", c# automatically converts it to  "DataSource=ServerName\\SQL" and reads it the way it needs to.

However, if we actually provide the string something like  "DataSource=ServerName\\SQL" (With Double Back Slashes), then it raises this error causing "Instance Failure".

Solution: Use a SINGLE ( \ ) instead of Double ( \\ ).

Tuesday, July 2, 2013

Get a List of all Countries

This is a very common requirement that comes up to me in every alternate application that I make. I know there would be a lot of people like me who have the same issue.

At first I thought of consuming a ready made webservice that would give me the list of all countries.

Then upon further research, I found a very simple yet effective solution to my problem:

Here is a function that returns a List of Country Names.

public static List GetCountryList()
        {
            List _CultureList = new List();
            CultureInfo[] _CultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            foreach (CultureInfo _Culture in _CultureInfo)
            {
                RegionInfo _RegionInfo = new RegionInfo(_Culture.LCID);
                if (!(_CultureList.Contains(_RegionInfo.EnglishName)))
                {
                    _CultureList.Add(_RegionInfo.EnglishName);
                }
            }
            _CultureList.Sort();
            return _CultureList;
        }

Tuesday, March 5, 2013

Only parameterless constructors and initializers are supported in LINQ to Entities

This is the error that usually come up when you try to run a LINQ Query and return an object out of this QUERY.

Something like the below example:
var query = (from c in ctx.Clients
                         where l.UserID == userid
                         select new ClientList(c.ClientFirstName, c.ClientLastName,  c.ClientEmail)
                          ).ToList();
Well the Error is Pretty much self explanatory. No Parameterized constructors are allowed for LINQ to Entities...

Workaround ?
Change your Query to something like this..
var query = (from c in ctx.Clients            
                         where l.UserID == userid
                         select new ClientList{ClientFirstName= c.ClientFirstName, ClientLastName= c.ClientLastName, ClientEmail= c.ClientEmail}
                          ).ToList();