views:

207

answers:

3

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

+5  A: 

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

Tom Cabanski
+1. `using` is a statement in source code that is proccessed by C# compiler. `web.config` is for web server configuration
abatishchev
And aspx pages are compiled by...? This is not logical to me :)
HasanGursoy
`.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
P.S. Please use symbol @ before nick, i.e. @abatishchev, sou it would be easy to find your comment on my one. Thanks! :)
abatishchev
@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; :(
HasanGursoy
+2  A: 

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
I don't worry for fast writing just for less code mess
HasanGursoy
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...
HasanGursoy
In that case, I think you had better mark @Tom's answer as correct, because it can't be done.
Daniel Dyson
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
+1  A: 

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

John