Save fields from TabPages to xml file

I have managed to successfully display an array from a xml file into TabPages. Each TabPage have an array of textbox fields and combo fields.

I have tried using a dataset, but when I try to save the fields to my xml file, the combofield save as unchanged.

My code for saving:
foreach (TabPage tp in dynamicTabControl.TabPages)
                {

                    foreach (Control ctrl in tabPageOp.Controls)
                    {
                        //Do Something...
                    }
                    foreach (Control ctrl in tabPageA.Controls)
                    {
                        dr["NewResult"] = cbx.Text;
                        dt.Rows.Add(dr);   //Add the row to a table
                    }
               }

Open in new window


Can anyone point me the right direction please?

Thank you.
Benny TheunisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
In the second loop, you are referencing cbx.Text, and not ctrl.Text.
0
Benny TheunisAuthor Commented:
Thanks Bob.

However, when I change it to ctrl.Text, I get the "NullReferenceException" error. "Use the "new" keyword to create an object or instance."

How can I get around this? I already used "cbBox = new ComboBox();" when I display the array from my xml file.
0
Bob LearnedCommented:
What is null, the "ctrl" reference, or ctrl.Text?  If it is the text, then you might want to use the null coalesce operator--ctrl.Text ?? ""
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Benny TheunisAuthor Commented:
Hi Bob

Sorry. But I'm a newbie to C#. Would you mind explaining it in simple terms please?

I tried the following:
if (ctrl is ComboBox)
                        {
                            drn["NewResult"] = ctrl;
                            //dta.Rows.Add(drn);
                        }

Open in new window

If I I use "dta.Rows.Add(drn);", then I get the error the row is already added. If I don't use it, the result in my xml file is still the same. The Combo/Dropdown field changed value does not show in my xml file.
0
Bob LearnedCommented:
If a DataRow already belongs to a table, then it cannot be added to another table, it needs to be imported.

Copying Data from one DataTable to Another using ImportRow
http://www.c-sharpcorner.com/UploadFile/mahesh/ImportRow07092006195427PM/ImportRow.aspx

foreach (DataRow dr in sourceTable.Rows) {
	destinationTable.ImportRow(dr);
}

Open in new window

0
Bob LearnedCommented:
?? Operator (C# Reference)
https://msdn.microsoft.com/en-us/library/ms173224.aspx

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.
0
Benny TheunisAuthor Commented:
Thanks Bob.

I figured it out.

foreach (TabPage tp in dynamicTabControl.TabPages)
                {
                            section.Add(new XElement("SectionName", tp.Text));

                    foreach (Control ctrl in tp.Controls)
                    {
                            if (ctrl is TextBox)
                            {
                                section.Add(new XElement("ItemName", ctrl.Text));
                            }
                            if (ctrl is ComboBox)
                            {
                                section.Add(new XElement("ItemResult", ctrl.Text));
                            }
                        }
                    }

doc.Root.Add(section);

Open in new window


I couldn't get the items under to my sections. They all are on the same level in my xml file. It's not pretty but, it helps.

Thanks for your support.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Benny TheunisAuthor Commented:
I figured it out on my own by doing some research on the Net.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

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.