Set Web.config to Show Error Messages

In ASP.NET web application development it is common practice to deploy to staging server before deploying it to production environment. Sometimes it is useful to show errors on staging environment and even production environment, especially after realizing that there is no errors on development environment. Most errors could be related to enviroment such as not being able to connect to database, IIS security issue, etc.

Here is how to add or change Web.config of the deployed application on staging server. Use Notepad or Notepad++ to open root Web.config, find or add the following xml tags:

To disable or hide error messages again, you can simply comment it out like below:

 

Install & Uninstall C# Windows Service

Run Command Prompt as Administrator, then to install:
C:\> %SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /i C:\CSharpWinServiceApp.exe

and to uninstall:
C:\> %SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /u C:\CSharpWinServiceApp.exe

Tip: above command can be used to install a Windows service programmatically as well in C#.

Entity Framework Code First Data Annotations by Examples

Two commonly used namespaces for Entity Framework Code First data annotation are:

[Table]

One of common use of Table data annotation schema is to specify created table name in database. Without Table data annotation, Entity Framework Code First will create a table using the class name with an ‘s’ character at the end. For below example model class without specifying Table data annotation, an Employees table will be created.

[Key]

Key data annotation is used to specify a property as primary key field of a table in database. By convension, Entity Framework Code First will try to identify a property that it can use as primary key field based on model class property name, for example if it sees first property variable name such Id or EmployeeId property name, it will use this property name as column name and set it as primary key.

[Required]

The Required data annotation specify that the field is a required field and EF Code First will set the field as a non-nullable field, i.e. NOT NULL field.

[StringLength]

StringLength attribute is used to specify the maximum number of allowed characters for the field. EF Code First will also specify field size, i.e. nvarchar(100) instead of nvarchar(MAX).

[Index]

Index attribute is used to specify the property to be an indexed field in a table. Also, StringLength attribute is required for indexed field of type string.

[NotMapped]

NotMapped attribute is used to specify that the property will be ignored or not mapped to database. NotMapped field will not be created during table creation.

 

Using EnumDropDownListFor HTML Helper

This is to explain EnumDropDownListFor HTML helper behaviour in ASP.NET MVC 5 with example code. Please remember, EnumDropDownListFor is not available in earlier version MVC, i.e. ASP.NET MVC 4 or before.

Default EnumDropDownListFor Behaviour

First, let’s say you want to create a drop down list from below employment type enum, then in your model you can create public non-nullable property of the enum type, see below code:

Then in EmployeeController Create action:

Now in your view inside html form, you can simply use EnumDropDownListFor HTML helper like this:

The HTML helper EnumDropDownListFor will generate drop down list with the following items:

  • ToBeDetermined (value = 0, selected)
  • FullTime (value = 1)
  • PartTime (value = 2)

Specify Default Selected Item

By default, if no selected item specified, enum item of value 0 will be selected. But for very common reason if you need to set a default value for the drop down list, you can create a new view model object in the controller action first, then set inital value for the property of type enum and pass the newly created model object to the razor view. For example in EmployeeController Create action:

This time EnumDropDownListFor will generate drop down list with the following items:

  • ToBeDetermined (value = 0)
  • FullTime (value = 1, selected)
  • PartTime (value = 2)

Enum Does Not Have Item of Value 0

What happens if you explicitly specify enum item values that start from 1 like this:

By default, if there enum member of value 0 does not exist, EnumDropDownListFor will automatically create a HTML drop down list with a prompt, i.e. a empty text select list item with item value of 0.

So, EnumDropDownListFor will generate drop down list with the following items:

  • [Empty Text] (value = 0, selected)
  • FullTime (value = 1)
  • PartTime (value = 2)

But if you specify default value for the property such as in Create action example above, a prompt item will not be generated, and you will have drop down list with only the following items:

  • FullTime (value = 1, selected)
  • PartTime (value = 2)