Server.Mappath method

N

Nick

I have been using the Server.Mappath method to get the physical directory
name so that I can create a connection string to my database.
When I include this in an ASPX file all works fine.

But I wanted to remove the code from my pages in put them either in code
behind or possibly an assembly.
I have to confess this is my first attempt at something like this and I am
having real problems.

For test purposes I created the following code

My Code in the ASPX page was:

Sub page_load
Response.Write(PathName())
End Sub

Function PathName() as string
Return Server.Mappath("/data")
End Function

This works absolutely fine
But when I transfer this to a code behind file and open the page I get the
error message

'PSolutions.DataAccess' is not a valid base class because it does not extend
class 'System.Web.UI.Page'.

In the ASPX file I have this code
<%@ page inherits="PSolutions.DataAccess" src="DataAccess.vb" %>

Sub Page_Load
Response.Write(PathName())


In the code behind file DataAccess.vb I have this code

Imports System
Imports System.Web

Namespace PSolutions
Public Class DataAccess
Public Function PathName() as String
Return HttpContext.Current.Server.Mappath("/Data") '<-- Got
this from the Microsoft web site.
End Function

End Class

End Namespace
End Sub

If I try to compile the vb file I get a message saying

name 'HttpContext' is not declared.


So at the moment I can't get anything to work.

Can anyone point me in the right direction.
As I said this is very new to me and I'm still learning so excuse any
amateur mistakes.

Many Thanks
Nick
 
L

LOZANO-MORÁN, Gabriel

That's because your class must inherit from System.Web.UI.Page. By the way
your Page_Load() needs to handle to MyBase.Load and it must match the
signature of the Load:

Public Class DataAccess
Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write(PathName())
End Sub

Private Function PathName() As String
Return Server.MapPath("/data")
End Function

Gabriel Lozano-Morán
 
N

Nick

Thanks for that.
As I said I'm very new to this so I understand the first part regarding
inheritance so I will amend my code.
But you have totally lost me with the Page_Load explanation.
If you could explain that further that would be great but thank you for the
other stuff anyway.

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top