Error in database query with Linq

Asked

Viewed 133 times

4

Good night! I am trying to perform a query in a mysql table, using English, and present this result in a gridview for the user. I have the table below: inserir a descrição da imagem aqui

I’m not searching all columns of this table, just a few for that I’m using the code below in my Manipulated class:

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 that:

 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 am 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 I compile I get the error below that I am unable to solve, someone can help me?

Data Binding directly to a store query (Dbset, Dbquery, Dbsqlquery, Dbrawsqlquery) is not supported. Instead populate a 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.

  • I do not know the mistake, if you have there to pass! something else Código with accentuation, it is not a good practice !!! and maybe it is the mistake!

  • Ops. Failed to pass error : Data Binding directly to a store query (Dbset, Dbquery, Dbsqlquery, Dbrawsqlquery) is not supported. Instead populate a 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 http://go.microsoft.com/fwlink/? Linkid=389592.

  • missed if give a Tolist() in crt, I put an answer!

1 answer

2


Call ToList() or ToArray() in the variable crt, because, the DataSource needs an enumeration. I changed your method being the tb (GridView) variable by reference becomes 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 Código (with accentuation) to Codigo (without accentuation) would be a good practice

  • 1

    I made the modifications you said, it was no mistake, but I think something was missing because it is not displaying.

  • Break a point on that line tb.DataSource = crt.ToList(); and check if there was a return?

  • 1

    Okay, what worked was that I wasn’t adding a number to the number, so he couldn’t return any value to me. Thank you so much for your help.

  • @user9090, legal ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.