open file, get variables

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
okay, i'm creating a very simple log- in program. This is what it does, you type in a username and password, hit the register button. you can then enter a username and password in the login box, if it matches a textbox will say "welcome " + uservariable. You can register multiple users. However, I want the variables to be remembered once the program is closed, so your registered users stay.
I have been told by a friend i need to look into opening and writing word documents. Can someone tell me how to do this, and then how to retrieve the written data in the word document as a variable?
 
Yea, you do need to consider the first run, where no user file may exist. I would, rather then Open or Create, just check if the file exists, it not, create it. If it didn't exist, then you would skip the deserialization part.

Your probably getting the "empty string" because you are deserializing from the freshly created file, which is empty.
If you'll put the IfFileExists Else SkipDeserialization code in there I think you'll overcome your problem.

Glad to see your apparently making progress with the approach! Please fire the questions off if you get more.
 
I don't have a hot clue how to check to see if the file exists. Tell me if this sounds right: If there is no file, I create user.cfg. I then open it and save some default information to it so i don't get that empty error. the default information could be an administrator user and password. After that is done, deserialization shouldn't be a problem right? if the file is there, I simply open it and deserialize it.
 
Last edited:
Okay, I believe I got it to work. By that I mean I'm getting no errors, and the testing I've done has succeded. To test it though, I'm going to need to know the code for the log in button. In otherwords, how do I check the input of the username and password textboxes against all the usernames and passwords stored in alAllUsers ? I've never used an array list before, so I don't know how to do this. I'm almost done, but I need the code!!!
 
VB.NET:
For Each Var as TypeInArray In YourArray

Next Var

so if you had an ArrayList consisting of strings then:
VB.NET:
For Each str As String In Users
  If str = LoginNameLookingFor Then
    'yea...
  End If
Next str
 
Yea, remember above that we declared that class to store the username and password in, and then we stored the instance of that class in our arraylist.

So, when you are checking the user name and password that has been provided, you would iterate through each instance of the user class in the arraylist to check for a valid match.

Something like:

(Sorry for mistakes, typing this up without my IDE)

VB.NET:
Dim blnLoginAccepted as boolean = false


For each myUser as clsUsers in alAllUsers

 if (Me.txtUsername.text = myUser.strPassword) AND (Me.txtUsername.text=myUser.strUsername) then
  blnLoginAccepted = true
  'do login code for an accepted user
 endif

Next

'if you make it out of the for..next with blnLoginAccepted still equal to false, then you had no user/pass matches.
 
edited because raven answered my question before i could ask it =)

although I seem to get an error that says: Object reference not set to an instance of an object

It refers to the register buttons code:

myUser.strPassword = Me.PassTextBox.Text
myUser.strUsername = Me.UserTextBox.Text
 
Last edited:
Likely your myUser object isnt initialized.

Do you have the line:

VB.NET:
Dim myUser As New clsUsers

in your button event? If not, you need that. That declares myUser as a new instance of clsUsers that you can use to hold the information for registration. If you do have that, something else is off...post your whole button event and we'll look.
 
Okay, I got past that now thaks to your advice. However something must be going wrong in either the saving or the loading, because it doesn't recognize the usernames and passwords i log in with once I restart the program.

PS: It won't let me dispose the filestream, so i got rid of that code. Is that part of the problem? It says this when i try to dispose

'System.IO.FileStream.Protected Overridable Sub Dispose(disposing As Boolean)' is not accessible in this context because it is 'Protected'.
 
Last edited:
Use the watch feature of Visual Studio to look at the variable and see if there is any data in the arraylist before you serialize it to file and after you deserialize the file back into the array.

Also, have you verified data is making it out to the file? You can open the file after you have serialized the data to it and closed the file stream, it will just look like really weird text.
 
couple of things. I'm afraid I don't know what you mean when you say the watch feature. second of all, I opened up the file with notepad and saw weird text, and also the username and password clearly in plain text. NOT VERY SECURE!! but I'm not going to worry about that now. I know there is information saved there, so the problem must be with the loading part. any ideas?
 
okay, I found my own mistake. Before when i told it to save the default information at startup, I forgot to say to deserialize it. Now I got a new error! YAY PROGRESS! it says:
BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 3.2054.
 
Ah, past one problem and on to another. Your using a BinaryFormatter object to both serialize and deserialize, correct?

I've not seen that error before, but a Google of the message implies its normally caused by using a BinaryFormatter on one end, and SOAP formatter on the other...
 
here is my code for form load

Dim myUser As New clsUsers
myUser.strPassword = "password"
myUser.strUsername = "Administrator"

alAllUsers.Add(myUser)


Dim myFileStream As New FileStream(Application.StartupPath & "\Users.cfg", FileMode.OpenOrCreate)
Dim myBinaryFormatter As New BinaryFormatter
myBinaryFormatter.Serialize(myFileStream, alAllUsers)

alAllUsers = DirectCast(myBinaryFormatter.Deserialize(myFileStream), ArrayList)
myUser = Nothing
myFileStream.Close()

myBinaryFormatter = Nothing

As far as I can tell, I use a binary formatter for serializing and deserializing.
 
Last edited:
EUREKA!!! It's working. Here is what the problem was. In the previous code(on form load) I both serialized, then deserialized the code. I don't know why that made it not work, but I removed the serialize part, as it is not needed in the beggining, and it works. I'm so happy!!!
 
Oh wow, sorry, totally over looked that line or I'd have told you! Yea, no need to serialize there so thats going to cause problems...though that was an interesting error message.

All is working now I assume? Hope it was a decent learning experience!
 
Back
Top