Home > database >  how to search textbox in dapper with database MS-Access in VB.NET
how to search textbox in dapper with database MS-Access in VB.NET

Time:10-13

How to read Select in dapper with database MS-Access in VB.NET? I tried doing a search in the textbox but it didn't work what was wrong with my code.

Private Sub FillDataGridView()
    Dim param As New DynamicParameters()
    param.Add("SELECT * FROM Contact WHERE @Nme='' OR Nme LIKE '%' @Nme '%'", txtSearch.Text.Trim())
    Dim list As List(Of Contact) = oledbCon.Query(Of Contact)("Select ContactID,Nme,Mobile,Address from contact", param, commandType:=CommandType.Text).ToList()

    dgvContact.DataSource = list

    dgvContact.Columns(0).Visible = False
End Sub

Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
    If Not Me.btnSearch.IsHandleCreated Then Return
    Try
        FillDataGridView()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

CodePudding user response:

I have never used dapper before, but literally the first example in their GitHub readme shows how to execute a query and map the results to a strongly typed List (here).

It would look something along these lines:

Dim contacts = oledbCon.Query(Of Contact)("SELECT ContactID, Nme, Mobile, Address FROM contact WHERE Nme='' OR Nme LIKE @NmeLike", New With {
    .NmeLike = $"%{txtSearch.Text.Trim()}%"
}).ToList()
  • Related