Error while getting connection string from web.confighttp://forums.asp.net/t/1829831.aspx/1?Error+while+getting+connection+string+from+web+configFri, 03 Aug 2012 16:56:11 -040018298315091892http://forums.asp.net/p/1829831/5091892.aspx/1?Error+while+getting+connection+string+from+web+configError while getting connection string from web.config <p>I tried couple of approaches to get connection string from web.config and have spent quite a bit of energy to get the soltuion, but no avail. Follows the code I have for web.config.&nbsp;</p> <pre class="prettyprint">&lt;configuration xmlns:xdt=&quot;http://schemas.microsoft.com/XML-Document-Transform&quot;&gt; &lt;connectionStrings&gt; &lt;add name=&quot;BASKET&quot; connectionString=&quot;Data Source=SABODDB04;Initial Catalog=BASKET;User ID=CRDB_Web_App;Password=xxxx&quot; providerName=&quot;System.Data.SqlClient&quot; /&gt; &lt;add name=&quot;CRED_STAGING&quot; connectionString=&quot;Data Source=SABODDB85;Initial Catalog=CRED_STAGING;User ID=Treasury_Loan_Mod_Web;Password=xxxxx&quot; providerName=&quot;System.Data.SqlClient&quot; /&gt; &lt;/connectionStrings&gt; &lt;appSettings&gt; &lt;add key=&quot;CRDBUserGroupName&quot; value=&quot;ag_CRDBRpt_admins&quot; /&gt; &lt;/appSettings&gt; &lt;/configuration&gt;</pre> <p>Follows couple of snippets of C# code (which is the back end of ASP page).</p> <pre class="prettyprint">string conn1 = System.Configuration.ConfigurationManager.ConnectionStrings["CRED_STAGING"].ConnectionString; string conn2 = System.Configuration.ConfigurationManager.ConnectionStrings["BASKET"].ConnectionString;</pre> <p>Error with the above code is:&nbsp;Object reference not set to an instance of an object.&nbsp;</p> <p>at CRDB_Web_Application._Default.GetConnectionStrings() in C:\Users\pmody\Documents\Visual Studio 2010\Projects\CRDB_Web_Application\CRDB_Web_Application\Request_Data_Processing.aspx.cs:line 128<br /> at CRDB_Web_Application._Default.Page_Load(Object sender, EventArgs e) in C:\Users\pmody\Documents\Visual Studio 2010\Projects\CRDB_Web_Application\CRDB_Web_Application\Request_Data_Processing.aspx.cs:line 41<br /> at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)<br /> at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)<br /> at System.Web.UI.Control.OnLoad(EventArgs e)<br /> at System.Web.UI.Control.LoadRecursive()<br /> at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)</p> <p>Another code that I tried is given below.</p> <pre class="prettyprint"> protected void GetConnectionStrings() { System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/"); if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count &gt; 0) { csCredStaging = rootWebConfig.ConnectionStrings.ConnectionStrings["CRED_STAGING"].ConnectionString; csBasket = rootWebConfig.ConnectionStrings.ConnectionStrings["BASKET"].ConnectionString; } }</pre> <p>The error with this code is:&nbsp;Object reference not set to an instance of an object.&nbsp;</p> <p>at CRDB_Web_Application._Default.GetConnectionStrings() in C:\Users\pmody\Documents\Visual Studio 2010\Projects\CRDB_Web_Application\CRDB_Web_Application\Request_Data_Processing.aspx.cs:line 126<br> at CRDB_Web_Application._Default.Page_Load(Object sender, EventArgs e) in C:\Users\pmody\Documents\Visual Studio 2010\Projects\CRDB_Web_Application\CRDB_Web_Application\Request_Data_Processing.aspx.cs:line 41<br> at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)<br> at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)<br> at System.Web.UI.Control.OnLoad(EventArgs e)<br> at System.Web.UI.Control.LoadRecursive()<br> at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)</p> <p>Somewhere I read that I can place connection string components to appsettings section of web.config but I do not think it is the right approach. &nbsp;Let me know...</p> <p>Thanks!</p> <p></p> 2012-08-01T23:11:04-04:005091900http://forums.asp.net/p/1829831/5091900.aspx/1?Re+Error+while+getting+connection+string+from+web+configRe: Error while getting connection string from web.config <p>That says, the obejct from the firts two lines is null. While debugging you will be able to find which object it is. I guess its rootWebConfig. Meanwhile i dont understand why are you complicating a simple thing by opening configuration section? You can directly read connection string using Configuartion Manager...</p> <pre class="prettyprint">protected void GetConnectionStrings() { csCredStaging = ConfigurationManager.ConnectionStrings[&quot;CRED_STAGING&quot;].ConnectionString; csBasket = ConfigurationManager.ConnectionStrings[&quot;BASKET&quot;].ConnectionString; }</pre> <p><br> Make sure to include proper namespaces...</p> 2012-08-01T23:30:24-04:005092849http://forums.asp.net/p/1829831/5092849.aspx/1?Re+Error+while+getting+connection+string+from+web+configRe: Error while getting connection string from web.config <p>Hello,</p> <p>Not sure exactly what's causing the error. Try by putting @ infront of the code like below</p> <pre class="prettyprint">string conn1 = @System.Configuration.ConfigurationManager.ConnectionStrings[&quot;CRED_STAGING&quot;].ConnectionString; string conn2 = @System.Configuration.ConfigurationManager.ConnectionStrings[&quot;BASKET&quot;].ConnectionString;</pre> 2012-08-02T14:52:06-04:005094633http://forums.asp.net/p/1829831/5094633.aspx/1?Re+Error+while+getting+connection+string+from+web+configRe: Error while getting connection string from web.config <p>Adding the @ sign in front of system did not help. Also, the answer provided by&nbsp;fayaz_3e is something that I have already tried and posted in my original post.</p> <p>One strange thing that I noticed is that the connectionStrings count show me 1 when I hover my mouse over &quot;connectionStrings&quot;, however web.config file contains 2 connectionStrings. I am not sure if this helps anyone troubleshoot my problem.&nbsp;</p> <p>Also, I am assuming that web.config in my project is referred by VS in debug mode, when I hit run and I do not need to deploy the web.config anywhere. Is that the right understanding? Let me know....</p> <p>Thanks!</p> 2012-08-03T16:09:59-04:005094664http://forums.asp.net/p/1829831/5094664.aspx/1?Re+Error+while+getting+connection+string+from+web+configRe: Error while getting connection string from web.config <p>I did some more debugging and realized that the web.config in my project is not read by the asp.net page and corresponding C# page during debug run. The web.config it refers to is some other file with other connectionstrings. My guess is that that is referring to web.config on the server, even during the debug mode. Frustrating!</p> 2012-08-03T16:56:11-04:00