1
I used the method below to add parameters using the interface IDbCommand
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EstudoDotNet.Comum.Dominio;
using System.Data.SqlClient;
using System.Data;
public List<Laboratorio> ListarLaboratorio(int IdCidade)
{
#region Conexão
DALManager objDalManager = new DALManager();
string connectionString = objDalManager.StringConexao;
string connString = objDalManager.StringConexao;
#endregion
#region Comando
StringBuilder sbQuery = new StringBuilder();
sbQuery.Append("SELECT IdLaboratorio, NmLaboratorio, IdCidade ");
sbQuery.Append("FROM Laboratorio lab ");
#endregion
#region Filtros
StringBuilder sbFiltro = new StringBuilder();
if (IdCidade != null)
sbFiltro.Append("lab.IdCidade = @IdCidade");
if (!string.IsNullOrEmpty(sbFiltro.ToString()))
sbQuery.Append(" WHERE ").Append(sbFiltro.ToString());
#endregion
#region Oredencao
sbQuery.Append(" ORDER BY NmLaboratorio ");
#endregion
#region Execução
List<Laboratorio> lista = new List<Laboratorio>();
try
{
using (IDbConnection conn = new SqlConnection(connString))
{
conn.Open();
using (IDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sbQuery.ToString();
if (IdCidade != null)
{
cmd.Parameters.AddWithValue("@IdCidade", IdCidade);
}
cmd.Connection = conn;
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Laboratorio laboratorio = new Laboratorio();
laboratorio.IdLaboratorio = Convert.ToInt64(reader["IdLaboratorio"]);
laboratorio.NmLaboratorio = reader["NmLaboratorio"].ToString();
lista.Add(laboratorio);
}
}
return lista;
}
}
}
catch (Exception ex)
{
throw ex;
}
#endregion
}
Returns the following error:
I tried referencing System.Dataq.IDataParameter
, but I didn’t find.
Options available for cmd.Parameters.
:
Change to SqlCommand
works normally.
How to fix this error?
I did not understand your doubt. You can give more details or explain otherwise?
– Maniero
@bigown I added more information.
– Jothaz
Put in your directions
using
at the beginning of the archive.– Maniero
I’m thinking that the problem is somewhere else. I can’t see why this error is happening. The
namespaces
necessary are there. I don’t know if you are changing consistently. Something tells me that your code is worse now than before.– Maniero
@Bigown Beginners are able to commit unspeakable and incomprehensible barbarism. The worst is that with
SqlCommand
works. The method returns aList
to fill aDropDownList
of laboratories according to the city selected in anotherDropDownList
,– Jothaz
You can’t do what you’re doing. You’re editing and every hour the question is different than it was. There is no way anyone can answer and the answer is right. Ask one thing and keep the question that way. You can add information to help answer that but not completely change what you were asked.
– Maniero
To add to what @bigown said, if you want to change the question, the best is to open a new question, leave the current one intact and accept one of the answers
– dcastro
@Bigown Sorry I have no intention of disturbing or compromising the quality of the forum. And even with the issues the scope of the question remained the same. I only added more information to facilitate help, so much so that the star responded with the solution. I hope this incident does not occur again, if perhaps something similar happens I already know that the way is to create a new post. Once again I’m sorry.
– Jothaz
No, the question is completely different from the original version, even if the subject is the same. This is not a forum but even in a forum if you edit the question, it makes it impossible. The difference is that a forum you can on that topic complementing new questions. Here we have no topics, we have questions, only. dcastro responded to the current version, not the original version. Cigano responded to the original version, his response is invalid for the current version.
– Maniero