Solved

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

Posted on 2013-05-22
18
451 Views
Last Modified: 2013-05-23
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
0
Comment
Question by:nobushi
  • 9
  • 9
18 Comments
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
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
 
LVL 1

Author Comment

by:nobushi
Comment Utility
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
 
LVL 1

Author Comment

by:nobushi
Comment Utility
Out of curiosity why would you choose a datagridview?

I dont want the data users input here stored in a database type view.
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
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
 
LVL 1

Author Comment

by:nobushi
Comment Utility
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
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
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
 
LVL 1

Author Comment

by:nobushi
Comment Utility
Really 4 would be the most I just made it 6 as a just in case, like spares basically.
0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
Comment Utility
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
 
LVL 1

Author Closing Comment

by:nobushi
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
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
 
LVL 1

Author Comment

by:nobushi
Comment Utility
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
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
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
 
LVL 1

Author Comment

by:nobushi
Comment Utility
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
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
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
 
LVL 1

Author Comment

by:nobushi
Comment Utility
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
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
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
 
LVL 1

Author Comment

by:nobushi
Comment Utility
Perfect.

Thanks
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
Anytime :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now