6

I'm using MySql in my asp.net project. But I don't want to type every "using MySql.Data.MySqlClient;" statement in every aspx.cs/aspx.vb file. How can I define this lines in web.config file?

I've defined some namespaces like below but this only works for aspx pages:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="false" targetFramework="4.0"/>
        <pages>
            <namespaces>
                <add namespace="System.Web.Configuration"/>
                <add namespace="MySql.Data"/>
                <add namespace="MySql.Data.MySqlClient"/>
            </namespaces>
        </pages>
    </system.web>
</configuration>

related question : Define common namespaces for code pages in Web.Config

HasanG
  • 12,734
  • 29
  • 100
  • 154

3 Answers3

6

There is no way to setup global usings for the code-behinds. You have to put the usings in the code files.

Tom Cabanski
  • 7,828
  • 2
  • 22
  • 25
  • +1. `using` is a statement in source code that is proccessed by C# compiler. `web.config` is for web server configuration – abatishchev Apr 16 '10 at 11:37
  • And aspx pages are compiled by...? This is not logical to me :) – HasanG Apr 16 '10 at 11:45
  • `.aspx` page - is just a markup with server-side code - they are also processed by web server. `.aspx.cs` are source files. You can name it even `1.cs` - just don't forget to specify it into page declaration. – abatishchev Apr 16 '10 at 11:56
  • P.S. Please use symbol @ before nick, i.e. @abatishchev, sou it would be easy to find your comment on my one. Thanks! :) – abatishchev Apr 16 '10 at 11:57
  • @abatishchev OK. I googled all day but no solution. Probably there is no solution. Maybe this is why every code-behind page contains using System; :( – HasanG Apr 16 '10 at 13:12
4

Yes you can. If you 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>
Daniel Dyson
  • 13,192
  • 6
  • 42
  • 73
  • Sorry I can't explain myself clear. Simply I don't want to see that code there :). Also this solution would not be good because in some projects I use MySQL in some SQL Serve or Access... – HasanG Apr 16 '10 at 11:54
  • 1
    In that case, I think you had better mark @Tom's answer as correct, because it can't be done. – Daniel Dyson Apr 16 '10 at 12:34
  • 2
    If you worry about managing "usings", I would suggest purchasing the Resharper add-in. It has an excellent template mechanism you can use to generate "usings" for different purposes if that's the way you want to go. It has a cleanup feature that removes unused "usings" and sorts them into logical order so it keeps the code tidy. It also has a feature that detects missing "usings" as you type code and prompts you to see if you want it to add the "using" for you. It does a whole lot more too. Highly recommended. – Tom Cabanski Apr 16 '10 at 12:36
1

Just wrap your using block in a #region and collapse it. No more worry about how many usings there are.

John
  • 1,094
  • 2
  • 11
  • 20