Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
2.59/5 (3 votes)
My application is not able to pick connection string from web.config file.
Quote:
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.


XML
<connectionStrings>
      <add name="myConnectionString" connectionString="server=XYZ;database=nDatabase;uid=sa;password=sa1234;" />
  </connectionStrings>


Please help
Posted
Updated 29-May-22 0:00am
v3
Comments
[no name] 10-May-14 3:16am    
show your code where it is throwing error
[no name] 10-May-14 3:22am    
Hi Sankarsan,

I would be pleased if you can please help me in resolving this issue.

Code : dbManager.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ToString();

If I hard code my connection String in DataaccessLayer its working fine, but if I try to pick connection string from web.config file its throwing the above exception

Regards,
Praveen N
[no name] 10-May-14 3:25am    
You are missing ConnectionString after ConnectionString"myConnectionString"] keyword
Vibhui Thakur 13-Jun-23 6:24am    
Open ku nhi ho rhi h I'd
[no name] 18-Sep-22 13:08pm    
Air full haque

For web site or web application default configuration file is web.config file. When you call ConfigurationManager.ConnectionStrings it will get the connection strings from your default configuration file, that means web.config. if you haven't define myConnectionString in web.config, add it and check your site.
 
Share this answer
 
Put your connection string
dbManager.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString.ToString();
 
Share this answer
 
Comments
[no name] 10-May-14 3:25am    
No Still the same exception exists :(
[no name] 10-May-14 3:27am    
show your web.config connection string
[no name] 10-May-14 3:27am    
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 16: {
Line 17: IDBManager dbManager = new DBManager(DataProvider.SqlServer);
Line 18: dbManager.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString.ToString();
[no name] 10-May-14 3:29am    
no!, What have you written in web.config file for connectionstring
[no name] 10-May-14 3:34am    
When I try to debug the compiler is not looking into web.config of DAL layer its straight a way jumping to UI layer web.config bit surprising .
In Web.Config
C#
<connectionstrings>
         <add name="MyConnectionString" connectionstring="Data Source=MyPc-PC;Initial Catalog=MyDb;Persist Security Info=True;User ID=sa;Password=12345" providername="System.Data.SqlClient" />
</connectionstrings>

And you can access using

C#
string constr = WebConfigurationManager.ConnectionStrings["webshopConnectionString"].ConnectionString;
 
Share this answer
 
v2
Got to know web.config file present in application file or presentation layer overrides the web.config file present in DAL layer.

Hence resolved


Special thanks to DamithSL :)

Thanks all for your help :)

Regards,
Praveen N
 
Share this answer
 
Comments
[no name] 18-Sep-22 12:57pm    
Air full haque

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


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