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.
Error with the above code is: Object reference not set to an instance of an object.
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 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 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
The error with this code is: Object reference not set to an instance of an object.
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
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
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
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. Let me know...
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...
Adding the @ sign in front of system did not help. Also, the answer provided by fayaz_3e is something that I have already tried and posted in my original post.
One strange thing that I noticed is that the connectionStrings count show me 1 when I hover my mouse over "connectionStrings", however web.config file contains 2 connectionStrings. I am not sure if this helps anyone troubleshoot my problem.
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....
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!
Marked as answer by prmody on Aug 03, 2012 04:56 PM
prmody
Member
8 Points
14 Posts
Error while getting connection string from web.config
Aug 01, 2012 11:11 PM|LINK
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.
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <connectionStrings> <add name="BASKET" connectionString="Data Source=SABODDB04;Initial Catalog=BASKET;User ID=CRDB_Web_App;Password=xxxx" providerName="System.Data.SqlClient" /> <add name="CRED_STAGING" connectionString="Data Source=SABODDB85;Initial Catalog=CRED_STAGING;User ID=Treasury_Loan_Mod_Web;Password=xxxxx" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="CRDBUserGroupName" value="ag_CRDBRpt_admins" /> </appSettings> </configuration>Follows couple of snippets of C# code (which is the back end of ASP page).
Error with the above code is: Object reference not set to an instance of an object.
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
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
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Another code that I tried is given below.
protected void GetConnectionStrings() { System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/"); if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0) { csCredStaging = rootWebConfig.ConnectionStrings.ConnectionStrings["CRED_STAGING"].ConnectionString; csBasket = rootWebConfig.ConnectionStrings.ConnectionStrings["BASKET"].ConnectionString; } }The error with this code is: Object reference not set to an instance of an object.
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
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
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
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. Let me know...
Thanks!
fayaz_3e
Star
9332 Points
1744 Posts
Re: Error while getting connection string from web.config
Aug 01, 2012 11:30 PM|LINK
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...
protected void GetConnectionStrings() { csCredStaging = ConfigurationManager.ConnectionStrings["CRED_STAGING"].ConnectionString; csBasket = ConfigurationManager.ConnectionStrings["BASKET"].ConnectionString; }Make sure to include proper namespaces...
Ruchira
All-Star
42900 Points
7021 Posts
MVP
Re: Error while getting connection string from web.config
Aug 02, 2012 02:52 PM|LINK
Hello,
Not sure exactly what's causing the error. Try by putting @ infront of the code like below
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.prmody
Member
8 Points
14 Posts
Re: Error while getting connection string from web.config
Aug 03, 2012 04:09 PM|LINK
Adding the @ sign in front of system did not help. Also, the answer provided by fayaz_3e is something that I have already tried and posted in my original post.
One strange thing that I noticed is that the connectionStrings count show me 1 when I hover my mouse over "connectionStrings", however web.config file contains 2 connectionStrings. I am not sure if this helps anyone troubleshoot my problem.
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....
Thanks!
prmody
Member
8 Points
14 Posts
Re: Error while getting connection string from web.config
Aug 03, 2012 04:56 PM|LINK
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!