I am trying to load a DataGridView with a list of filtered partners, but at the time of loading I load all the partners plus those that I filter. I don't see where the error could be. The filter is applied while you write, to explain myself better: if I enter fra it has to show all the members, whose names start with those 3 letters, such as Franco, Francisco, Franca, etc.
The textBox code is as follows:
private void txt_filtro_nombre_TextChanged(object sender, EventArgs e)
{
if (!String.IsNullOrWhiteSpace(txt_filtro_nombre.Text))
{
cargarGrilla(svSocio.getSocioPorNombre(txt_filtro_nombre.Text));
}
}
The code getSocioPorNombre(textbox)
that brings me the data is as follows:
public IList<DTO_Socio> getSocioPorNombre(string nombre)
{
IList<DTO_Socio> lista = new List<DTO_Socio>();
IList<Socio> listaSocios = dao.getSociosPorNombre(nombre);
foreach (Socio socio in listaSocios)
{
lista.Add(mapSocio.getDtoSocio(socio));
}
return lista;
}
which communicates with the DAO that brings me the data from the database and has this code:
public IList<Socio> getSociosPorNombre(string nombre)
{
string sp = "listar_socios_por_nombre";
var parametro = new SqlParameter("@nombre", nombre);
parametro.SqlDbType = SqlDbType.VarChar;
IList<Socio> listaSocios = getSocios();
DataTable tabla = helper.consultarStoredProcedureConUnParametro(sp, parametro);
foreach (DataRow fila in tabla.Rows)
{
listaSocios.Add(mapper(fila));
}
return listaSocios;
}
The helper code that communicates with the database:
public DataTable consultarStoredProcedureConUnParametro(string sp, SqlParameter parametro)
{
SqlConnection cnn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
DataTable tabla = new DataTable();
try
{
cnn.ConnectionString = string_conexion;
cnn.Open();
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sp;
cmd.Parameters.Add(parametro);
tabla.Load(cmd.ExecuteReader());
return tabla;
}
catch (SqlException ex)
{
MessageBox.Show("EXPLOTO EL HELPER", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw (ex);
}
finally
{
this.CloseConnection(cnn);
}
}
The code ofcargarGrilla():
dgv_socios.Rows.Clear();
if (lista != null)
{
foreach (DTO_Socio dto in lista)
{
dgv_socios.Rows.Add(new Object[]
{
dto.NumeroSocio,
dto.Nombre,
dto.Apellido,
dto.Dni
}
);
}
}
else
{
MessageBox.Show("No hay datos para mostrar", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
I can't see where the error is, for which it brings me all the partners + the filtered ones.
I leave you an image of what I say
Run the SP on the database to see if that failed, but it works great
Franco, reviewing the code you posted, there is a statement that caught my attention in the
getSociosPorNombre
.It is the instruction where you initialize the variable
listaSocios
and although you did not publish the code of the methodgetSocios()
, from the name it has I assume that it is responsible for finding all the partners.That variable should be initialized with an empty list.