Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-05-22
18
Medium Priority
?
465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
18 Comments
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39190213
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
ID: 39190652
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
ID: 39190675
Out of curiosity why would you choose a datagridview?

I dont want the data users input here stored in a database type view.
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 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39190724
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
ID: 39190889
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
ID: 39191032
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
ID: 39191144
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 2000 total points
ID: 39191258
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
ID: 39191676
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
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39191707
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
ID: 39191751
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
ID: 39191768
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
ID: 39191790
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
ID: 39191820
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
ID: 39191904
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
ID: 39191996
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
ID: 39192097
Perfect.

Thanks
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39192101
Anytime :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

704 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