Small textbox, read file error

Hello

I'm testing a code verification (people entere a code and then the code gets written to a file and read by the program to change labels and acces to certain parts of the program).

This is the code I used:

Dim var1 As String

TextBox1.Text = var1

My.Computer.FileSystem.WriteAllText("c:\test.txt", var1, True)

Dim filereader As String

filereader = My.Computer.FileSystem.ReadAllText("c:\test.txt")

If (filereader) = ("12345") Then

TextBox1.Text = "testuser1"

But when I press the button that makes this run, the file is created, the text but the text (var1) is not written. I know the mistake is somewhere with Var1 or the textbox but don't know how to fix it.

Bye and thanks in advance.



Answer this question

Small textbox, read file error

  • Pradeep Raj

    I fixed it. I needed to put .text after textbox1 for it to be an allowed type. Now I got another problem with it.

    It just keeps adding the input to the file instead of overwriting how do I clear the file before the input is put in

     

    Found the command to delete the file.

    Thanks for the help guys.


  • Anujpathania

    I made the var1 string because otherwise I got an error that it was the wrong variable to write to a file.
  • KrazyKevin

    If (filereader) = ("12345") Then

    Why the brackets

    Have you set a breakpoint in this code and stepped through it to see what var1 is being set to Have you checked the file on disc to see what it contains



  • Edward V. Wright

    The file is created, it's just Var1 that isn't being written to it.

    I have changed it to a manual text and then everything worked (with or without the brackets).


  • Joshua Sells

    First thing: what do you believe you are doing with these lines

    Dim var1 As String
    TextBox1.Text = var1

    You are creating a string variable. It starts off empty. Then, you make an assignment: you assign the text property of the text box to the value of Var1 - which is empty: the text box is now empty.

    When you write to your file, you write Var1 to the file - it's still empty. That's why you get an empty file.

    You need to assign var1 the text value property of textbox1 (presumably, you ultimately want to write the text that a user has entered in the text box). So you would change the assignment, thus:

    var1 = TextBox1.Text

    This will assign the Text Property of the text box to var1.



  • Sacristy

    var has no reason to exit. Textbox1.Text is ALSO a string, so what else would you want it to be The point is, instead of making var contain the text of Textbox1, you're making Textbox1.Text contain the value of var. You have

    Textbox1.Text = var

    when you should have

    var = Textbox1.Text

    Or even better, get rid of var ( it's useless ) and pass in Textbox1.Text instead.



  • bljacobs

    http://msdn2.microsoft.com/en-us/library/system.io.file.writealltext.aspx

    If you read the docs, or your intellisense, you'll see that your last parameter should be false if you want to overwrite the file.



  • MasterG152

    I did, the error I made was that I had forgotten to add the .text after textbox1 so that it gave an error.
  • SnowLover

    *sigh* I'm so tired. I can't believe I looked straight through that.



  • Small textbox, read file error