0

My DB Connection dosnt work, can someone please tell me why?

I run this from a unit test in VS 2010. Simple Assert.IsTrue(testDbConnection) and it fails. i'v also tried running it in normal mode as a web application and I get the same exception.

please note the XXXXX are ofcourse done with purpose.

web.config

  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
    <add name="DomainDatabase" connectionString="Data Source=XXXXXXXXXX;Initial Catalog=domain;Persist Security Info=True;User ID=campain_xxx;Password=XXXXXXXXX" providerName="System.Data.SqlClient" />
  </connectionStrings>

My test method

 public bool testDBConnection()
        {
            try
            {
                SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["DomainDatabase"].ConnectionString);
                sqlCon.Open();
                sqlCon.Close();

                return true;
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
                return false;
            }

        } 

This gives me the exception

System.NullReferenceException: Object reference not set to an instance of an object.
6
  • Don't know if it matters but connectionString is spelled with a lower-case 'c' in your configuration and an upper-case 'c' in the test code. Commented May 26, 2011 at 9:03
  • Where do you exactly get the exception ? Commented May 26, 2011 at 9:05
  • I run this from a unit test in VS 2010. Simple Assert.IsTrue(testDbConnection) Commented May 26, 2011 at 9:08
  • 2
    You said web.config? VS 2010 test suite unit test would be using an app.config........ Commented May 26, 2011 at 9:10
  • Now I also tried it by simply running my web application, still fails. Same error Commented May 26, 2011 at 9:17

2 Answers 2

2

As neither the tests or the web application is working, it's likely that the problem you are seeing is because the configuration is not being loaded. I suspect that the following...

ConfigurationManager.ConnectionStrings["DomainDatabase"]

...returns null. The NullReferenceException is thrown when you attempt to access the ConnectionString property on that null object (reference). I would verify that your configuration for connectionStrings matches the definition defined here:

You can verify that your settings aren't being loaded with the following test:

[TestMethod]
public void VerifyThatMyDatabaseConnectionStringExists()
{
    Assert.IsNotNull(ConfigurationManager.ConnectionStrings["DomainDatabase"]);
}

If this test fails, add an app.config to your test project with the proper configuration. You will know that you have solved this problem when ...you can take this pebble from my hand -- no wait, wrong reference -- this test passes.

Tip: This answer suggests you should write tests to verify that your settings are present to prevent madness.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to copy app.config file wich has connectionString into Test-project and it works!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.