Category Archives: C#

C# – Mapping Enum Types to Entity Properties in the Entity Framework Designer

Enums have implicit numeric indicies.

public enum BallType{
  Baseball = 0,
  Basketball = 1,
  Football = 2

You can change the “seed”:
Implies 1, 2, 3

public enum BallType{
 Baseball = 1,

The Entity Framework Designer maps enums to int data types.

In Entity Data Model Diagram(edmx)
– Right-click column
– Select convert to enum
– In the Add Enum Type dialog, either:
— Select reference external type, enter namespace + type, or…
— Enter Enum type name, enter Members and optional values

C# – Nuget Package Management and No-Commit Workflow

Package Manager – install files, folders, add references to third party packages supplying common functionality.

Add dependencies.
Update packages and dependencies

Tools -> Nuget Package Manager -> Package Manager Console
Install-Package elmah
Update-Package elmah

package.config file keeps track of packages, dependencies and versions.

Nuget No-Commit Workflow
To restore deleted packages at compile:

Solution Explorer -> Right-Click Solution -> Enable NuGet Package Restore

Adds a .nuget folder in the project.

Or, go to the Manage Nuget Packages dialog and click the Restore button at the top (yellow area)

C# – Creating a New Instance of Entity and Persisting to the Database

var customer = new Customer();

//Populate properties of customer

Filtering DbSets  using LINQ method syntax:

ACMEEntities db = new ACMEEntities();
var dbCustomers = dbCustomers.OrderBy(p => p.Name).ToList();

Lambda expression – “mini methods”

Using a Tools- or Maintenance-Centric Approach to Building a Database Application

Tools-Centric approach / workflow = use Visual Studio’s designers, tools, etc. to build applications w/ minimal code.

Great for small, departmental apps with little business logic, change is not anticipated and there is a tight frame.

In Contrary, Maintenance-Centric approach  / workflow = Anticipate change, mitigate it’s negative effects on software by separating concerns, applying unit testing etc.

Great for larger, enterprise scale apps with many business rules, where change is anticipated because it is crucial  to the operation of the business and there is a longer development timeframe.

DTO – Data Transfer Object — model used for transferring from one layer to another to avoid a leaky abstraction. Ex., I do not want Entity Framework leaking out of persistence because other layers would be dependent on it.

C# – Displaying the DbSet Result in an ASP.Net Gridview

Gridview server control – Databinds to enumerable collections of objects and renders in a tabular format.

Must call ToList() on DbSet to bind to a databound control.

gridControl.DataSource = dbCustomers.ToList();

Implementing a Button Command in a GriedView
Click Chevron -> GridView Taks -> Edit Columns…

BoundField – Databind to a object property
ButtonField – Hyperlink Button

Handle button click in the GridView_RowCommand event handler.

protected void GriedView1_RowCommand(object sender, GridViewCommandEventArgs e){
  //Retrieve the ROW CLICKED in the grid
  int index = Convert.ToInt32(e.CommandArgument);
  GridView1 row = GridView1.Rows[index]

  //Accessing the cells is risky because the order of the columns may change
  //over time and you might forget that this depends on it. Also ..... 0 based!
  var someValue = row.Cells[1].Text;


C# – Creating a Database and an Entity Model in Visual Studio 2013

Creating Database
LocalDb – Local dev-only version of SQL Server
Project -> Add New Item…. -> Installed -> Data -> Sql Server Database

Createa an .mdf file
SQL Server Data Tools (SSDT) -> Tools to create and manage SQL Server databases from Visual Studio.

Creating an Entity Model
Entity Data Model – Object Relational Mapper to treat database tables w/columns as classes w/ properties.

Project -> Add New Item…. -> Installed -> Data -> ADO.Net Entity Data Model -> Entity Data Model Wizard -> EF Designer from Database -> Connection -> Database Objects

DbContext == Handle to the entity model > database
DbSet == Collection of all entities in the DBContext

CarsEntities db = new CarEntities();
var dbCustomers = db.Customers;


C# – Understanding Global Exception Handling

Unhandled exception bubble up until they are exposed to the end user (aka “yellow screen of death”)

Best place to handle exception is the nearest locale to the exception itself. However, You CAN handlig globally.

In global.asax

void Application_Error(object sender, EventArgs e){

//What just happened?

Exception ex = Server.GetLastError();

//ex will always be of type HttpUnhandledException.

//To get to the exception that CAUSED that to happen

//you will need to look at the ex.InnerException

var innerException = ex.InnerException;

//Handle a specific type of Error differently.

if(innerException.GetType() == typeof(ArgumentOutOfRangeException){



//You must do this if you want to hide the yellow page of death.... any existing exceptions after this point will send the end user to exception page.




Understanding Custom Exceptions

Inherit from Exception like so:

public class myCustomException : Exception{


« Older Entries