Open for the second time a file give errors

61 Views Asked by At

I have a program that start work. I can open and store files. But when I open a file (e.g. File 1) the program reads it and than when I want to open a second file (e.g. File 2) I get errors.

Opening files goes with bindingSource.DataSource I assume the issue is there but I have tried to set it to Null but that give other errors.

        private void openToolStripMenuItem_Click(object sender, EventArgs e)     
    {
        string CheckConnection;
        _bindingSource.DataSource = null;
        
        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            ofd.Filter =  "XML File (*.xml)|*.xml";
            ofd.Title = "Open Client Profile File";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                lblCompanyName.Enabled = true;
                lblClientNumber.Enabled = true;
                lblSiteName.Enabled = true;
                lblMachineTotal.Enabled = true;
                lblImo.Enabled = true;
                cmbMachineName.Enabled = true;
                lblPower.Enabled = true;
                lblMachineType.Enabled = true;
                lblFrequency.Enabled = true;
                lblSpeed.Enabled = true;


                //Deserialize
                _root = HelperXml.DeserializeXMLFileToObject<XmlRoot>(ofd.FileName);
                ClientFileName = ofd.FileName;                                                      //Store current filename

                lblCompanyName.ForeColor = Color.Black;
                lblCompanyName.Text = "Company Name: " + _root.CompanyProfile.CompanyName;
                lblSiteName.Text ="Sitename: " + _root.CompanyProfile.SiteName;
                lblImo.Text = "IMO: " + _root.CompanyProfile.Imo.ToString();
                lblMachineTotal.Text = "Machine Total: " + _root.CompanyProfile.MachineTotal.ToString();
                lblClientNumber.Text = "Clientnumber: " + _root.CompanyProfile.ClientNumber.ToString();

                
                _bindingSource.DataSource = _root.MachineProfiles.ToList();
                cmbMachineName.DataSource = _bindingSource;
                lblPower.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.NominalPower));                   
                lblFrequency.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.Frequency));
                lblMachineType.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.TypeDescription));
                lblSpeed.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.NominalSpeed));

                
                _bindingSource2.DataSource = _root.MachineMeasurements.ToList();
                dataGridView1.DataSource = _bindingSource2;

                dataGridView1.DataBindings.Add("Text", _bindingSource2, nameof(MachineMeasurement.MeasurementDate));

            }
        }


        CheckConnection = statusLblDevice.Text.ToString();
        //Enable "ImportData" button function only when a clientfile has been loaded and a device has been detected.
        if (CheckConnection.Contains("VM25")== true && lblSiteName.Text != null)
        {
            importDataToolStripMenuItem.Enabled = true;
        }
    }

Above is the code that opens a file this is connected to other classes but I think there is not the issue. I think prior to run this code all should be empty or so. Does anybody know how I could do this?

Without adding any code I get this error:

System.ArgumentException: 'This causes two bindings in the collection to bind to the same property.

This is the error message I get when I added the code for making the bindingSource null

System.ArgumentException: 'Cannot bind to the property or column NominalPower on the DataSource.

Parameter name: dataMember'

0

There are 0 best solutions below