NullReferenceException when using DAAB

Posted on 2006-05-01
Last Modified: 2012-08-13
  I'm using Data Access Application Block (MS Enterprise Library). I installed it and configured my app.config file as per the instructions using the "Enterprise Library Configuration" tool. Here is what my app.config looks like:

<?xml version="1.0" encoding="utf-8"?>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=, Culture=neutral, PublicKeyToken=null" />
    <add name="MyDB"
         connectionString="Database=TestDB;Server=(local)\SQLEXPRESS;Integrated Security=SSPI;User Instance=true;"
      providerName="System.Data.SqlClient" />
  <dataConfiguration defaultDatabase="MyDB" />

Here is a call that I'm making:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Data;
using System.Configuration;

            Database db = DatabaseFactory.CreateDatabase();

However, I get a NullReferenceException on this line but the exception shows up in the "DatabaseConfigurationView.cs" file in DefaultName:

public string DefaultName
                  DatabaseSettings settings = this.DatabaseSettings;
Throws excpetion-->            string databaseName = settings.DefaultDatabase;                        
                  return databaseName;

I would appreciate any help on this. I'm learning to use DAAB for the first time so not sure what I'm doing wrong here.


Question by:rahulInSD
    LVL 35

    Expert Comment


    Database db = DatabaseFactory.CreateDatabase("MyDB");

    Then it should read the connection string out of the config file.

    Author Comment

     Thanks for the tip but I did that as well and I get the following exception:"ConfigurationErrorException was unhandled" in the same file with this message:

    "The requested database MyDB is not defined in configuration."

    Any ideas what I'm doing wrong in the configuration???


    Author Comment

    Problem solved....Actually the problem was related to how I had setup my solution. I had a ClassLibrary project which I am calling DAL (short for Data Access Layer). This DAL.dll was using the DAAB and I had use the enterprise library configuration tool to create its configuration file DAL.dll.config.

    However I was using this DAL.dll in another Windows Forms Application and I did not have anything defined in the windows application's config file. So when I was running the code, it was looking for the config section of the windows applicaiton not the DAL.dll.config.

    so to fix it all I had to do was copy the contents from the DAL.dll.config to Windows Form Application's config file. I could have used the Enterprise Library Configuration also and pointed it to my Windows Form Application to configure it.

    LVL 35

    Expert Comment

    Glad you got it working.
    LVL 35

    Expert Comment

    I would PAQ/Refund this.

    Accepted Solution

    PAQed with points refunded (125)

    Community Support Moderator

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    .net  + Moving Files in a list 6 26
    return array from dll 13 21
    Explain Unit of Work pattern 2 29
    The Using statement in C# 5 19
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now