Parameterizing Dataset in C#

Asked

Viewed 66 times

5

I created a search screen with a textbox, one button and a gridview. I would like the search results to appear on this Gridview but I can only do that with one 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;
}

1 answer

4


I didn’t understand why you separated the procedure into two methods. Anyway, the error doesn’t seem to be from the Binding of DataSet with the GridView, and yes with the method Select.

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();
}
  • 1

    Hello Morrison, all right? Look, sorry it took me so long to answer. So, this is a college job but I had already solved that I just didn’t have time to post here. Your tip to shorten the tbm code worked, I tested later but did not solve the problem because the error was in another class. Still my thanks for the attention Sorry for the bug, I’m beginner in this, a lot to learn yet

  • Tranquility. In need, we are there.

Browser other questions tagged

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