Coming from classic asp, I'm a bit in the dark here.
I want to dispaly the content of a file in a literal server control.
I've created a class utils.vb
Imports System.IO
Public Class Utils
Inherits System.Web.UI.Page
Public Function ReadFile(ByVal pFile) ' As String
Dim ForReading As Integer = 1
Dim fso As Object
Dim ts
Dim s
fso = CreateObject("Scripting.FileSystemObject")
ts = fso.OpenTextFile(MapPath(pFile), ForReading)
s = ts.ReadAll
ts.Close()
ReadFile = s
End Function
End Class
on the aspx page I use
Dim utl As Utils = New Utils
Dim lBody As String = utl.ReadFile( strFile )
litContent.Text = lBody
If I have the ReadFile function in the code behind page of the calling
aspx page it works.
When I move the function to the utils.vb class I get the following
error:
System.NullReferenceException: Object reference not set to an instance
of an object
Line 11: Dim s
Line 12: fso = CreateObject("Scripting.FileSystemObject")
Line 13: ts = fso.OpenTextFile(MapPath(pFile), ForReading)
Line 14: s = ts.ReadAll
Line 15: ts.Close()
Source File: c:\inetpub\wwwroot\project\Utils.vb Line: 13
I figure its the dim ts part of ReadFile function. But ...?
/MortenHi, hainsiman,
It is the call to MapPath() that is throwing the exception. Try:
HttpContext.Current.Server.MapPath()
instead.
Greetings
Martin
"hansiman" <hansi@.hotmail.com> wrote in message
news:s0fr30hrhlqctbupmovvkspidhj4m5v5nr@.4ax.com...
> Coming from classic asp, I'm a bit in the dark here.
> I want to dispaly the content of a file in a literal server control.
> I've created a class utils.vb
> Imports System.IO
> Public Class Utils
> Inherits System.Web.UI.Page
> Public Function ReadFile(ByVal pFile) ' As String
> Dim ForReading As Integer = 1
> Dim fso As Object
> Dim ts
> Dim s
> fso = CreateObject("Scripting.FileSystemObject")
> ts = fso.OpenTextFile(MapPath(pFile), ForReading)
> s = ts.ReadAll
> ts.Close()
> ReadFile = s
> End Function
> End Class
> on the aspx page I use
> Dim utl As Utils = New Utils
> Dim lBody As String = utl.ReadFile( strFile )
> litContent.Text = lBody
> If I have the ReadFile function in the code behind page of the calling
> aspx page it works.
> When I move the function to the utils.vb class I get the following
> error:
> System.NullReferenceException: Object reference not set to an instance
> of an object
> Line 11: Dim s
> Line 12: fso = CreateObject("Scripting.FileSystemObject")
> Line 13: ts = fso.OpenTextFile(MapPath(pFile), ForReading)
> Line 14: s = ts.ReadAll
> Line 15: ts.Close()
> Source File: c:\inetpub\wwwroot\project\Utils.vb Line: 13
> I figure its the dim ts part of ReadFile function. But ...?
> /Morten
Hi Martin
I still get the error even when I change the mappath part (see below).
The strange thing is that if I put the ReadFile function in the code
behind page of the aspx file it works with simply MatPath (pFile)...
utils.vb
Public Function ReadFile(ByVal pFile)
Dim ForReading As Integer = 1
Dim fso As Object = CreateObject("Scripting.FileSystemObject")
Dim ts, s
ts =
fso.OpenTextFile(MapPath(HttpContext.Current.Serve r.MapPath(pFile)),
ForReading)
s = ts.ReadAll
ts.Close()
ReadFile = s
End Function
Call from aspx file:
Dim utl As Utils = New Utils
Dim lBody As String = utl.ReadFile( strFile )
litContent.Text = lBody
Error:
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.
Source Error:
Line 11: Dim ts, s
Line 12: 'ts = fso.OpenTextFile(MapPath(pFile), ForReading)
Line 13: ts =
fso.OpenTextFile(MapPath(HttpContext.Current.Serve r.MapPath(pFile)),
ForReading)
Line 14: s = ts.ReadAll
Line 15: ts.Close()
Source File: c:\inetpub\wwwroot\customersurveyanalysis\mmaUtils .vb
Line: 13
On Thu, 26 Feb 2004 10:47:31 +0100, "Martin Dechev"
<detcheff_@.hotmail.com> wrote:
>Hi, hainsiman,
>It is the call to MapPath() that is throwing the exception. Try:
>HttpContext.Current.Server.MapPath()
>instead.
>Greetings
>Martin
>"hansiman" <hansi@.hotmail.com> wrote in message
>news:s0fr30hrhlqctbupmovvkspidhj4m5v5nr@.4ax.com...
>> Coming from classic asp, I'm a bit in the dark here.
>>
>> I want to dispaly the content of a file in a literal server control.
>>
>> I've created a class utils.vb
>>
>> Imports System.IO
>>
>> Public Class Utils
>> Inherits System.Web.UI.Page
>> Public Function ReadFile(ByVal pFile) ' As String
>> Dim ForReading As Integer = 1
>> Dim fso As Object
>> Dim ts
>> Dim s
>> fso = CreateObject("Scripting.FileSystemObject")
>> ts = fso.OpenTextFile(MapPath(pFile), ForReading)
>> s = ts.ReadAll
>> ts.Close()
>> ReadFile = s
>> End Function
>> End Class
>>
>> on the aspx page I use
>>
>> Dim utl As Utils = New Utils
>> Dim lBody As String = utl.ReadFile( strFile )
>>
>> litContent.Text = lBody
>>
>> If I have the ReadFile function in the code behind page of the calling
>> aspx page it works.
>>
>> When I move the function to the utils.vb class I get the following
>> error:
>>
>> System.NullReferenceException: Object reference not set to an instance
>> of an object
>>
>> Line 11: Dim s
>> Line 12: fso = CreateObject("Scripting.FileSystemObject")
>> Line 13: ts = fso.OpenTextFile(MapPath(pFile), ForReading)
>> Line 14: s = ts.ReadAll
>> Line 15: ts.Close()
>>
>> Source File: c:\inetpub\wwwroot\project\Utils.vb Line: 13
>>
>> I figure its the dim ts part of ReadFile function. But ...?
>>
>> /Morten
>
> fso.OpenTextFile(MapPath(HttpContext.Current.Serve r.MapPath(pFile)),
should be:
fso.OpenTextFile(HttpContext.Current.Server.MapPat h(pFile)),
Greetings
Martin
ahhh, of course... thanx
On Fri, 27 Feb 2004 14:14:23 +0100, "Martin Dechev"
<detcheff_@.hotmail.com> wrote:
>> fso.OpenTextFile(MapPath(HttpContext.Current.Serve r.MapPath(pFile)),
>should be:
>fso.OpenTextFile(HttpContext.Current.Server.MapPat h(pFile)),
>Greetings
>Martin
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment