Error in query in bank with Linq

4

Good evening! I'm trying to perform a query on a mysql table, using linq, and present that result in a gridview to the user. I have the table below:

I'm not looking for all the columns in this table, just a few for this I'm using the code below in my class ManipulaDados:

public GridView exibeCarteira(string cepf, GridView tb)
    {
        try
        {
            bancotccEntities bc = new bancotccEntities();

            var crt = from cart in bc.carteira
                      where cart.cpf == cepf
                      select new
                      {
                          Código = cart.codigo,
                          Valor = cart.valoracao,
                          Quantidade = cart.qtdacao,
                          Total = cart.vtotalacao,
                          Investido = cart.vinvestido,
                          ValorTotal = cart.vtotalacao
                      };
            tb.DataSource = crt;
            tb.DataBind();
            return tb;
        }
        catch (Exception e1)
        {
            throw new Exception(e1.Message.ToString());

        }

    }

This method is called by a method of another class the method is this:

 public GridView mostraCarteira(string cpf, GridView gv)
    {
        try
        {
            ManipulaBanco mp = new ManipulaBanco();
            return mp.exibeCarteira(cpf, gv);
        }
        catch (Exception e4)
        {

            throw new Exception(e4.Message.ToString());
        }
    }

To test this method I'm using the Web class below:

 public partial class ExibeCarteira : System.Web.UI.Page
{
    string cpf;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                exibirCarteira();
            }
        }
        catch (Exception ex)
        {

            throw new Exception(ex.Message.ToString());
        }
    }
    private void exibirCarteira()
    {
        try
        {
            Trataformes tf = new Trataformes();
            this.gvcarteira = tf.mostraCarteira(cpf, gvcarteira);

        }
        catch (Exception e1)
        {

            throw new Exception(e1.Message.ToString());
        }
    }
    protected void gvcarteira_SelectedIndexChanged(object sender, EventArgs e)
    {

    }}

When compiling I get the error below that I can not solve, can anyone help me?

  

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead, populate the DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList (). For ASP.NET WebForms you can bind to the result of calling ToList () on the query or use Model Binding, for more information see go.microsoft.com/fwlink/?LinkId=389592.

    
asked by anonymous 09.06.2014 / 01:46

1 answer

2

Call ToList() or ToArray() in variable crt , because DataSource needs an enumeration. I changed its method being tb ( GridView ) variable by reference is more practical, but could be by return.

public void exibeCarteira(string cepf, ref GridView tb)
{
    try
    {
        bancotccEntities bc = new bancotccEntities();
        var crt = from cart in bc.carteira
                  where cart.cpf == cepf
                  select new
                  {
                      Código = cart.codigo,
                      Valor = cart.valoracao,
                      Quantidade = cart.qtdacao,
                      Total = cart.vtotalacao,
                      Investido = cart.vinvestido,
                      ValorTotal = cart.vtotalacao
                  };
        tb.DataSource = crt.ToList();
        tb.DataBind();      
    }
    catch (Exception e1)
    {
        throw new Exception(e1.Message.ToString());

    }
}

How to use:

Trataformes tf = new Trataformes();
tf.mostraCarteira(cpf, ref gvcarteira);

Obs: Change% of Código (with emphasis) to Codigo (without accent) would be a good practice

    
09.06.2014 / 01:58