Home Asp.net How to Get appSettings Value from Web.config File?

How to Get appSettings Value from Web.config File?

3072
0
How to Get appSettings Value from Web.config File?
How to Get appSettings Value from Web.config File?

Now here in this tutorial, I’ll explain how to read or get appSettings value from web.config file to fetch connection string using c# as well as vb.net in Asp.net. If you find the tutorial useful, do check out our other tutorials as well.

In my previous tutorials, I’d explained about insert update and delete on asp.net gridview, sql injection with example, get connectionstring from web.config file and other more cracking tutorials on appSettings, Asp.net here.

Get appSettings Value from Web.config File

I’m taking an example of connection string to get it from appSettings section of web.config file. It’ll be easier to understand the basic difference between appSettings and connectionStrings in web.config file.

Before starting how to read or get appSettings value from web.config file, you should know how to get connection string to connect your database from here that will show you also the difference why I’ve not used user id and password in my connection string. After knowing that, you can adjust your connection string in web.config as follows.

<appSettings>
    <add key="myDbConnection" value="Data Source=myServer;Integrated Security=true;Initial Catalog=myDatabase" />
</appSettings>

To read or get appSettings value from web.config file in asp.net, we need to add System.Configuration namespace that will help us to read our connection string from appSettings section of web.config file.

Note: If you have defined your connection string under connectionStrings section, you can get your connection string from connectionStrings section this way.

Read appSettings Value in C#

protected void Page_Load(object sender, EventArgs e) {
    string con = System.Configuration.ConfigurationManager.AppSettings["myDbConnection"];
}

Read appSettings Value in Vb.net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim con As String = System.Configuration.ConfigurationManager.AppSettings("myDbConnection")
End Sub

That’s it, now you’ll be able to use your connection string from appSettings section of web.config file in asp.net.

Previous articleHow to Get ConnectionString from Web.config in Asp.net?
Next articleMain Difference between SessionState and ViewState in Asp.net
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

7 − 7 =