I'm passing some parameters to a view where I need to get the return of this query. When defining the filters in the Where of my query, the query return reports the following error:
Invalid 'competition' column name.
I know it's my coding failure, but I declare in my where columns for query
public RelatorioDTO relatorio(DateTime compInicial)
{
// vw_relatorio: View
string query = "select cpfCnpj from vw_relatorio where competencia <= @compInicial";
string myConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
SqlConnection connection = new SqlConnection(myConnectionString);
SqlCommand dbCommand = new SqlCommand(query, connection);
SqlParameter parameter = dbCommand.CreateParameter();
parameter.ParameterName = "@compInicial";
parameter.Value = compInicial;
parameter.DbType = DbType.DateTime;
dbCommand.Parameters.Add(parameter);
dbCommand.Connection.Open();
dbCommand.ExecuteReader();
// retornar a consulta
var retornoLista = new List<RelatorioDTO>();
if (((SqlDataReader)(myReader)).HasRows)
{
while (myReader.Read())
{
retornoLista .Add(FiltroRelatorio(myReader));
}
}
else
{
return null;
}
return retornoLista;
}
Returning item to item:
private RelatorioDTO FiltroRelatorio(IDataReader myReader)
{
var result = new RelatorioDTO ();
result.CpfCnpj = myReader["CpfCnpj"].ToString().ToUpper();
return result;
}