Tuesday, March 13, 2012

Server.MapPath Failing When Moved to Class Module

In a VB/ASP.NET project, the following lines work as advertised when placed
in a code-behind file (whatever.aspx.vb) - meaning that I get the full
physical path to MyDatabase.MDB in the variable PathToMDB. However, when the
same two lines are moved to a separate class file (whatever.vb), the project
fails to build - with the message "Name 'Server' is not declared."

Dim PathToMDB As String
PathToMDB = Server.MapPath("/MySite/db/") + "MyDatabase.mdb"

Why is this? What can I do about it? I need to obtain the physical path to
the MDB file from within the class file.

Thanks in advance."Jeff S" <JeffS@.asdf.net> wrote in message
news:uloQjlvoDHA.1928@.TK2MSFTNGP12.phx.gbl...
> In a VB/ASP.NET project, the following lines work as advertised when
placed
> in a code-behind file (whatever.aspx.vb) - meaning that I get the full
> physical path to MyDatabase.MDB in the variable PathToMDB. However, when
the
> same two lines are moved to a separate class file (whatever.vb), the
project
> fails to build - with the message "Name 'Server' is not declared."
> Dim PathToMDB As String
> PathToMDB = Server.MapPath("/MySite/db/") + "MyDatabase.mdb"
>
> Why is this? What can I do about it? I need to obtain the physical path to
> the MDB file from within the class file.
> Thanks in advance.

Try
HttpContext.Current.Server.MapPath("/MySite/db/") + "MyDatabase.mdb"

--
[[((hillarie))]]

To reply by email, remove ".SPAMBLOCK" from email address
Try it in this format:
MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
server.mappath("/") & "../data/myDB.mdb;"

David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com

"Jeff S" <JeffS@.asdf.net> wrote in message
news:uloQjlvoDHA.1928@.TK2MSFTNGP12.phx.gbl...
> In a VB/ASP.NET project, the following lines work as advertised when
placed
> in a code-behind file (whatever.aspx.vb) - meaning that I get the full
> physical path to MyDatabase.MDB in the variable PathToMDB. However, when
the
> same two lines are moved to a separate class file (whatever.vb), the
project
> fails to build - with the message "Name 'Server' is not declared."
> Dim PathToMDB As String
> PathToMDB = Server.MapPath("/MySite/db/") + "MyDatabase.mdb"
>
> Why is this? What can I do about it? I need to obtain the physical path to
> the MDB file from within the class file.
> Thanks in advance.

0 comments:

Post a Comment