Click here to Skip to main content
15,893,564 members

Null reference exception when looking up connection string in web.config

DINESH K MAURYA asked:

Open original thread
Dear all

following error occored.


Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 23: 
Line 24:         //Database connection strings
Line 25:         public static readonly string ConnectionString_Synergy = ConfigurationManager.AppSettings["LocalMySqlServer"].ToString();
Line 26:         public static readonly string ConnectionString_Hospital = ConfigurationManager.AppSettings["DefaultConnectionString"].ToString();
Line 27: 


Source File: c:\inetpub\vhosts\synergyactiongroup.com\httpdocs\App_Code\CSharp\SQLHelper.cs    Line: 25



below is web.config code


XML
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Note: As an alternative to hand editing this file you can use the
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in
    machine.config.comments usually located in
    \Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
  <configSections>
    <!--<section name="urlrewritingnet" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter" requirePermission="false"/>-->
  </configSections>
  <connectionStrings>
    <remove name="LocalMySqlServer" />
    <add name="LocalMySqlServer" connectionString=" Data Source=XXXX;Database=synergya_shoes;User ID=synergyaction;Password=XXX" />
    <add name="DefaultConnectionString" connectionString="Data Source=XXXXX;Database=synergya_shoes;User ID=synergyshoes12345;Password=XXXX" />
    <add name="hospitalConnectionString" connectionString="Data Source=XXXX;Database=synergya_shoes;User ID=synergyshoes12345;Password=XXXX" />
    <add name="hospitalConnectionString2" connectionString="Data Source=111.118.213.110;Database=synergya_shoes;User ID=synergyshoes12345;Password=password@1" />
  </connectionStrings>
  <!--<urlrewritingnet configSource="UrlRewriting.config"/>-->
  <system.web>
    <customErrors mode="Off" />
    <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
    <compilation debug="true" targetFramework="4.0">
      <codeSubDirectories>
        <add directoryName="CSharp"/>
      </codeSubDirectories>

      <assemblies>
        <add assembly="System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.DirectoryServices.Protocols, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.ServiceProcess, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Web.RegularExpressions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
    <!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
        -->
    <authentication mode="Forms">
      <forms loginUrl="~/Default.aspx" timeout="2880" />
    </authentication>
    <membership defaultProvider="AspNetKarigariNewSqlMembershipProvider">
      <providers>
        <clear />
        <add name="AspNetKarigariNewSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="DefaultConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="KarigariNew" />
      </providers>
    </membership>
    <profile defaultProvider="AspNetKarigariNewSqlProfileProvider" enabled="true">
      <providers>
        <clear />
        <add name="AspNetKarigariNewSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="DefaultConnectionString" applicationName="KarigariNew" />
      </providers>
      <properties>
        <add name="FirstName" type="system.string" />
        <add name="LastName" type="System.string" />
        <add name="Gender" type="System.String" />
        <add name="DateOfBirth" type="System.DateTime" />
        <add name="PhoneNumber" type="System.String" />
        <add name="FaxNumber" type="System.String" />
        <add name="AlternateEmail" type="System.String" />
        <add name="Company" type="System.String" />
        <group name="ShippingAddress">
          <add name="Address1" type="System.String" />
          <add name="Address2" type="System.String" />
          <add name="City" type="System.String" />
          <add name="State" type="System.String" />
          <add name="CountryID" type="System.Int32" />
          <add name="ZipPostalCode" type="System.String" />
        </group>
        <group name="BillingAddress">
          <add name="Address1" type="System.String" />
          <add name="Address2" type="System.String" />
          <add name="City" type="System.String" />
          <add name="State" type="System.String" />
          <add name="CountryID" type="System.Int32" />
          <add name="ZipPostalCode" type="System.String" />
        </group>
        <group name="Preferences">
          <add name="ReceiveEmails" type="System.Boolean" defaultValue="True" />
          <add name="SubscribedToNewsletter" type="System.Boolean" defaultValue="True" />
          <add name="CurrencyId" type="System.Int32" defaultValue="12" />
        </group>
      </properties>
    </profile>
    <roleManager defaultProvider="AspNetKarigariNewSqlRoleProvider" enabled="true">
      <providers>
        <clear />
        <add name="AspNetKarigariNewSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="DefaultConnectionString" applicationName="KarigariNew" />
        <add name="AspNetKarigariNewWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="KarigariNew" />
      </providers>
    </roleManager>
    <!--
            The <customErrors> section enables configuration
            of what to do if/when an unhandled error occurs
            during the execution of a request. Specifically,
            it enables developers to configure html error pages
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
      <controls>
        <!--<add tagPrefix="lbn" assembly="LightBox, Version=0.1.2006.0726, Culture=neutral, PublicKeyToken=69adabc7dfa9ff4c" namespace="LightBox"/>-->
      </controls>
    </pages>
    <httpModules>
      <!--<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>-->
    </httpModules>
  </system.web>
      <!-- <system.webServer>
        <directoryBrowse enabled="true"/>
        <defaultDocument>
            <files>
                <add value="Login.aspx"/>
            </files>
        </defaultDocument>
    </system.webServer>   -->
  <system.net>
  </system.net>
  <!--
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
  <system.webServer>
    <modules>
      <!--<remove name="UrlRewriteModule"/>-->
      <!--<add name="UrlRewriteModule" preCondition="managedHandler" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>-->
    </modules>
    <!--<rewrite>
            <rules>
                <rule name="CanonicalHostNameRule1">
                    <match url="(.*)"/>
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www\.karigari\.in$" negate="true"/>
                    </conditions>
                    <action type="Redirect" url="http://www.karigari.in/{R:1}"/>
                </rule>
                <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
                    <match url="^product\.aspx$"/>
                    <conditions>
                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true"/>
                        <add input="{QUERY_STRING}" pattern="^productid=([^=&amp;]+)&amp;name=([^=&amp;]+)$"/>
                    </conditions>
                    <action type="Redirect" url="product/{C:1}/{C:2}" appendQueryString="false"/>
                </rule>
                <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
                    <match url="^product/([^/]+)/([^/]+)/?$"/>
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                    </conditions>
                    <action type="Rewrite" url="product.aspx?productid={R:1}&amp;name={R:2}"/>
                </rule>
            </rules>
            <outboundRules>
                <rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^(.*/)product\.aspx\?productid=([^=&amp;]+)&amp;(?:amp;)?name=([^=&amp;]+)$"/>
                    <action type="Rewrite" value="{R:1}product/{R:2}/{R:3}/"/>
                </rule>
            </outboundRules>
        </rewrite>-->
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <location path="online">
    <system.web>
      <trust level="Full" />
    </system.web>
  </location>
</configuration>
Tags: ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900