Last post Jul 13, 2009 09:05 PM by pinoyz
Jul 09, 2009 09:30 PM|pinoyz|LINK
I have a simple question.... how can i import system.data.sqlclient globally?
instead of typing system.data.sqlclient in everypage.. i want is to type it once and all pages has now the namespace of the system.data.sqlclient...
Jul 09, 2009 09:57 PM|atconway|LINK
Yes you can do this. Use the following directive inside your Global.asax page:
<%@ Import Namespace="System.Data.SqlClient" %>
...from MSDN: Explicitly imports a namespace into an application, making all classes and interfaces of the imported namespace available to the application. The syntax for the
@ Import directive is as follows. (http://msdn.microsoft.com/en-us/library/866zees4(VS.71).aspx)
That should solve your problem, thanks!!
Jul 09, 2009 10:39 PM|pinoyz|LINK
Ok I inclue a Global.asax into my sample page... but when i try to have a sqlcommand in my code behind. it does not recognize...
Jul 10, 2009 09:09 AM|atconway|LINK
The Global.asax is a file that will reside in your web project. The directive for adding System.Data.SqlClient I mentioned should be in that file in the markup section (not the code) at the top. It sounded like from your last post you added Global.asax
into your exsisting page (it is a separate file not part of the web page). Is this how you proceeded?
Jul 10, 2009 09:15 AM|mohd786hussain|LINK
you can add it in your web.config under system.web and it will be available gloablly.
Jul 13, 2009 10:01 AM|atconway|LINK
I must admit, I just tried importing the namespace using both the @Import directive and using the "add namespace" method from the last post to no avail. I too could not type directly into a SqlCommand without a local import.
I must be missing something because the documentation is plain and clear stating that this should import the namespaces for all of the pages and do what you are requesting. Maybe the last poster can speak up to as what is missing. Here are the links for
'add namespace (web.config)' method:
Jul 13, 2009 09:05 PM|pinoyz|LINK
I tried the add namespace(web.config) and works perfectly... I think you just need to wait or save the class that has sqlcommand and it will recognize it.
Yeah! Maybe he know what is missing in global.asax when importing a namespace for all the pages.
But i don't know if this is a good practice for importing namespace... maybe we should know the BEST PRACTICES.