Click here to Skip to main content
15,916,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible to read connection string from Application config in asp.net web application.If so How?
Posted

First, set the connection string in App.Config File.
HTML
<configuration>
  <connectionstrings>
    <add name="CharityManagement">
    connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
  </add></connectionstrings>
</configuration>

In your forms; set references that you want to use.
C#
using System.Data.SqlClient;

Then you can get Connection String from App.Config by use ConnectionStrings properties.
C#
var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;

Refer: http://msdn.microsoft.com/en-us/library/ms178411.aspx[^]
 
Share this answer
 
Try this ..

XML
<Configuration>
    <connectionStrings>
        <add name="AppName" connectionString="Connectionstring Name" />
    </connectionStrings>
</Configuration>


C#
System.Configuration.ConfigurationManager.ConnectionStrings["AppName"].ToString();


Here is the detailed example

http://www.dotnetfunda.com/articles/article283.aspx[^]

http://www.dreamincode.net/forums/topic/45321-grabbing-connectionstring-from-appconfig/[^]

http://csharpdotnetfreak.blogspot.com/2012/07/webconfig-connectionstring-appsettings.html[^]

Regards
Sebastian
 
Share this answer
 
v2
Comments
Benoy Benin 13-Jul-12 4:52am    
No it wont work
Sebastian T Xavier 13-Jul-12 4:54am    
why?
Yes, you can do like this

in web.config:

XML
<Configuration>
    <connectionStrings>
        <add name="ConnectionString" connectionString="Connectionstring Name" />
    </connectionStrings>
</Configuration>



your solution page:

Dim ConStr As String = ConfigurationSettings.AppSettings("ConnectionString")
 
Share this answer
 
web.config is used with web applications. web.config will by default have several configurations required for the web application. You can have a web.config for each folder under your web application.

app.config is used for windows applications. When you build the application in vs.net, it will be automatically renamed to .exe.config and this file has to be delivered along with your application.

I suggest you copy your app-settings into web.config file.
 
Share this answer
 
Comments
Benoy Benin 13-Jul-12 5:05am    
I have a class library,i need to get connection there without using web config.
StianSandberg 13-Jul-12 5:29am    
ok, but you are running this class library in a web application? or is it a windows form? Console app?

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