[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Saving Data

Posted on 2006-05-06
7
Medium Priority
?
193 Views
Last Modified: 2010-04-23
Hi, I am new to vb.net.

I have a form with some fields on it.  I want to be able to save the data on the form and have it retrieved next time the program runs.  This is not a database application and there will not be more than one record.  I basically just want to save some user settings.  What is easiest way of doing this?
0
Comment
Question by:jet46
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:tolgaong
ID: 16622932
It will be more easy to use a text file I think. Or XML if you like

I personally do not recommend registry for this purpose because, some users do not have write access to the registry -for security reasons- and it is hard to back up that data.
0
 
LVL 5

Author Comment

by:jet46
ID: 16622940
How is this done?
0
 
LVL 12

Accepted Solution

by:
gbzhhu earned 2000 total points
ID: 16623041
The best way I know is to create a class that has the same fields as your form,  Say class Data add all the fields that you need.  Also add public property for each private field (so that you can access the data later on in the form)

Make sure these directives are included at the top of your class

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

Then add these methods to the class

    public void SerializeNow()
    {
        File f=new File(@"c:\temp.dat");
        Stream s=f.Open(FileMode.Create);
        BinaryFormatter b=new BinaryFormatter();
        b.Serialize(s,this);
        s.Close();
    }

    public void DeSerializeNow()
    {
        File f=new File(@"c:\temp.dat");
        Stream s=f.Open(FileMode.Open);
        BinaryFormatter b=new BinaryFormatter();
        this=(yourClassName)b.Deserialize(s);
        s.Close();
    }

Where yourClassName is the name you gave your class.

When you are ready to serialize your data do this (in the form)

yourClassName cts = new yourClassName();
cts.SerializeNow();

File will be saved in the path given
....

When you want to re restore user saved data do this (in the form)

yourClassName cts = new yourClassName ();
cts.DeSerializeNow();

Then you can access the properties of the class

MessageBox.Show(cts.SomeProperty.ToString());

Code is typed in NotePad so may have some syntax errors

If you want to save XML instead it is a similar process, let me know and i will post code here

Cheers





0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Author Comment

by:jet46
ID: 16773823
Sorry it took so long to get back.  What language is that?  How would this be done in VB?
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 16788616
That was C#, can you convert to VB.net?  if not let me know and I will do it but I am busy right now maybe later on today (maybe my lunch time break)
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 16958429
The only person who provided a solution is me and user didn't continue participating, therefore I believe I deserve the points
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question