7

I need to add this namespace to my c# file:

using System.Data;

Is there a way to automatically add this to newly created pages in c#.net?

I don't want to add this namespace to new pages.

1
  • 2
    I don't know if this is an answer as such, but in VS, when you first type, e.g. IDataReader you should see a little red box at the bottom right of the word. If you hit CTRL + . you should then see a little prompt appear to add the using statement for you. Commented May 26, 2010 at 11:38

6 Answers 6

9

Open %Program Files%\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip, Or: %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033

You can modify the class.cs file within that's used to generate all new C# source files - it looks like this:

using System;
using System.Collections.Generic;
using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

Also, there is a file called Class.vstemplate. Open this and you can edit the following:

<Reference>
    <Assembly>System</Assembly>
        </Reference>
        <Reference>
            <Assembly>System.Data</Assembly>
        </Reference>
        <Reference>
            <Assembly>System.Xml</Assembly>
        </Reference>
    </References>
Sign up to request clarification or add additional context in comments.

Comments

3

You can create a custom template (see here) that contains the namespace delcaration, or you can edit the existing template if you need it always and for every project.

1 Comment

To add on the this answer, you may want to have a look at this thecodinghumanist.com/Content/HowToEditVSTemplates.aspx
0

I think your out of luck you'll have to add it to every page you create since every page you create is a Class of its own.

Comments

0

You have to put it on all pages.

Comments

0

You can edit the default item templates to add whatever you want OR make your own. The default templates are here (or you get the gist of their location from my machine): C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates

Understand that (at least in prior versions of VS) there was a cache of templates as well, so you would have to edit the zipped template AND look for a directory with the same name as the zip file and edit the templates in there/delete the directory.

Here's a not too bad blog post about it.

Comments

0

You can put it in the web.config file.

    <pages>
   <namespaces>
      <add namespace="System" />
      <add namespace="System.Collections" />
      <add namespace="System.Collections.Specialized" />
      <add namespace="System.Configuration" />
      <add namespace="System.Text" />
      <add namespace="System.Text.RegularExpressions" />
      <add namespace="System.Web" />
      <add namespace="System.Web.Caching" />
      <add namespace="System.Web.SessionState" />
      <add namespace="System.Web.Security" />
      <add namespace="System.Web.Profile" />
      <add namespace="System.Web.UI" />
      <add namespace="System.Web.UI.WebControls" />
      <add namespace="System.Web.UI.WebControls.WebParts" />
      <add namespace="System.Web.UI.HtmlControls" />
   </namespaces>
   <!-- Other elements -->
</pages>

This can be used to add the namespace in all the pages

2 Comments

Hi Shivam. This section of the config file only makes these namespaces available to the aspx files, not the cs files.
true but how about creating a sort of master page for all the .cs files and using inheritence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.