Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting the connx string of an SQL Connection

Status
Not open for further replies.

dand11

Programmer
Jun 24, 2008
63
US

I am setting the connection string as follows but I'm getting an error that states "The ConnectionString property has not been initialized." I used the command window in VS2005 and found out that "ConfigurationManager.AppSettings["ConnxString"]" is returning a null value. I set it in my web.config as follows. Why am I getting a null value?


my code
Code:
SqlConnection objConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnxString"]);


my web.config
Code:
<?xml version="1.0"?>
<!-- 
    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>
  <appSettings />
	<connectionStrings>
    <!--   "Data Source=LAPTOP;Initial Catalog=RODEOMOUNT;Integrated Security=True"     -->
    <!-- Live string  "Data Source=tcp:sql2k510.discountasp.net;Initial Catalog=SQL2005_548493_rodeomount;User ID=SQL2005_548493_rodeomount_user;Password=******;"  -->
		<add name="ConnxString" connectionString="Data Source=LAPTOP;Initial Catalog=RODEOMOUNT;Integrated Security=True" />
	</connectionStrings>

    
   	

	<system.web>    
		<!-- 
            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"/>
		<!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
		<authentication mode="Windows"/>
		<!--
            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>
        -->
	</system.web>
</configuration>
 
your mixing app settings
<appSettings>
<add key="" value="" />
...

with connection strings
<connectionStrings>
<add name="" connectionString="" Provider="" />
...

use the connection strings node since this is a connection string. in code you call
ConfigurationManager.ConnectionStrings["name of cnn str"].ConnectionString;

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Could you be more specific? I didn't think there were any app settings because I use the open/close tag <appSettings/>.
I don't see how I'm mixing them. Also I don't have the option show for placing "ConnecttionStrings" on the end of the code call for intellisense.
 
your right, I misread the config file.
the config file is ok, the problem is the code
Code:
new SqlConnection(ConfigurationManager.ConnectionStrings["ConnxString"].ConnectionString);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top