Problem saving data to database

Asked

Viewed 55 times

0

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

Follow picture:

inserir a descrição da imagem aqui

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);
        }

Code of the Control (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 in a console application, it is working perfectly.

  • What does troubleshooting present as an error? If I remember correctly it gets some relevant information in this case.

  • Hello @Gabrielcoletta managed to solve the problem, thank you.

2 answers

2

Probably the error is happening due to a conversion that is returning an exception. More precisely on these lines:

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

I added a try catch in your code (to identify possible errors) and changed the method Parse for 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}");
    }
}

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

ChemicalCtrl ctrl = new ChemicalCtrl();
ctrl.Create(newChemical);

0


Hello, the problem was very simple, I found strange, however resolved...

The following error was because I had another class on the system in which 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 in order to run the software.

What I found strange was the following: There was no compilation error and the console showed me no execution fault.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.