0

My aim is to save the all form data via button click (as opposed to upon closing). To that end, I've used the example given in the following thread. Saving the form state then opening it back up in the same state

I've tried to adapt my code to the best of my ability, but nothing happens, and there are no errors shown. What am I doing wrong?

Here's the relevant parts of my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace WindowsFormsApplication1
{
    public partial class frmPayroll : Form
    {
        SaveData sd = new SaveData();
        public frmPayroll()
        {
            InitializeComponent();
        }
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            writeConfig();
        }
        private void writeConfig()
        {
            using (StreamWriter sw = new StreamWriter("config.xml"))
            {
                sd.Married = rdoMarr.Checked;
                sd.PayPd = cbPayPd.Text;
                sd.Allow = cbAllow.Text;
                sd.Gross = txtGross.Text;
                sd.Fit = txtFit.Text;
                sd.Soc = txtSoc.Text;
                sd.Med = txtMed.Text;
                sd.NetPay = txtNet.Text;
                sd.PayPd = cbPayPd.Text;
                XmlSerializer ser = new XmlSerializer(typeof(SaveData));
                ser.Serialize(sw, sd);
            }
        }
        private void frmPayroll_Load(object sender, EventArgs e)
        {
            if (File.Exists("config.xml"))
            {
                loadConfig();
            }
            sd.Married = rdoMarr.Checked;
            sd.PayPd = cbPayPd.Text;
            sd.Allow = cbAllow.Text;
            sd.Gross = txtGross.Text;
            sd.Fit = txtFit.Text;
            sd.Soc = txtSoc.Text;
            sd.Med = txtMed.Text;
            sd.NetPay = txtNet.Text;
        }
        private void loadConfig()
        {
            XmlSerializer ser = new XmlSerializer(typeof(SaveData));
            using (FileStream fs = File.OpenRead("config.xml"))
            {
                sd = (SaveData)ser.Deserialize(fs);
            }
        }
    }
    public struct SaveData
    {
        public bool Married;
        public string PayPd;
        public string Allow;
        public string Gross;
        public string Fit;
        public string Soc;
        public string Med;
        public string NetPay;
    }
}
Community
  • 1
  • 1
Gatorneck
  • 5
  • 4

1 Answers1

0

You are loading your object by deserializing.

But Where are you assigning the states back to your controls?

look at frmPayroll_Load function.

You are trying to assign the data back to the object again.

You have to assign data back to form controls.

Should be something like this (you may need to apply data conversions if required):

rdoMarr.Checked = sd.Married;
.
.
.
.
txtFit.Text = sd.Fit;
.
.
.
.
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • THANKS!! It worked as soon as I tried that. I was racking my brain trying to figure out what I was doing wrong. I'm taking a c# class, but haven't taken any of the prerequisite, so its sometimes a little difficult to understand what many of the tutorials or examples on MSDN are saying. Now to attempt to add a clear button. – Gatorneck Apr 09 '17 at 19:58
  • I tried to up-vote your answer, but I don't have enough rep yet. – Gatorneck Apr 09 '17 at 19:59
  • Sorry about that, and thanks for the help. I'm new here, but the deed is now done – Gatorneck Apr 10 '17 at 02:55