How do I exit a WPF application programmatically?

To exit your application you can call System.Windows.Application.Current.Shutdown(); As described in the documentation to the Application.Shutdown method you can also modify the shutdown behavior of your application by specifying a ShutdownMode: Shutdown is implicitly called by Windows Presentation Foundation (WPF) in the following situations: When ShutdownMode is set to OnLastWindowClose. When the ShutdownMode is set … Read more

Associating enums with strings in C#

I like to use properties in a class instead of methods, since they look more enum-like. Here’s an example for a Logger: public class LogCategory { private LogCategory(string value) { Value = value; } public string Value { get; private set; } public static LogCategory Trace { get { return new LogCategory(“Trace”); } } public … Read more

How to build a query string for a URL in C#?

You can create a new writeable instance of HttpValueCollection by calling System.Web.HttpUtility.ParseQueryString(string.Empty), and then use it as any NameValueCollection. Once you have added the values you want, you can call ToString on the collection to get a query string, as follows: NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(string.Empty); queryString.Add(“key1”, “value1”); queryString.Add(“key2”, “value2”); return queryString.ToString(); // Returns “key1=value1&key2=value2”, all … Read more

Unable to update the EntitySet – because it has a DefiningQuery and no element exist

It usually happens because one of the following reasons: Entity Set is mapped from Database view A custom Database query Database table doesn’t have a primary key After doing so, you may still need to update in the Entity Framework designer (or alternatively delete the entity and then add it) before you stop getting the … Read more

LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria

If your result set returns 0 records: SingleOrDefault returns the default value for the type (e.g. default for int is 0) FirstOrDefault returns the default value for the type If you result set returns 1 record: SingleOrDefault returns that record FirstOrDefault returns that record If your result set returns many records: SingleOrDefault throws an exception … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)