1

Using Visual Studio 2012 which was recently installed but now I can't connect to our SQL Server database.

These are the steps I'm following

  1. create App1.config
  2. type this in App1.config:

     <?xml version="1.0" encoding="utf-8" ?>
     <configuration>
       <connectionStrings>
         <add name ="xxx" connectionString="USER ID=xx;PASSWORD=xx;PERSIST SECURITY INFO=True;Data Source=xx;Initial Catalog=xx" />
       </connectionStrings>
     </configuration>
    
  3. Add a reference to the project to System.Configuration

  4. Create access to namespaces via:

     using System.Data.SqlClient;
     using System.Data;
     using System.Configuration;
    
  5. implement the following:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.SqlClient;
    using System.Data;
    using System.Configuration;
    
    namespace ConsoleApplication10 {
        class Program {
            static void Main(string[] args) {
    
                SqlConnection conn = null;
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ConnectionString);
    
            }
        }
    }
    
  6. I've created a new console app and added the above and I still get an error NullReferenceException was unhandled Object reference not set to an instance of an object. ...

enter image description here

EDIT

Via the immediate window I determined that the following is null:

ConfigurationManager.ConnectionStrings["xxx"].ConnectionString

If I hard-code the connection string into the constructor for SqlConnection then it connects ok

What am I missing - something really obvious!! Or is this in connection with my new VS ?

21
  • 2
    Determine exactly which variable/expression was null by using the debugger. Commented Jan 23, 2013 at 12:57
  • What's null? conn or the connection string returned by the ConfigurationManager? Commented Jan 23, 2013 at 12:57
  • 1
    poor format, review you question. some parts are missing Commented Jan 23, 2013 at 12:57
  • @Cybermaxs ok - I'll determine exactly what is null and add to the OP Commented Jan 23, 2013 at 12:58
  • @ThorstenDittmar - thanks : looks like the configuration manager is returning a null string - if I hard-code the string in then it executes fine Commented Jan 23, 2013 at 13:07

5 Answers 5

3

check what is in the ConfigurationManager.ConnectionStrings by excecuting at least the following:

if (ConfigurationManager.ConnectionStrings != null ) {
    Console.WriteLine(ConfigurationManager.ConnectionStrings.Count);
    Console.WriteLine(ConfigurationManager.ConnectionStrings[0].ConnectionString);
    Console.WriteLine(ConfigurationManager.ConnectionStrings[0].Name);
    ....
} else {
    Console.WriteLine("null");
}

This will highlight any obvious problems like duplication of the App.config file which could well be the case as you mentioned App1.config in the OP.

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

7 Comments

+1 thanks for all the help - a frustrating question but if you feel kind you might like to cancel out some of the downvotes it seems to be gathering!
@whytheq I think downvote came from formatting more than from relevance of the question.
formatting looks fine - I've fully explained the problem with all code and screenshots...just edited your answer with my embarrassing finding!
You missed ConfigurationManager.ConnectionStrings
@JohnSaunders no - I added a configuration file to the solution and there was already one in there; so even though I was using the correct name I think the project was referring to the original config file. Once I deleted one of them and renamed to App.config rather than App1.config all was well in the World again !
|
3

Check your output folder.

Assuming your application is called myapp.exe, there should be myapp.exe.config.

This should exist, and should contain the contents of your app.config file in Visual Studio.

If it doesn't, check whether you already have an app.config file elsewhere (I notice you called your file app1.config)

2 Comments

Even if it doesn't exist, the default values will be used.
+1 thanks tom - exactly as you noticed app1.config was the problem. I thought this was a relevent question but already on -2 !
2

Have your tried checking what the value of ConfigurationManager.ConnectionStrings["xxx"].ConnectionString is and hardcoding with that value? Do you still get null?

Also it's advised to created the connection like this:

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ConnectionString))
{
        //code        
}

I would reply but I don't have enough rep yet ... :-(

1 Comment

let me try with it hard-coded ; can determine if it is the connection reference causing the problem then
2

Guessing from the fact that your code does not "do anything" with conn yet, I'm pretty sure that the ConfigurationManager returns null for the connection string name you pass in. An exception is thrown by the SqlConnection's constructor when passing in null instead of a valid connection string.

7 Comments

if I add code so that my code does something - like run a stored procedure than it still errors on this line of code
Yes of course! I meant: As you are not using conn in the source code you posted in your question, the only thing that can be null is the result of the ConfigurationManager call. Had there been other code actually using conn, conn could also have been null, causing the exception. But that isn't the case, so the exception must come from SqlCommands constructor.
+1 thanks for help (although I could almost see the smoke coming out of your ears!!) seems like I had duplicate app files in the solution!
I know: I could punch myself in the face!...can i edit them not manually? ...via the properties section in the solution?
Yes, the connection string is a setting.
|
0

When you place your connection strings in Properties->Settings, as a connection string, access them with full namespace, and your done.

See my answer here

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.