Problem saving data to the database

0

I created a simple application, but when I click on the action button responsible for saving the data, the program stops working.

Here's a picture:

Save button action: -

private void button_Click(object sender, RoutedEventArgs e)
        {
            Chemical newChemical = new Chemical();

            newChemical.ChemicalName = nameField.Text;
            newChemical.ChemicalFormula = formulaField.Text;
            newChemical.MW = Decimal.Parse(MWField.Text);
            newChemical.VFId = Int32.Parse(VFIDField.Text);

            ctrl.Create(newChemical);
        }

Control Code (ctrl):

public class ChemicalCtrl
{

    public ChemicalCtrl()
    { }

    public void Create(Chemical ch)
    {
        //throw new NotImplementedException();

        try
        {
            using (var repo = new Repository<Chemical>())
            {
                repo.Create(ch);
            }
        }
        catch(CustomExceptionsCtrl err)
        {
            Console.WriteLine(err);
        }

    }
}

DETAIL: I have already tested the 'Create ()' method on a console application, it is working perfectly.

    
asked by anonymous 14.03.2018 / 22:28

2 answers

0

Hello, the problem was very simple, I found it strange, but solved ...

The following error was because I have another class on the system where I had a property that was not mirrored in the database (I did not use Migrations to update it). To fix this, I had to comment on the two properties in the class to run the software.

What I found strange was the following: It did not give compilation error and the console did not show me any execution failure.

    
19.03.2018 / 12:16
2

The error is probably due to a conversion that is returning an exception. More precisely in these lines:

newChemical.MW = Decimal.Parse(MWField.Text);
newChemical.VFId = Int32.Parse(VFIDField.Text);

I added try catch to your code (to identify possible errors) and changed the method Parse to TryParse :

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        Chemical newChemical = new Chemical();

        newChemical.ChemicalName = nameField.Text;
        newChemical.ChemicalFormula = formulaField.Text;

        if (Decimal.TryParse(MWField.Text, out var mw))
        {
            newChemical.MW = mw;
        }
        else
        {
            MessageBox.Show("Valores inválidos para MW.");
            MWField.Focus();
            return;
        }

        if (Int32.TryParse(VFIDField.Text, out var vfid))
        {
            newChemical.VFID = vfid;
        }
        else
        {
            MessageBox.Show("Valores inválidos para VFID.");
            VFIDField.Focus();
            return;
        }

        ChemicalCtrl ctrl = new ChemicalCtrl();
        ctrl.Create(newChemical);
    }
    catch(Exception ex)
    {
        MessageBox.Show($"Erro ao criar: {ex.Message}");
    }
}

Furthermore, in your code there is no creation of the object ChemicalCtrl , which may be generating error as well. I added the creation of it, as well as the call to the Create() method:

ChemicalCtrl ctrl = new ChemicalCtrl();
ctrl.Create(newChemical);
    
15.03.2018 / 11:25