Home > Software engineering >  How to make this SQL statement work using where
How to make this SQL statement work using where

Time:10-14

I have the following code, I want to make a query with the where statement, but it does not give me any results, could you help me

public List<Persona> Listarwhere()
{
    List<Persona> oLista = new List<Persona>();

    using (SQLiteConnection conexion = new SQLiteConnection(cadena))
    {
        conexion.Open();

        Persona obj = new Persona();
        Form1 idcliente = new Form1();
        //int cliente = int.Parse(idcliente.txtidpersona.Text);

        string query = "select IdPersona, Nombre, Apellido, Telefono from Persona where idpersona = @idpersona";

        SQLiteCommand cmd = new SQLiteCommand(query, conexion);
        cmd.Parameters.Add(new SQLiteParameter("@idpersona", idcliente.txtidpersona.Text));
        cmd.CommandType = System.Data.CommandType.Text;

        using (SQLiteDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                oLista.Add(new Persona()
                    {
                        IdPersona = int.Parse(dr["IdPersona"].ToString()),
                        Nombre = dr["Nombre"].ToString(),
                        Apellido = dr["Apellido"].ToString(),
                        Telefono = dr["Telefono"].ToString()
                    });
            }
        }
    }

    return oLista;
}

I need to pass the data of this query in several textbox

CodePudding user response:

You should try to pass in your 'idpersona' as a parameter to your method. ... maybe something like this:

public List<Persona> Listarwhere(string id) // or (int id)
{
    List<Persona> oLista = new List<Persona>();

    using (SQLiteConnection conexion = new SQLiteConnection(cadena))
    {
        conexion.Open();

        Persona obj = new Persona();
        //Form1 idcliente = new Form1();
        //int cliente = int.Parse(idcliente.txtidpersona.Text);

        string query = "select IdPersona, Nombre, Apellido, Telefono from Persona where idpersona = @idpersona";

        SQLiteCommand cmd = new SQLiteCommand(query, conexion);
        cmd.Parameters.Add(new SQLiteParameter("@idpersona", id));
        cmd.CommandType = System.Data.CommandType.Text;

        using (SQLiteDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                oLista.Add(new Persona()
                    {
                        IdPersona = int.Parse(dr["IdPersona"].ToString()),
                        Nombre = dr["Nombre"].ToString(),
                        Apellido = dr["Apellido"].ToString(),
                        Telefono = dr["Telefono"].ToString()
                    });
            }
        }
    }

    return oLista;
}
  • Related