Read ConnectionString From App.config File

Vijayaragavan1982
Posted by in ASP.NET category on for Intermediate level | Views : 36363 red flag

Read ConnectionString Information from Application Configuration File.
Introduction

Read ConnectioString Infromation from App.config file.

Please write the below code in app.config file.

<Configuration>
    <connectionStrings>
        <add name="AppName" connectionString="Connectionstring Name" />
    </connectionStrings>
</Configuration>
Paragraph Title 1

n the code behind file, you will have to use System.Configuration namespace. So add it at the top of the .cs file like

using System.Configuration;

Now, write the following code to retrieve the values from the app.config appSettings and connectionStrings.

Paragraph Title 2
Please write the below code in .cs file. This class file is common to all.

public string connection()
        {
          return System.Configuration.ConfigurationManager.ConnectionStrings["AppName"].ToString();
        }



Page copy protected against web site content infringement by Copyscape

About the Author

Vijayaragavan1982
Full Name: Vijayaragavan R
Member Level:
Member Status: Member
Member Since: 3/11/2009 3:40:29 AM
Country:



Login to vote for this post.

Comments or Responses

Posted by: Raja on: 3/11/2009
Hey,

Your code is wrong, the correct code of the connection function will be.

public string connection()

{
return System.Configuration.ConfigurationManager.ConnectionStrings["AppName"].ToString();
}


You are keeping your connection string into ConnectionString tag not AppSetting (you have written above) tag.

Thanks

Login to post response

Comment using Facebook(Author doesn't get notification)