HI,
Im wondering if anyone can help me, my vb.net is not the best and i am trying to create a program that allows the user to search for staff.
i have created a database containing one table with details such as name, depatment, email address i have then created a form in vb containing the database headings as label along with a text box. i have also created a 'search' button.

i want the user to be able to type in a name in to the name text box and i want any reaults to display and then the user can select the appropriate one and i want the details to display i have connected the form to the databse but i dont know how to code the the search button to do what i want

Dani AI

Generated

described the requirement clearly and ’s TableAdapter example is a useful starting point for typed datasets. The following adds a compact, safe pattern for a search button when using MySQL + VB.NET (ADO.NET): run a parameterized LIKE query for partial matches, bind results to a grid for selection, and populate detail fields from the selected row.

A minimal ADO.NET pattern (Connector/NET) to run the search and bind results:

' Requires MySql.Data.MySqlClient
Dim sql As String = "SELECT id, firstname, lastname, department, email FROM staff WHERE firstname LIKE @q OR lastname LIKE @q"
Using cn As New MySqlConnection("Server=localhost;Database=staffdb;Uid=user;Pwd=pass;"),
      cmd As New MySqlCommand(sql, cn)
    cmd.Parameters.AddWithValue("@q", "%" & txtName.Text.Trim() & "%")
    Dim da As New MySqlDataAdapter(cmd)
    Dim dt As New DataTable()
    da.Fill(dt)
    DataGridView1.DataSource = dt
End Using

Populate detail textboxes from the selected grid row:

If DataGridView1.CurrentRow IsNot Nothing Then
    txtFirst.Text = Convert.ToString(DataGridView1.CurrentRow.Cells("firstname").Value)
    txtLast.Text  = Convert.ToString(DataGridView1.CurrentRow.Cells("lastname").Value)
    txtEmail.Text = Convert.ToString(DataGridView1.CurrentRow.Cells("email").Value)
End If

Notes and troubleshooting: parameterize queries to avoid SQL injection; Connector/NET accepts @param names; for very large tables add an index on name columns or use full‑text search; rely on MySQL collation (or use LOWER()) for case‑insensitive matching; if many rows are returned, bind only id/name to the list and fetch full details on selection for performance; ensure the search button’s Click handler is wired to the method and that the MySql.Data assembly is referenced. For projects already using Visual Studio TableAdapters (see ), the designer can generate similar parameterized queries, but the manual approach above gives explicit control when working with MySQL.

HI,
Im wondering if anyone can help me, my vb.net is not the best and i am trying to create a program that allows the user to search for staff.
i have created a database containing one table with details such as name, depatment, email address i have then created a form in vb containing the database headings as label along with a text box. i have also created a 'search' button.

i want the user to be able to type in a name in to the name text box and i want any reaults to display and then the user can select the appropriate one and i want the details to display i have connected the form to the databse but i dont know how to code the the search button to do what i want

So if I understand you, you basically want to create a search (master - detail). I am still learning so bear with me on this, I have a piece of code that I have written for a similar project. This code is only for reference, begin that your variables will be different i.e., the way you connect to the database, but none the less it should get you going in the right direction....A few notes here this page is part of a MDI (Multiple Document Interface) so anywhere you see that remember that when coding yours, if you are not using a MDI you are referring to the forms name. Example of this is Me.MDIPARENT yours should look like Me.(whatever your form name is ). This code makes uses of standard text boxes not the pre-made ones form the drag and drop feature and the datagrid. This is how far I have gotton, I am still trying to learn how to create the detail pages from the datagrid. So like I said this is to only get you started.

Pay attention to the narratives that are mark with ' they contain important notes. Hope it helps

Public Class Employees

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Inventory_management_databaseDataSet.Employees' table. You can move, or remove it, as needed.
        Me.EmployeesTableAdapter.Fill(Me.Inventory_management_databaseDataSet.Employees)

    End Sub

    Private Sub firstname1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles firstname1.TextChanged
        'First Name search field fillter
    End Sub

    Private Sub lastname_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lastname.TextChanged
        'Last Name search field fillter
    End Sub

    Private Sub Title_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Title.SelectedIndexChanged
        'Title search field fillter
    End Sub

    Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
        'Runs the the search query and filler against the database and returns result via DataGrid
        Try
            Me.EmployeesTableAdapter.Search(Me.Inventory_management_databaseDataSet.Employees, firstname1.Text, lastname.Text, Title.SelectedItem, EmployeeID.Text)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Edit.Click

        ' Edit the highlighted Employee...
        Dim strEmployeeID As String = ""

        ' Make sure a row is selected...
        If (DataGridView1.SelectedRows.Count = 0) Then
            MessageBox.Show("There are no Employee Records listed to Edit!", "Problem:", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If

        ' Obtain the Employee ID...
        strEmployeeID = DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value


        Dim objedit_employees As New edit_employees
        objedit_employees.MdiParent = Me.MdiParent
        objedit_employees.EmployeeID = EmployeeID
        objedit_employees.Show()
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.