Error: Must declare the scalar variable

Asked

Viewed 569 times

0

Can someone help me with this?

Must declare the scalar variable "@hospitalId".

Pendenciacontroller:

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

1 answer

1


Explanation

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

Example

var lista = await cn.QueryAsync<Dia1>(query, new { HospitalId = hospitalId });
  • Thanks guy I didn’t even notice, my eyes are hooked already

  • It happens. You need to accept the answer.

Browser other questions tagged

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