Error: Must declare the scalar variable

0

Can anyone help me with this?

Must declare the scalar variable "@hospitalId".

PendingController:

public async Task<ActionResult> Index()
{
    if (!Cookies.Exists("hid"))
        return RedirectToAction("Index", "Login", new { area = "Entrar" });

    var hospitalId = int.Parse(Cookies.GetCookie("hid"));
    var lista = await Dia1Service.GetPendenciasByUser(hospitalId);
    return View(lista);
}

Dia1Service:

    public async Task<IEnumerable<Dia1>> GetPendenciasByUser(int hospitalId)
            {
                return await _dia1Repository.GetPendenciasByUser(hospitalId);
            }

**Dia1Repository:**

public async Task<IEnumerable<Dia1>> GetPendenciasByUser(int hospitalId)
        {
            const string query = @"SELECT d1.PatientId AS Id,
                                   c.inpac AS Iniciais,
                                   CASE 
                                    WHEN (CONVERT(DATE,r.RandomizacaoData , 103) = CONVERT(DATE,d1.dtd1 , 103)) OR d1.dtd1 IS NULL
                                        THEN r.RandomizacaoData
                                    WHEN (CONVERT(DATE,r.RandomizacaoData , 103) < CONVERT(DATE,d1.dtd1 , 103)) 
                                        THEN CONVERT(datetime,d1.dtd1 , 103)
                                    ELSE r.RandomizacaoData
                                   END DataRandomizacao
                            FROM Basics_Dia1 d1
                            LEFT JOIN Basics_Cadastro c ON c.PatientId = d1.PatientId AND d1.StatPree = 1
                            LEFT JOIN basics_Randomizacao r ON r.PatientId = c.PatientId AND r.CentroId = c.HospitalId
                            WHERE c.HospitalId = @hospitalId AND c.StatPree = 1 AND c.Removido = 0";

            var cn = Db.Database.Connection;
            var lista = await cn.QueryAsync<Dia1>(query, new { Hospital = hospitalId });

            return lista;
        }
    
asked by anonymous 22.03.2018 / 21:53

1 answer

1

Explanation

The value of the Hospital property of the anonymous object used in the command parameter does not match the value specified in SQL . Change the declaration line of the variable list to the one suggested in the example below:

Example

var lista = await cn.QueryAsync<Dia1>(query, new { HospitalId = hospitalId });
    
22.03.2018 / 21:57