0

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'

ElectricRay81
  • 121
  • 11
  • 1
    `I get errors` Why would you not tell us what the errors are? – LarsTech Jun 28 '22 at 15:45
  • I can but I actually thought people understood why I get these errors. My apologies above the error message – ElectricRay81 Jun 28 '22 at 15:48
  • You ALWAYS need to specify EXACTLY what errors you get and WHERE you get them. The system provides that diagnostic information for a reason. I would assume that the issue is that you're calling `DataBindings.Add` multiple times for the same property. You can't add a binding for a property if there already is one. You need to remove the old one first or else modify the old one. That said, there's no need to add new bindings at all if you're still binding to the same `BindingSource`. – user18387401 Jun 28 '22 at 15:59
  • Thanks it makes fully sense next time I will add this information immediately. Now regarding the error. It happens only when this function is called for a second time. So indeed I would need to remove it or empty it or so. But where or how do I do that? – ElectricRay81 Jun 28 '22 at 16:25

0 Answers0