简体   繁体   中英

How to display SELECT query results to a textbox in vb.net

I want to allow the user to query the database for a specific customer_id and to use this to populate textboxes(tbFName, tbLName, tbPhoneNum, etc) with the users relevant information from the table which can then be edited on another button press update the database. Below are a copy of my current code and an image of the form i'm trying to create.

Public Class searchcustomers

    Dim sql As New sqlcontrol

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        If sql.HasConnection = True Then
            sql.RunQuery("SELECT order_id, date_ordered, order_total, collection_method, staff_id FROM orders WHERE customer_id=" & tbSearchID.Text & " ORDER BY date_ordered")
            If sql.sqldataset.Tables.Count > 0 Then
                dgvPOrders.DataSource = sql.sqldataset.Tables(0)

            End If
        End If
        'queries database to search for customer id to then display relevant data in data grid view'

    End Sub

    Private Sub searchcustomers_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnSearchName_Click(sender As Object, e As EventArgs) Handles btnSearchName.Click
        If sql.HasConnection = True Then
            sql.RunQuery("SELECT customers.customer_id, order_id, date_ordered, order_total, collection_method, staff_id FROM (customers INNER JOIN orders on orders.customer_id=customers.customer_id) WHERE customers.first_name=" & tbSearchFName.Text & "")
            If sql.sqldataset.Tables.Count > 0 Then
                dgvPOrders.DataSource = sql.sqldataset.Tables(0)
            End If
        End If
        'queries database to search for customer name to then display relevant data in data grid view'
    End Sub
End Class


FYI I've already managed to get the previous orders section working correctly.

image of form

You could run separate query to retrieve the customer information based on the customer id and use the results to populate the form. Then run the query to get the order information and populate the DataGridView.

isit right or wrong

    Dim qry As String
    qry = "select max(i.comp_id) from inventory i "
    itemcode.Text = qry
    If RS.State > 0 Then RS.Close()
    RS.Open(qry, Form1.dB)
    RS.Close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM