-9

When I try

var con = new SqlConnection(
                  ConfigurationManager
                  .ConnectionStrings["RegConnectionString"]
                  .ConnectionString);

I get the following error:

Error message-null reference exception object reference not set to an instance of an object

What is going wrong?

user1240912
  • 29
  • 1
  • 2
  • 1
    You connection string is probably not defined in web.config or is named something else. – Candide Mar 09 '12 at 19:39
  • 1
    well looks like there is no connection string named `RegConnectionString` defined in your config file? – BrokenGlass Mar 09 '12 at 19:39
  • (assuming this is what you want to know, since your 'question' doesn't have an actual question, just a line of code and an exception message, I think that) The ConfigurationManager doesn't seem to have found your connection string. – MilkyWayJoe Mar 09 '12 at 19:41
  • google: "**connection string webconfig**" - you'll surely find the answer! – Alex Mar 09 '12 at 19:43
  • Whatever happened to requiring that a question actually be asked? – Bob Horn Mar 09 '12 at 19:45
  • @BobHorn: Good point. But then, that's why anyone can [edit] to improve. –  Mar 09 '12 at 20:20

3 Answers3

3

Check for a mis-spelled or missing Configuration setting.

99.99999% of the time this error happens on this line of code, the issue is that the SqlConnecton setting in the Web.Config (or app.config) is either missing or mis-spelled.

(Actually, in my experience it's 100% of the time, but you never know. There are exceptions to darn near everything.)

From here:

Connection string in .NET 3.5 (and above) config file

Do not use appsettings in web.config. Instead use the connectionStrings section in web.config.

<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>

use the ConfigurationSettings class. string connStr =

ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

So YOUR web.config should contain

<add name="RegConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />

Of course, you'll need to replace the server name, database name, username, password, etc...

David
  • 72,686
  • 18
  • 132
  • 173
3

Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <!-- snip -->
    <connectionStrings>
        <add name="RegConnectionString"
             connectionString="Put your connex string here drrr" />
</connectionStrings>
2

You are probably missing the RegConnectionString connection string in your web.config.

Here's an example. See MSDN for more information.

<configuration>
<!-- Other configuration settings -->
 <connectionStrings>    
  <add name="RegConnectionString" 
       providerName="System.Data.SqlClient" 
       connectionString="server=.;database=NorthWind;Integrated Security=SSPI" />    
 </connectionStrings>    
<!-- Other configuration settings -->
</configuration>
jrummell
  • 42,637
  • 17
  • 112
  • 171