locked
Newbie needs help with first program. RRS feed

  • Question

  • User1062395471 posted

    Hi Folks,

    I've got to a point...where I need some help.

    I'm uploading a text file to a text box -- got that working fine:

    Private Sub btnStreamRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStreamRead.Click
            Dim objFile As New System.IO.StreamReader("C:\test5.txt")
            Dim strContents As String
            strContents = objFile.ReadToEnd()
            objFile.Close()
            objFile.Dispose()
            TextBox1.Text = strContents

        End Sub

    But I can't figure out how to save that "strContents" to another .txt file.  I've created a "SaveToFile" button  :)

    I've tried StreamWriter, but it wants a Dim and when I go to Dim strContents I get the error that it has already been Dimmed...

    Can somebody help me?

    Thanks!

    Fred


    Tuesday, March 2, 2010 8:20 PM

Answers

  • User406085191 posted

    Hi for the first error

    use 

    Imports

    System.IO

    & for second error is because tht u declared 'strContents' inside the 

    btnStreamRead_Click() so delcare it outside

    or

     for second error modify the code as

    Private Sub savTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savTextBox.Click
            Dim objFile As New System.IO.StreamReader("C:\test5.txt")
            Dim strContents As String
            strContents = objFile.ReadToEnd()
            Dim swOutput As StreamWriter = New StreamWriter("C:\Output.txt")
            swOutput.Write(strContents)
            swOutput.Flush()
            swOutput.Close()
            objFile.Close()
            objFile.Dispose()
        End Sub
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 4, 2010 3:01 AM
  • User-952121411 posted

    The last few posts have cleared up the coding issues, but if you want to learn and understand how the StreamReader and StreamWriter work, check out the following MSDN links:

    StreamReader Class:
     
     
    StreamWriter Class:
     

    Error 2 - Name 'strContents' is not declared
    <!---->
    <!---->

     This is caused due to a the scope of the variable you declared named 'strContents'.  It was declared within the 'btnStreamRead_Click' event.  However, when using the 'Dim' keyword, that creates a procedural or block level variable.  Beyond that scope the variable is no longer accessible.  In a non-web app you could make that variable 'Public', but in the case of the web is it 'Stateless' so variables do not hold values between server postbacks; so just making it 'Public' will solve compile issues but not the overall problem.  In your case, your value was saved to a control 'TextBox1' which uses ViewState to retain its value beyond a postback, and is why you can use its value directly in the 'savTextBox_Click' event to solve the issue.  ViewState and SessionState and (2) common methods to retain values in web application to persist postbacks.

    The code posted in previous posts will fix the immediate issue, but based on some of your questions, I want to provide you with some links that may help as you go forward:

    How to: Control the Scope of a Variable:

    http://msdn.microsoft.com/en-us/library/w5d4e988.aspx

    Scope in Visual Basic:

    http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx

    Access Levels in Visual Basic:

    http://msdn.microsoft.com/en-us/library/76453kax.aspx

    Underpinnings of the Session State Implementation in ASP.NET:

    http://msdn.microsoft.com/en-us/library/aa479041.aspx

    ASP.NET Session State:

    http://msdn.microsoft.com/en-us/library/ms972429.aspx

    Hope this helps! Smile

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 4, 2010 9:38 AM

All replies

  • User1759999623 posted

    You create a StreamWriter called swOutput:

    Dim swOutput As StreamWriter = New StreamWriter("C:\Output.txt")
    swOutput.Write(strContents)
    swOutput.Flush()
    swOutput.Close()


     

    Tuesday, March 2, 2010 10:02 PM
  • User1062395471 posted

    Thank you Christopher, I really appreciate it!

    Ok, I added the code:

    Private Sub btnStreamRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStreamRead.Click
            Dim objFile As New System.IO.StreamReader("C:\test5.txt")
            Dim strContents As String
            strContents = objFile.ReadToEnd()
            objFile.Close()
            objFile.Dispose()
            TextBox1.Text = strContents

        End Sub

        Private Sub savTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savTextBox.Click
            Dim swOutput As StreamWriter = New StreamWriter("C:\Output.txt")
            swOutput.Write(strContents)
            swOutput.Flush()
            swOutput.Close()

        End Sub

    ---

    but now am getting 2 errors:

    Error 1 - Type 'StreamWriter' is not defined

    Error 2 - Name 'strContents' is not declared

    Any ideas on this?

    Thanks!

    Fred




    Wednesday, March 3, 2010 7:02 AM
  • User1330806141 posted

     Error 2 - Name 'strContents' is not declared - because strContents is a local variable not global variable. 

     

    Try this

    Private Sub savTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savTextBox.Click
       Dim swOutput As System.IO.StreamWriter
       Dim myFile As String = TextBox1.Text
       Dim myFilePath As String = "C:\Output.txt"
       swOutput = System.IO.File.CreateText(myFilePath)
       swOutput.Write(myFile)
       swOutput.Close()

    End Sub

     

     

    Wednesday, March 3, 2010 4:36 PM
  • User1759999623 posted

    Try this:

        Private Sub savTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savTextBox.Click
            ' There is apparently no Imports line for System.IO, so put it here
            Dim swOutput As StreamWriter = New System.IO.StreamWriter("C:\Output.txt")
            swOutput.Write(TextBox1.Text) ' strContents is not declared in this method, so use the control value
            swOutput.Flush()
            swOutput.Close()
        End Sub


     

    Wednesday, March 3, 2010 11:23 PM
  • User406085191 posted

    Hi for the first error

    use 

    Imports

    System.IO

    & for second error is because tht u declared 'strContents' inside the 

    btnStreamRead_Click() so delcare it outside

    or

     for second error modify the code as

    Private Sub savTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savTextBox.Click
            Dim objFile As New System.IO.StreamReader("C:\test5.txt")
            Dim strContents As String
            strContents = objFile.ReadToEnd()
            Dim swOutput As StreamWriter = New StreamWriter("C:\Output.txt")
            swOutput.Write(strContents)
            swOutput.Flush()
            swOutput.Close()
            objFile.Close()
            objFile.Dispose()
        End Sub
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 4, 2010 3:01 AM
  • User-952121411 posted

    The last few posts have cleared up the coding issues, but if you want to learn and understand how the StreamReader and StreamWriter work, check out the following MSDN links:

    StreamReader Class:
     
     
    StreamWriter Class:
     

    Error 2 - Name 'strContents' is not declared
    <!---->
    <!---->

     This is caused due to a the scope of the variable you declared named 'strContents'.  It was declared within the 'btnStreamRead_Click' event.  However, when using the 'Dim' keyword, that creates a procedural or block level variable.  Beyond that scope the variable is no longer accessible.  In a non-web app you could make that variable 'Public', but in the case of the web is it 'Stateless' so variables do not hold values between server postbacks; so just making it 'Public' will solve compile issues but not the overall problem.  In your case, your value was saved to a control 'TextBox1' which uses ViewState to retain its value beyond a postback, and is why you can use its value directly in the 'savTextBox_Click' event to solve the issue.  ViewState and SessionState and (2) common methods to retain values in web application to persist postbacks.

    The code posted in previous posts will fix the immediate issue, but based on some of your questions, I want to provide you with some links that may help as you go forward:

    How to: Control the Scope of a Variable:

    http://msdn.microsoft.com/en-us/library/w5d4e988.aspx

    Scope in Visual Basic:

    http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx

    Access Levels in Visual Basic:

    http://msdn.microsoft.com/en-us/library/76453kax.aspx

    Underpinnings of the Session State Implementation in ASP.NET:

    http://msdn.microsoft.com/en-us/library/aa479041.aspx

    ASP.NET Session State:

    http://msdn.microsoft.com/en-us/library/ms972429.aspx

    Hope this helps! Smile

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 4, 2010 9:38 AM