Parameterizing DataSet in C#

Question:

I created a search screen with a textbox , a button and a 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 .

Here is 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;
}

Answer:

I don't understand why you split the procedure into two methods. Anyway, the error doesn't seem to be the DataSet binding with the GridView , but with the 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();
}
Scroll to Top