Adding NameSpace in ASP.net or C#

1

Written on 10:10 PM by Mj blog

Namespace are nothing but logical grouping of class inside the Assembly. Lets see the ways of adding Namespace in ASP.net

  1.  <%@Import Namespace =”System.Data” %>  - Embedding in HTML Source code
  2. Using System.Data – Importing in CS file
  3. System.Data.DataSet – Calling directly in CS code

The 3 ways are very common and there is a one more way to add namespace, consider your application has 100 pages and you need to add System.Data namespace to every page individually you can do this.

Go to the web.config and add the namepace in Pages element like this

<configuration>

      <appSettings/>

      <connectionStrings/>

      <system.web>

    <pages>

      <namespaces>

        <add namespace ="System.Data"/>

      </namespaces>

    </pages>

      </system.web>

</configuration>

So you learned 4 ways of adding Namespace in ASP.net. There is one more way also but it will be machine specific. Lets see, when ever we create add a new page or create new website in the visual studio the page has some namespace included in the .cs file … Ever wondered how that happened, actually microsoft has a hidden web.config file where all those namespace has been predefined. To fine out more go this path

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG

you will find a “web.config” file where ASP.net has predefined its default namespace, you can add your namespace there so that it appears in every page of the website you create.

Sorry guys I have not tried this with App.config file of windows application, if any one explore that pls report