[Last Call] Learn how to a build a cloud-first strategyRegister Now


NullReferenceException when using DAAB

Posted on 2006-05-01
Medium Priority
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
  • 3
  • 2
LVL 35

Expert Comment

ID: 16578710

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

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

Author Comment

ID: 16585254
 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

ID: 16585333
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.

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 35

Expert Comment

ID: 16586783
Glad you got it working.
LVL 35

Expert Comment

ID: 16817617
I would PAQ/Refund this.

Accepted Solution

GranMod earned 0 total points
ID: 16968203
PAQed with points refunded (125)

Community Support Moderator

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

830 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