Filter in gridview, how to do?

2

I'm trying to do a filtering in my gridview, but it's not returning anything on the grid. The parameters that step are correct. But even so it does not return anything!

Here is the button click:

Private Sub btnPesquisar_Click(sender As Object, e As EventArgs) Handles btnPesquisar.Click

    Dim strOpcao As String
    Dim strPesquisar As String
    Dim objLivros As New ClasseLivros

    strOpcao = ddlPesquisar.SelectedValue
    strPesquisar = txtPesquisar.Text

    gvLivros.DataSource = objLivros.ObtemLivrosFiltrado(strOpcao, strPesquisar)
    gvLivros.DataBind()

End Sub

And here's my function:

Public Function ObtemLivrosFiltrado(ByVal strOpcao As String, ByVal strPesquisar As String) As DataSet

    Try

    Dim con As New SqlConnection(Conexao.ConnectionString)
    Dim da As New SqlDataAdapter("usp_lista_livros_filtrado", con)

    strPesquisar = String.Concat("'%", strPesquisar, "%'")

    da.SelectCommand.CommandType = CommandType.StoredProcedure
    da.SelectCommand.Parameters.AddWithValue("@OPCAO", strOpcao)
    da.SelectCommand.Parameters.AddWithValue("@CAMPO_PESQUISA", strPesquisar)

    Dim ds As New DataSet()

        da.Fill(ds, "t_livros")

        Return ds

    Catch ex As Exception
        Throw ex
    End Try

End Function

And here's my procedure:

    SELECT 
        *
    FROM
        t_livros
    WHERE
        @OPCAO like @CAMPO_PESQUISA
    ORDER BY 
        nome_livro
    
asked by anonymous 31.05.2015 / 17:24

1 answer

0

Try using dynamic SQL within your SP. You'll just have to change the way your SP does the query:

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = '    SELECT      *
                FROM        t_livros
                WHERE       ' + @OPCAO + ' LIKE ''' + @CAMPO_PESQUISA + '''
                ORDER BY    nome_livro'
EXEC(@SQL)
    
10.08.2018 / 13:35