I have an xml config file for my website, which stores all the
information required to connect to a database. I have used the following
code to get the xml file into my program as an object, and this works
beautifully, as long as I directly call it from my codebehind.
Dim MyConfigXml As New System.Xml.XmlDataDocument
MyConfigXml.Load(aHttpServUtil.MapPath("My.config"))
However, as the settings are common I would like to use this in a static
method (sorry, just realised, I mean shared function - I learnt programing
in java mainly, so I sometimes mix up the terms) of an object, and call it
from all my pages. Unfortunately, when I do
this the MapPath gives compiler errors, and I have to manually enter the
entire path into the method. Are there any other ways of getting the path -
the path of the config file is the same as that of all of my .vb files. I am
using the VB.net language, .net framework 1.1 and VS.net 2003.
Thanks,
MartinYou forgot to mention what is the exact compilation error you have. A common
error in this context could be to try to access a non shared member inside a
shared member.
You may want also to have a look at the documentation for the "web.config"
file. This is the usual configuration file for web applications and it could
provide perhaps what you are trying to do...
--
Patrice
"Martin Eyles" <martin.eyles@.NOSPAMbytronic.com> a crit dans le message de
news: 1234ev9n6loiu95@.corp.supernews.com...
> Hi,
> I have an xml config file for my website, which stores all the
> information required to connect to a database. I have used the following
> code to get the xml file into my program as an object, and this works
> beautifully, as long as I directly call it from my codebehind.
> Dim MyConfigXml As New System.Xml.XmlDataDocument
> MyConfigXml.Load(aHttpServUtil.MapPath("My.config"))
> However, as the settings are common I would like to use this in a static
> method (sorry, just realised, I mean shared function - I learnt programing
> in java mainly, so I sometimes mix up the terms) of an object, and call it
> from all my pages. Unfortunately, when I do
> this the MapPath gives compiler errors, and I have to manually enter the
> entire path into the method. Are there any other ways of getting the
> path -
> the path of the config file is the same as that of all of my .vb files. I
> am
> using the VB.net language, .net framework 1.1 and VS.net 2003.
> Thanks,
> Martin
"Patrice" <scribe@.chez.com> wrote in message
news:uHI94u8VGHA.2952@.TK2MSFTNGP11.phx.gbl...
> You forgot to mention what is the exact compilation error you have. A
> common error in this context could be to try to access a non shared member
> inside a shared member.
Ok, here is what you need to know I think. If I used the class directly, I
get "Reference to a non-shared member requires an object reference.". If I
try to make an object of this class, I get an error when trying to
instantiate it (with the code "Dim aHttpServUtil As New
HttpServerUtility") - "Overload resolution failed because no 'New' is
accessible.".
> You may want also to have a look at the documentation for the "web.config"
> file. This is the usual configuration file for web applications and it
> could provide perhaps what you are trying to do...
I want my own config file, with just my own configuration data - this makes
it easy to install on different server setups with different database
servers. It also makes it harder for us to break the system by changing the
wrong thing in web.config. Thanks for the idea though - it is appreciated
Thanks,
Martin
> "Martin Eyles" <martin.eyles@.NOSPAMbytronic.com> a crit dans le message
> de news: 1234ev9n6loiu95@.corp.supernews.com...
>> Hi,
>> I have an xml config file for my website, which stores all the
>> information required to connect to a database. I have used the following
>> code to get the xml file into my program as an object, and this works
>> beautifully, as long as I directly call it from my codebehind.
>>
>> Dim MyConfigXml As New System.Xml.XmlDataDocument
>> MyConfigXml.Load(aHttpServUtil.MapPath("My.config"))
>>
>> However, as the settings are common I would like to use this in a static
>> method (sorry, just realised, I mean shared function - I learnt
>> programing in java mainly, so I sometimes mix up the terms) of an object,
>> and call it from all my pages. Unfortunately, when I do
>> this the MapPath gives compiler errors, and I have to manually enter the
>> entire path into the method. Are there any other ways of getting the
>> path -
>> the path of the config file is the same as that of all of my .vb files. I
>> am
>> using the VB.net language, .net framework 1.1 and VS.net 2003.
Hi Martin!
> If I try to make an object of this class, I get an error
> when trying to instantiate it (with the code "Dim aHttpServUtil As New
> HttpServerUtility") - "Overload resolution failed because no 'New' is
> accessible.".
You just have to use the current HttpContext:
string s = HttpContext.Current.Server.MapPath("file.xml");
This only works, if there is a current HttpContext. So you cannot use this
e.g. in static constructors, or in background Threads you have created
(HttpContext.Current is null under this circumstances). But if you use this
in a static functions, witch is called somewhere in the middle of a Http -
Request, it is no problem.
OK?
br, GP
"Gnter Prossliner" <g.prossliner/gmx/at> wrote in message
news:exgy%23U9VGHA.4328@.TK2MSFTNGP12.phx.gbl...
> Hi Martin!
>> If I try to make an object of this class, I get an error
>> when trying to instantiate it (with the code "Dim aHttpServUtil As New
>> HttpServerUtility") - "Overload resolution failed because no 'New' is
>> accessible.".
> You just have to use the current HttpContext:
> string s = HttpContext.Current.Server.MapPath("file.xml");
>
> This only works, if there is a current HttpContext. So you cannot use this
> e.g. in static constructors, or in background Threads you have created
> (HttpContext.Current is null under this circumstances). But if you use
> this in a static functions, witch is called somewhere in the middle of a
> Http - Request, it is no problem.
> OK?
> br, GP
Thanks very much - it works a treat. :-)
Martin
0 comments:
Post a Comment