-1

I have a list of contacts with Id, Name, Adress etc. and i want to do a query to return all those contacts.

public List<MyAgenda> mostrarContatos()
    {
        MyAgenda ma = new MyAgenda();
        DataSet ds = new DataSet();
        OleDbConnection conn = new OleDbConnection();

        try
        {



            conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["myconn"].ConnectionString);

            //2ª Passo: Definir query
            string query = "select * from Table where Id like '" + ma.Id + "%'";

            //3ª Passo: Preprarar para Executar
            OleDbDataAdapter da = new OleDbDataAdapter(query, conn);

            //4º Passo: Executa a query
            da.Fill(ds, "Dados");

            //Devolvo DataSet
            return ds;
        }
        catch (Exception err)
        {
            throw new Exception("ERRO: " + err.Message);
        }
        finally
        {
            conn.Close();
        }

It seems i cant return the Ds and im not sure if my query is totally correct. Any help please?

  • I don't know anything about your table structure, so I don't know what will be returned from this query. This query should return all columns for all rows where the ID starts with `ma.Id`. – maniak1982 Dec 03 '16 at 15:46
  • This code doesn't compile. You declare to return a MyAgenda but instead you return a DataSet. A part from this how we could know if your query is correct or not. What is ID? What are the columns of Table? Please post a [mcve] if you want to be helped – Steve Dec 03 '16 at 15:47
  • If Id is a string you query could be correct if you Id is number .. you should use = instead of like and don't use wildchar (%) – ScaisEdge Dec 03 '16 at 15:49

1 Answers1

1

You are trying to return DataSet as List<MyAgenda> which is impossible. You have to use return ds.Tables[0].AsEnumerable().Select(...).ToList(); Answer is here

Convert DataSet to List

Community
  • 1
  • 1
Lemjur
  • 338
  • 3
  • 11