Link to home
Start Free TrialLog in
Avatar of Benny Theunis
Benny TheunisFlag for South Africa

asked on

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.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

In the second loop, you are referencing cbx.Text, and not ctrl.Text.
Avatar of Benny Theunis

ASKER

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.
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 ?? ""
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.
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

?? 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.
ASKER CERTIFIED SOLUTION
Avatar of Benny Theunis
Benny Theunis
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I figured it out on my own by doing some research on the Net.