Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5958167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:30:15+00:00 2026-05-22T18:30:15+00:00

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

  • 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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:30:16+00:00Added an answer on May 22, 2026 at 6:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone tell me why the following doesnt work please. I am expecting the
can anyone tell me why this doesnt work? public class GeocodeCoord { public string
MySQL connection in PHP can be established in two ways: $mysql = new mysqli('localhost',
My Connection to my test application keeps terminating in the same place without any
This is how my connection is set: Connection conn = DriverManager.getConnection(url + dbName +
How do I read connection strings from custom config file (say abc.config) using WebConfigurationManager
I've checked the connection string (I got it from the server explorer). I've checked
Possible Duplicate: Close mysql connection (PHP) Please help me, I not know where add
I've written a simple console application using windows sockets to work as a proxy
In this particular case I need to run .complicated NET application for Linux. By

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.