Adding 2 rows of fields in a Database using C# (MVC)

Asked

Viewed 32 times

1

I am doing an Actionresult within a control to effect some purchase data. The idea is to buy and sell products (in this case Coins),negativando a Quantidade and positive Valor in case of sale and otherwise in case of Purchase, and taking these values and some more fields to be stored in a table.

Only at the time of sending the data to a database db.Movimento the fields do not exit correctly. In the display of the table data, the first entry exits the data correctly, with the exception of the Cd_moeda and Nm_moeda

Second inning, nothing comes out, it’s all on NULL

How do I add the two entries correctly?

Reported code:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Efetivar([Bind(Include = "Codigo,Dat,Tipo_ope,Cd_loja,Nm_loja,Cd_moeda,Nm_moeda,Cd_conta,Nm_conta,Historico,Descricao,Quantidade,Valor,Preco,Invoice")] OperacaoCV operacaoCV)
    {
        string wId = (string)Session["Cd_loja"];

        //operacaoCV = db.OperacaoCVs.Find(operacaoCV.Codigo);

        double wValor1;
        double wValor2;
        if (operacaoCV.Tipo_ope == "C")            //se o tipo operacao for Compra (C)
        {
            wValor1 = operacaoCV.Valor * (-1);
            wValor2 = operacaoCV.Quantidade;
        }
        else                                                           //se o tipo operacao for Venda
        {
            wValor1 = operacaoCV.Valor;
            wValor2 = operacaoCV.Quantidade * (-1);
        }

        if (ModelState.IsValid)
        {
           
            Movimento mv = new Movimento();
            mv.Cd_conta = operacaoCV.Cd_conta;
            mv.Cd_loja = operacaoCV.Cd_loja;
            mv.Nm_conta = operacaoCV.Nm_conta;
            mv.Nm_moeda = "Real";
            mv.Cd_moeda = 1;
            mv.Dat = operacaoCV.Dat;
            mv.Nm_conta = operacaoCV.Nm_conta;
            mv.Tp_operacao = operacaoCV.Tipo_ope;
            mv.Preco = operacaoCV.Preco;
            mv.Quantidade = operacaoCV.Quantidade;
            mv.Valor = wValor1;
            mv.Historico = operacaoCV.Historico;
            mv.Invoice = operacaoCV.Invoice;

            db.Movimento.Add(mv);
            db.SaveChanges();

            Movimento mv2 = new Movimento();
            mv.Cd_conta = operacaoCV.Cd_conta;
            mv.Cd_loja = operacaoCV.Cd_loja;
            mv.Nm_conta = operacaoCV.Nm_conta;
            mv.Nm_moeda = operacaoCV.Nm_moeda;
            mv.Cd_moeda = operacaoCV.Cd_moeda;
            mv.Dat = operacaoCV.Dat;
            mv.Nm_conta = operacaoCV.Nm_conta;
            mv.Tp_operacao = operacaoCV.Tipo_ope;
            mv.Preco = operacaoCV.Preco;
            mv.Quantidade = operacaoCV.Quantidade;
            mv.Valor = wValor2;
            mv.Historico = operacaoCV.Historico;
            mv.Invoice = operacaoCV.Invoice;

            
            db.Movimento.Add(mv2);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        return View(operacaoCV);
    }
  • 2

    In the second part the prefix should not be mv2 and not mv? It seemed kind of strange this wValor2 and Quantity.

  • It really was. It didn’t even cross my mind, thank you very much! In the Quantity part it is supposed to take the value and the quantity, and in one entry of these 2 fields it is the opposite of the other Ex.- Transaction type -> Purchase ; soon in mv will be -(value) and +(quantity) , while in mv2 it will be +(value) and -(quantity). It is a currency transfer operation.

  • Is that you attribute to mv.Valor wValor1, obtained from value, and in the second part wValor2, obtained from quantity.

1 answer

1

Look at your second Insert, it is with "mv", which is the first Insert has to put the "mv2".

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Efetivar([Bind(Include = "Codigo,Dat,Tipo_ope,Cd_loja,Nm_loja,Cd_moeda,Nm_moeda,Cd_conta,Nm_conta,Historico,Descricao,Quantidade,Valor,Preco,Invoice")] OperacaoCV operacaoCV)
{
    string wId = (string)Session["Cd_loja"];

    //operacaoCV = db.OperacaoCVs.Find(operacaoCV.Codigo);

    double wValor1;
    double wValor2;
    if (operacaoCV.Tipo_ope == "C")            //se o tipo operacao for Compra (C)
    {
        wValor1 = operacaoCV.Valor * (-1);
        wValor2 = operacaoCV.Quantidade;
    }
    else                                                           //se o tipo operacao for Venda
    {
        wValor1 = operacaoCV.Valor;
        wValor2 = operacaoCV.Quantidade * (-1);
    }

    if (ModelState.IsValid)
    {
       
        Movimento mv = new Movimento();
        mv.Cd_conta = operacaoCV.Cd_conta;
        mv.Cd_loja = operacaoCV.Cd_loja;
        mv.Nm_conta = operacaoCV.Nm_conta;
        mv.Nm_moeda = "Real";
        mv.Cd_moeda = 1;
        mv.Dat = operacaoCV.Dat;
        mv.Nm_conta = operacaoCV.Nm_conta;
        mv.Tp_operacao = operacaoCV.Tipo_ope;
        mv.Preco = operacaoCV.Preco;
        mv.Quantidade = operacaoCV.Quantidade;
        mv.Valor = wValor1;
        mv.Historico = operacaoCV.Historico;
        mv.Invoice = operacaoCV.Invoice;

        db.Movimento.Add(mv);
        db.SaveChanges();

        Movimento mv2 = new Movimento();
        mv2.Cd_conta = operacaoCV.Cd_conta;
        mv2.Cd_loja = operacaoCV.Cd_loja;
        mv2.Nm_conta = operacaoCV.Nm_conta;
        mv2.Nm_moeda = operacaoCV.Nm_moeda;
        mv2.Cd_moeda = operacaoCV.Cd_moeda;
        mv2.Dat = operacaoCV.Dat;
        mv2.Nm_conta = operacaoCV.Nm_conta;
        mv2.Tp_operacao = operacaoCV.Tipo_ope;
        mv2.Preco = operacaoCV.Preco;
        mv2.Quantidade = operacaoCV.Quantidade;
        mv2.Valor = wValor2;
        mv2.Historico = operacaoCV.Historico;
        mv2.Invoice = operacaoCV.Invoice;

        
        db.Movimento.Add(mv2);
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    return View(operacaoCV);
}

Browser other questions tagged

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