Error retrieving record from the DB in the Data layer for use in an ActionResult within the ASP Controller. Net MVC 5

0

I am running a billing system in ASP .Net MVC with 3 layers. I just need to record the detail of the sale, because the sale already inserts me. In the case of the detail I extract all the fields that are required to insert the detail, except the sales code that is self-generated (Example: 'V001').

That's why I want to do select top (1) * from VENTAS and extract the 'V' and zeros (0) to add 1. Then it would be V001 > --- 1 (+1) > V002.

Could something like that be done or will there be conflict?

[HttpPost]
    public ActionResult GuardarVenta(string RUC,double VEN_TOT,List<VentadetaEntity>ListadoDetalles)
    {
        VentaEntity x = new VentaEntity();
        x.RUC = RUC.Trim();
        x.VEN_TOT = VEN_TOT;
        ViewBag.mensaje = venta.agregarVentaBL(x);

        VentaEntity a = venta.ultimaVentaBL();
        //a.VEN_COD;



        //VentadetaEntity y = new VentadetaEntity();
        //y.VEN_COD = "V003";

        foreach (var item in ListadoDetalles)
        {
            VentadetaEntity d = new VentadetaEntity();

            d.VEN_COD = a.VEN_COD;
            //d.VEN_COD = "V003";
            d.PROD_COD = int.Parse(item.PROD_COD.ToString());
            d.DET_CANT = int.Parse(item.DET_CANT.ToString());
            d.DET_PRE = double.Parse(item.DET_PRE.ToString());

            ViewBag.mensaje = venta.agregarVentadetaBL(d);


            //VentadetaEntity d = new VentadetaEntity("V003",PROD_COD,DET_CANT,DET_PRE);
        }

        //y.PROD_COD = int.Parse(PROD_COD);
        //y.DET_CANT = DET_CANT;
        //y.DET_PRE = DET_PRE;

        //ViewBag.mensaje = venta.agregarVentadetaBL(y);

        return Json(true);

        //return RedirectToAction("ListarCliente");

    }

Data Layer:

Currently, when I give it to save, I get the error that can not find data, why?

    
asked by J. Carlos 25.07.2017 в 06:33
source

1 answer

0

select top (1) * from VENTAS this statement does not ensure that you return the last value and if from the data brought from this statement you try to generate the following sales code this may be repeated and may cause errors as you have designed the table.

On the other hand, you have the error in that you do not read the SqlDataReader . To be able to access the data it contains you must "read" that reader, for this you can use the method bool Read() of SQLDataReader which will advance a "row" if it has not reached the end of these.

SqlDataReader dr = cmd.ExecuteREader();

while(dr.Read())//Avanza a la siguiente fila si existe y devuelve true en caso de que avance
{
    //Ahora si podríamos leer los datos de la fila actual
    dr["VEN_COD"].ToString();
}

Anyway, for your specific case if you want to read the first row I recommend you use the bool NextResult() method:

SqlDataReader dr = cmd.ExecuteREader();

if(dr.NextResult())//Avanza a la siguiente fila si existe y devuelve true en caso de que avance
{
    //Ahora si podríamos leer los datos de la fila actual
    dr["VEN_COD"].ToString();
}
    
answered by 25.07.2017 в 09:31