webclient is causing stack overflow

Luc

Well-known member
Joined
Nov 29, 2005
Messages
59
Programming Experience
1-3
I'm new to asp.net 2.0 and I'm trying to make a simple newspage wich can be changed by changing a textdocument, the code goes as following:

VB.NET:
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim logged_in As Boolean = False
    Dim first As Boolean = True
    Dim wc As New System.Net.WebClient()
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblNews.Text = ""

        txtlogged.Text = "Not logged in"
        txtlogged.ForeColor = Drawing.Color.Red

        Dim sr As New System.IO.StreamReader(wc.OpenRead("http://users.skynet.be/fa588483/news.txt"))
        Dim line$
        Do
            line = sr.ReadLine
            If Not line Is Nothing Then lblnews.Text &= line & "<br>"

        Loop Until line Is Nothing
        sr.Close()
        

        If first Then
            txtlogged.Text = "Not logged in"
            txtlogged.ForeColor = Drawing.Color.Red
            first = False
            transfer_login()

        End If

    End Sub

However I keep getting a stack overflow exception at the line containing
Dim sr As New
VB.NET:
System.IO.StreamReader(wc.OpenRead("http://users.skynet.be/fa588483/news.txt")
I checked the link and its valid. Anny help?
 
Hi

I tested your code and it worked fine for me, however I did create a separate sub and called it from the page load in the following manner:
' in page load:
If Not Page.IsPostBack Then

Me.TryWC()
EndIf

' Then I wrote a pre test loop instead of a post test and tested it again and
' it worked fine too

Protected Sub TryWC()
lblNews.Text =​
""

txtLogged.Text = "Not logged in"

txtLogged.ForeColor = Drawing.Color.Red

Dim sr As New System.IO.StreamReader(wc.OpenRead("http://users.skynet.be/fa588483/news.txt"))

Dim line As String

'Do

' line = sr.ReadLine

' If Not line Is Nothing Then

' lblNews.Text &= line & "<br>"

' End If
'Loop Until line Is Nothing

While Not sr.EndOfStream
line = sr.ReadLine​

If Not line IsNothing Then

lblNews.Text &= line & "<br>"

End If
End While
sr.Close()

If first Then

txtLogged.Text =​
"Not logged in"

txtLogged.ForeColor = Drawing.Color.Red

first = False

'transfer_login()
End If

End Sub

Unless there is something with transfer_login() that would cause the page to postback I do not see anything wrong with it.
 
Back
Top