Click here to Skip to main content
14,326,246 members
Rate this:
Please Sign up or sign in to vote.
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.


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


Please help
Posted
Updated 39 mins ago
v3
Comments
sankarsan parida 10-May-14 3:16am
   
show your code where it is throwing error
praveen_07 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
sankarsan parida 10-May-14 3:25am
   
You are missing ConnectionString after ConnectionString"myConnectionString"] keyword
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Put your connection string
dbManager.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString.ToString();
   
Comments
praveen_07 10-May-14 3:25am
   
No Still the same exception exists :(
sankarsan parida 10-May-14 3:27am
   
show your web.config connection string
praveen_07 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();
sankarsan parida 10-May-14 3:29am
   
no!, What have you written in web.config file for connectionstring
praveen_07 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 .
Rate this:
Please Sign up or sign in to vote.

Solution 4

In Web.Config
<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

string constr = WebConfigurationManager.ConnectionStrings["webshopConnectionString"].ConnectionString;
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 11

ewfefax3dgagkiuijknkunkcpiuisfkgnkuim,mkl0sdklvmkisdvu
''ppl;;x,EHJHJUHFJWHHHYUHFUIUU 7 HUUHFU U 8UFFJKHU UUYFUEIFFDJK U8N IJITFJHDUYI2IOU8JJ1HUFEFB JJHUHJEFHDUJDBJHUINFJH8AOIJDAKFJ9ANUNI
   
Rate this:
Please Sign up or sign in to vote.

Solution 5

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
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100