VB.NET Read/Write from several textboxes to an XML file and vice versa

Afternoon,

I’m using Visual Studio Express 2012 for a new VB.Net project.

I want to use a XML file with my program. So when the user clicks the button to get to the Settings area (below) of my windows form, the form upon opening will read the XML file from \\ServerA\test\settings.xml and populate all of the textboxes where there is data already set.

Settings Windows Form
Then if the user wants to add a new row of data in the available text boxes or maybe update one of the textboxes they will just type this in and click on the Save Settings button which will write the new information to the XML file.

My textboxes are labeled as CDtxtbox1, DGtxtbox1, DTtxtbx1, DSTtxtbx1, CDtxtbox2, DGtxtbox2, DTtxtbx2, DSTtxtbx2… and so on, each row of textboxes being related to each other.

I’m not sure how to start this off as Im new to VB.net.

Would appreciate any sample code to get me started with this.

Thanks,
N
LVL 1
KevinAsked:
Who is Participating?
 
Ioannis ParaskevopoulosConnect With a Mentor Commented:
Well,

Coding even for 4 rows and 4 columns would be a huge overhead if you see the attached code. Create a project and replace the created form1.vb and form1.designer.vb with the attached. Then copy the attached xml in "C:\"

You'll see why i insist on using the grid.

Giannis
Form1.Designer.vb
Form1.vb
Sample.xml
0
 
Ioannis ParaskevopoulosCommented:
Hi,

Are you sure textboxes are the way to go? Have you checked the DataGridView control?

I am asking because you may actually need the textboxes....but i would choose the DataGridView.

Giannis
0
 
KevinAuthor Commented:
Using the datagridview was my first thought, but it's not going to work for my purpose. So this is why i decided to choose textboxes.

Basically i just need whatever is stored in these text boxes written to the xml file when the save settings button is pushed, and upon loading this setting form the xml file is read and the information is displayed back in the text boxes.

N
0
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.

 
KevinAuthor Commented:
Out of curiosity why would you choose a datagridview?

I dont want the data users input here stored in a database type view.
0
 
Ioannis ParaskevopoulosCommented:
It will not be connected to a DB. You will only represent your data in the datagridview.

If you need the textboxes, you can post a sample xml and we may work it out.

Personally i will give you both solutions and you will decide which suits you the most.

Giannis
0
 
KevinAuthor Commented:
Jyparask,

I dont have an XML file.

Basically I was thinking of possibly using an .ini file to begin with, but after reading several forums, the majority of all said that .ini files were no longer used to store information with vb.net and that XML was the new way to go. So this is why i decided to go this route.

I'm really just looking for the easiest working route to go with of saving information from my settings area, so that my main windows form can use that information for another purpose.

Reason why i thought textboxes would be better then the dgv was that i could tell my application to look at cdtxtbx1 for the value or dgtxtbx1 etc and use that to make my application do the other work.

Though i am only guessing here as I am still learning.

I'm flexible to whatever method can be used to read/write/store data just dont really know where to start.

What do you think?

N
0
 
Ioannis ParaskevopoulosCommented:
Ok,

A quick question... How many sets would you have? Is 6 the most? If you have a definite (and i would add a small) amount of settings, it is ok, but if you have many and the quantity may vary, then you have a problem with textboxes. With grid views you just add or remove rows as you see fit.

Giannis
0
 
KevinAuthor Commented:
Really 4 would be the most I just made it 6 as a just in case, like spares basically.
0
 
KevinAuthor Commented:
Ahh ok i see what you mean. With the dgv I can add as much rows as I want without touching the code.

Thank you very much for this example.

But there is one thing I dont quite understand and maybe I overlooked.

My main windows form will need this information in the xml to perform other functions.

How would it know which bit of information to use from the xml? Like for example if I were to create a button and a textbox and code in: if button2 is pushed it will show one set of informationin the textbox, whats going to stop it from showing another set instead of the one intended.

Like how is the information defined?

Would the placement of the information in the XML file be what I am to link with?

Hope you understand what I am referring to, please let me know if you still are not clear.
0
 
Ioannis ParaskevopoulosCommented:
I think that you would have to link with the first columns(Code).  So if you would filter the table, something like:

dim searchrow as datarow
searchrow = dt.Select("Code = 'FTO'")

Open in new window


would return the first row of the xml file. Now to access other columns in that row you simply have to do the following:

dim DocGroupas string
DocGroup = searchrow("DocGroup")

Open in new window


Now the varriable DocGroup has the value of "Documentation".

Giannis
0
 
KevinAuthor Commented:
I see.

Problem is that that's not going to work because the user may change the FTO to something else for another code.

I'll have a look around on how to make this work.

Thank you so much for the first bit though.

N
0
 
Ioannis ParaskevopoulosCommented:
I think you can make that column not updateable. You may also define a specific set of rows in the xml so you may code them in your code, and the user won't be able to change the first column as it will be readonly
0
 
KevinAuthor Commented:
Yes I follow you. But I dont want this to be an option of the first column\row being readonly.

No worries.. Ill have a looksee around the net.
0
 
Ioannis ParaskevopoulosCommented:
We,, in the page load you may add the following after loading the data:

dgvSettings .Columns("Code").ReadOnly = True

Open in new window


This way the column which has the name "Code" will be readonly....

Giannis
0
 
KevinAuthor Commented:
Yes I understand this.

But I dont want any of the fields to be readonly as the user must be able to change all of the settings in each of the rows.

So this is why the readonly value will not help me in this instance.

Hmm unless... I have another column in there labeled "id" or something like this and have it go by that, the user wont need to touch it and that could be readonly.

But going this route, i will need to make sure more rows cannot be created in the datagridview then the amount of id's i put in.

Is there a way to put a limit on how many rows can be added within the dgv?
0
 
Ioannis ParaskevopoulosCommented:
In design time click on the grid on the top right corner there will be a triangle.
Click on the triangle and a popup will appear. There is an option to allow or not adding rows.
0
 
KevinAuthor Commented:
Perfect.

Thanks
0
 
Ioannis ParaskevopoulosCommented:
Anytime :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.