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.
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.
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.
<add key="myDbConnection" value="Data Source=myServer;Integrated Security=true;Initial Catalog=myDatabase" />
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.
Read appSettings Value in C#
string con = System.Configuration.ConfigurationManager.AppSettings["myDbConnection"];
Read appSettings Value in Vb.net
Dim con As String = System.Configuration.ConfigurationManager.AppSettings("myDbConnection")
That’s it, now you’ll be able to use your connection string from appSettings section of web.config file in asp.net.