Error "Object reference not set to an instance of an object." for SqlConnection

166 views Asked by At
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TropicalConnectionString"].ConnectionString);  

It said

Object reference not set to an instance of an object.

In order to check the error, I gave my app.config, and I want to know is it possible I gave the wrong name for Initial Catalog etc?

Here is my app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="TropicalConnectionString" 
             connectionString="Data Source = JIRAN-PC; Initial Catalog = TropicalServer; Integrated Security = True"
             providerName="System.Data.SqlClient" />
        <add name="TropicalServiceWinApp.Properties.Settings.TropicalConnectionString"
             connectionString="Data Source=JIRAN-PC;Initial Catalog=TropicalServer;Integrated Security=True"
             providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ITropicalService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:5483/TropicalService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITropicalService"
                contract="TropicalServiceRef.ITropicalService" name="BasicHttpBinding_ITropicalService" />
        </client>
    </system.serviceModel>
</configuration>
1

There are 1 answers

0
laskdjf On BEST ANSWER

Try this:

  1. Select the App.Config file in the solution explorer
  2. In the property window select Copy to Output Directory to Copy Always.
  3. Now Build the application and try again.