Parameterizing DataSet in C #

5

I created a search screen with textbox , button and gridview . I would like the search results to appear in this Gridview but I can only do this with a Dataset . In this case, I needed to find a way to pass parameters through this DataSet .

Follow the code for evaluation:

protected void btnPesquisar_Click(object sender, EventArgs e)
{
    string pesquisa = txtBusca.Text.Trim();
    CarrinhoCompraBD bd = new CarrinhoCompraBD();
    CarrinhoCompras select = bd.Select(pesquisa);
    DataSet ds = new DataSet();
    ds = bd.Select(); //Esta linha é onde o visual studio aponda o erro
    lblTeste.Text = "Resultado da busca para: " + CarregaResultado(pesquisa) +".";
    gvResultado.DataSource = ds.Tables[0].DefaultView; //CarregaResultado(pesquisa);
    gvResultado.DataBind();
}
protected string CarregaResultado(string pesquisa)
{
    CarrinhoCompraBD bd = new CarrinhoCompraBD();
    CarrinhoCompras select = new CarrinhoCompras();
    select = bd.Select(pesquisa);

    gvResultado.Visible = true;
    pesquisa = select.Busca;
    return pesquisa;
}
    
asked by anonymous 19.06.2016 / 18:32

1 answer

4

I did not understand why you separated the procedure into two methods. In any case, the error does not appear to be binding of DataSet with GridView , but with Select method.

Your code can be simplified to:

protected void btnPesquisar_Click(object sender, EventArgs e)
{
    string pesquisa = txtBusca.Text.Trim();
    CarrinhoCompraBD bd = new CarrinhoCompraBD();
    CarrinhoCompras select = bd.Select(pesquisa);

    DataSet ds = new DataSet();
    // ds = bd.Select(); //Esta linha é onde o visual studio aponda o erro
    gvResultado.Visible = true;
    lblTeste.Text = "Resultado da busca para: " + select.Busca +".";
    gvResultado.DataSource = ds.Tables[0].DefaultView; //CarregaResultado(pesquisa);
    gvResultado.DataBind();
}
    
20.06.2016 / 01:14