Bruce
Bruce

Reputation: 577

How to save data into application vb.net ? (Not use database or txt file)

I a newbie in VB.net. I have a small project that save data from textbox. (just simple: create textbox, fill my name to textbox and save it).

But I don't want use database or txtfile because if I use like that, I will have 2 file: my app.exe and database file. I don't know whether there have anyway to save data into this my app.exe. Just have 1 file app.exe, database is included in this app (App is same to excel file: just have 1 file excel which can fill data and save)

Upvotes: 0

Views: 847

Answers (2)

Fabio
Fabio

Reputation: 32445

Obviously you have a class Student (from your comment)

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

For binding to DataGridView you can use BindingList<Student>.

Then when you close your application save in JSON format.

var data = _yourBindingList.ToList();
var serializedData = Newtonsoft.Json.JsonConvert.SerializeObject(data);
System.IO.File.WriteAllText(@"fileName.anyExtensionYouWant", serializedData);

When application starts load data from the file and deserialize it.

var data = System.IO.File.ReadAllText(@"fileName.anyExtensionYouWant", serializedData);
var loadedStudents = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Student>>(data);

_yourBindingList = new BindingList(loadedStudents);
yourDataGridView.DataSource = _yourBindingList;

Upvotes: 2

fedeteka
fedeteka

Reputation: 963

The easy way is to save the text to a txt file

My.Computer.FileSystem.WriteAllText

https://msdn.microsoft.com/es-es/library/27t17sxs(v=vs.90).aspx

So

My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt", "This is new text to be added.", False)

on your're case can be

My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt", textbox1.text, False)

If you need to use MyDocuments or Desktop folders you need to use relative system routes

Upvotes: 1

Related Questions