HOW TO SEARCH IN VB.NET USING DATAGRIDVIEW AND TEXTBOX

clynnekent 21 Reputation points
2022-02-16T05:26:25.847+00:00

Good day everyone, Someone can help me. I want to search data from database using datagridview but i have a problem with my code.

This is my code.
databaseconnection.conn()
cn.Open()
Dim i As Integer
DataGridView1.Rows.Clear()

    cmd = New OleDbCommand
    cmd = New OleDbCommand("Select * from tblemprecord where Employee_lname like '%" & TextBox1.Text & "%' OR Employee_empid like '%" & TextBox1.Text & "%' OR Employee_fname like '%" & TextBox1.Text & "%'", cn)
    cmd.Connection = cn
    Using dtr = cmd.ExecuteReader()
        If dtr.HasRows Then

            While dtr.Read
                i += 1
                DataGridView1.Rows.Add(i, dtr.Item("Employee_id").ToString, dtr.Item("Employee_fname").ToString, dtr.Item("Employee_lname").ToString, dtr.Item("Employee_mname").ToString, dtr.Item("Employee_startdate").ToString, dtr.Item("Employee_gender").ToString, dtr.Item("Employee_employmentstatus").ToString, dtr.Item("Employee_designatory").ToString, dtr.Item("Employee_department").ToString)
            End While

        End If
    End Using
    cn.Close()

I'm using VB.NET 2022 and MS Access database.

Thanks
clynnekent

Developer technologies .NET Other
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-02-18T06:49:01.72+00:00

    Hi @clynnekent ,
    Please check that the table and column names in your sql statement match those in the database.
    And using parameters in SQL statements may help you.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.