Monthly Archives: November 2018

Retrieving Items from a List in a Model

Let’s look at how you would get the List<Book> Books from the Author model. We’ll use Author’s Detail View and then extract that to a Partial View. A common error is to get a null pointer exception here:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

This is fixed by adding Virtual to the Books property. Now, when we go to the Author’s Detail page, the Books property is instantiated and the list can be displayed.

List Books

Using the Code First Approach with Entity Framework

Using Entity Framework, we can take a Code First Approach developing the data models and allow EF to translate the model classes into database tables. The properties of classes become the attributes of the tables. Defining an ID for a class will result in a primary key and referencing that ID from another class will create a foreign key relationship. The first step is to import EF to the application.

Import Entity Framwork


It will be useful to create Controllers and Views from the Models. With a DbContext Model in place: run build, then enable migrations from the PM Console, add an initial migration and run update-database. At this point, EF creates the database in LocalDb. See the initial create file in the Migrations folder for the operations run on update. Repeat with add-migration [name] whenever the models change and update-database.

Connect LocalDb

Further control can be gained by defining the connection string in the Web.config file and modify the Configuration.cs. For example, seeding the database with preliminary data. The connection string is also the place to attach the DB if you want to see it in the App_Data folder. It will appear after building and executing the app.

App Data mdf

Storing Values in App.config

Storing values in the App.config file will allow others to update values without needing to access the source code and rebuilding the project. For example, when delivering a solution to another team which may need to change variables such as url, username or emails, then building the project with this in mind and retrieving values from App.config will allow them to easily make those changes.

  1. Add references and using statements to System.Configuration
  2. Add a key value pair to App.config
  3. Get the value with ConfigurationManager.AppSettings
  4. Edit the key value pair from the XML file MyApp.exe.config

On editing the config file, there will be no need to recompile the executable, but the values will be updated.

Add References

Using Configuration



Sending Email from .NET using Gmail SMTP

I automate lots of jobs to run daily. Mostly just getting Task Scheduler to run a bat file or python script and I can just check from my desktop if something failed and re-run it. However, if I am in .NET and something is going to run from another server that I won’t necessarily be able to access whenever I want, receiving an email that a job succeeded or failed would be nice. I am hoping for a more permanent solution, but in pinch, a gmail account will work.


Connect to Oracle Data Source from .NET – A Console App

In order to connect to an Oracle database from .NET, use the Oracle.ManagedDataAccess package, otherwise called ODP.NET. It can be downloaded with NuGet by searching “oracle.”


You can get started easily with defining a connection string. Later, you can put this into App.Config with different Data Sources for Testing and Production environments. Don’t forget the using statement.