Filter on gridview, how to do?

Asked

Viewed 226 times

2

I am trying to filter my gridview, but it is not returning anything on the grid. The parameters I pass are correct. But still nothing returns!

Here is the click of the button:

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

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

    SELECT 
        *
    FROM
        t_livros
    WHERE
        @OPCAO like @CAMPO_PESQUISA
    ORDER BY 
        nome_livro

1 answer

0

Try using dynamic SQL within your SP. You 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)

Browser other questions tagged

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