Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


when I am trying to use connection string from Web.Config , it is showing null...

I used
C#
string conStr = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

and
C#
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


Please Help


Thank You
Posted
Updated 19-Aug-14 21:29pm
v2
Comments
Hemant Singh Rautela 19-Aug-14 3:42am    
And your webcofing section code..??

Is it same as :

<connectionstrings>
<add name="ConnectionString" connectionstring="Server=database_server_ip;Initial Catalog=databasename;User ID=username;Password=yourpassword">
</add>
</connectionstrings>


Or anything else....??
Anup kumar(MCA) 21-Aug-14 7:32am    
are you using multiple applications in your solution?

Check whether you have mentioned the details under ConnectionString Tag or AppSettings Tag in your web.config file.

If connection string is in ConnectionString Tag then use this

C#
string conStr = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString.ToString();


If connection string is in AppSettings Tag then use this

C#
string conStr = ConfigurationManager.AppSettings["ConnectionStringName"].ToString();
 
Share this answer
 
C#
System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;



note:"connectionStringName" must be same as in webconfig
 
Share this answer
 
Comments
Member 3567411 19-Aug-14 3:39am    
Hi,
I have used the same name as per the web.config file ...
System.Configuration.ConfigurationManager.
ConnectionStrings["connectionStringName"].ConnectionString;
above code is not working...
NowYouSeeMe 19-Aug-14 3:57am    
post your connection string from web config here
Sanchayeeta 20-Aug-14 2:36am    
Please post your connection string from web.config and the code where you have used it.
Check if the web.config is at its right location?
Do you have any web.config (which overrides main web.config) in the same folder as that of your page?
 
Share this answer
 
Comments
Gihan Liyanage 15-Sep-14 6:17am    
Did you got a solution from my support, I can see you have not accepted any answer.If you are ok with this answer plz accept it. Then any user having same problem can identified it has solved the problem..
I think you may have multiple web.config files. So your needed file may be overridden by other.
 
Share this answer
 
hi
you can use alternative solution for this type of problem. You also use appsetting-
just like that-:
System.Configuration.ConfigurationSettings.AppSettings["MySetting"]
 
Share this answer
 

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



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