Archive

Posts Tagged ‘web.config’

Getting ConnectionStrings value from code behind

December 5, 2010 Leave a comment

When you do wants to get some data from a database to show it on your ASP.NET web application, you will need to use a connection string to connect to the database. This string is very important because, at most conditions, you will be using this string again and again across the entire application. Therefore, you can utilize the ConnectionStrings tag in the web.config file to store your predefined connection strings.

Well, some programmers will say “Why do I need to store it in the web.config file? I can just code it in my code behind codes and it will still work the same.”. Yes, it will still work the same. But that is only suitable for a mini web application that does not require much connection to the database. However, if it’s a big web application and the connection string values have to be changed, the programmers will spend a long time just to find and replace the existing connection string to the new string.

Therefore, it’s advised to always store a connection string in the web.config file and get its value from here. Below, I will show you how to get the connection string values from the ConnectionStrings tag in the web.config file.

 

Code in web.config:

<connectionStrings>
        <add name="MyConnectionString" connectionString="Your connection string here" providerName="System.Data.SqlClient" />
</connectionStrings>

Code for C#:

System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

Code for VB.NET:

System.Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString

From the sample above, I’ve named my connection string to “MyConnectionString”. For the connectionString values which I’ve written as “Your connection string here”, you will have to replace it with a proper connection string. You can find more about connection string from http://connectionstrings.com/. As for the providerName, you have to put the namespace of the data connection class. In this case above, “System.Data.SqlClient” will be connecting to Microsoft SQL Server.

Get AppSettings value from code behind

December 4, 2010 1 comment

When you are developing an ASP.NET web application, you sometimes may need to reuse a term or name over and over again across the application. If the term is used in some places only, there might not be a problem to change it when you need to change the term.

However, if the application that you are developing is a big application, it will be very tedious if you need to change the term value after lots of pages has been coded. So how do you solve this?

The solution is to use AppSettings which can be located in the web.config. Values that has been added into the AppSettings tag in the web.config file can be accessed across the entire application. So, any changes made to the value of the AppSettings, it will be applied across the entire application without having to change the values page by page.

Below, I’ll be showing you how to access the AppSettings values you have added into the web.config file from the code behind file. You can also access this values from class file in your application.

 

Note: I have added a Label with the ID of “Label1” in the Default.aspx file.

 

Code in web.config:

<appSettings>
        <add key="MyAppSettings" value="Some value" />
</appSettings>

Code for C#:

Label1.Text = System.Configuration.ConfigurationManager.AppSettings["MyAppSettings"].ToString();

Code for VB.NET:

Label1.Text = System.Configuration.ConfigurationManager.AppSettings("MyAppSettings").ToString()

 

The sample code above shows you how to get the value of the key “MyAppSettings” from the collection of pre defined application settings from the AppSettings tag in the web.config file. Then it displays the value out to the Label with ID “Label1”.

 

Note: Make sure that your web application has reference to System.Configuration. Otherwise, you will get errors when you compile your web application.

Get AppSettings value in ASPX page

December 4, 2010 1 comment

In my previous post, I’ve shown how to get AppSettings value from the code behind file. Here, I’ll show you how to get AppSettings value directly from the .ASPX page.

The sample below displays the value for “MyAppSettings” key stored in web.config file as the text for a label named “Label1”.

 

Code for ASPX page:

<asp:Label Text='<%$ AppSettings:MyAppSettings %>’ runat="server" ID="Label1" />

Code in web.config:

<appSettings>
        <add key="MyAppSettings" value="Some value" />
</appSettings>

Categories: ASP.NET Tags: , ,

How to: Encrypt/Decrypt web.config

October 17, 2010 Leave a comment

When you are developing a ASP.NET Web Application, you know that the web.config file is a very important file to your web application. This is because, it contains all/part of your application settings, such as connection strings. So, when you have completed developing your web application and wants to deploy it to the server, you might want to encrypt the web.config file first. This is to make sure that nobody knows what is the content in that web.config file if they manage to get a copy of it. Well of course IIS will not allow any request to view/download the web.config file.

You can encrypt/decrypt the web.config file at any time. Take note that you can ONLY decrypt an encrypted web.config file on the machine that encrypted the web.config. Here is several ways of how you can encrypt your web.config files:

Encrypt

  1. Open up a Command Prompt.
    For Windows XP: press Win+R, then type in “cmd” and hit Enter.
    For Windows Vista/Windows 7: Press start and type “cmd” in the search bar. Click on the “Cmd” program.
  2. Navigate to the the directory “C:\Windows\Microsoft.NET\Framework\v2.0.50727\
  3. Then, type in the following:
    aspnet_regiis -pe “connectionStrings” -app “/SampleApplication” -prov “RsaProtectedConfigurationProvider”

NOTE:

This sample above is to encrypt the “connectionStrings” section for the application “SampleApplication” using the provider “RsaProtectedConfigurationProvider”.

 

Decrypt

  1. Open up a Command Prompt.
    For Windows XP: press Ctrl+R, then type in “cmd” and hit Enter.
    For Windows Vista/Windows 7: Press start and type “cmd” in the search bar. Click on the “Cmd” program.
  2. Navigate to the the directory “C:\Windows\Microsoft.NET\Framework\v2.0.50727\
  3. Then, type in the following:
    aspnet_regiis –pd “connectionStrings” -app “/SampleApplication”
Categories: ASP.NET Tags: , , ,