【问题标题】:how to display data from database into separate textbox in a new form, when user clicks an item that is in the listbox当用户单击列表框中的项目时,如何将数据库中的数据显示到新表单中的单独文本框中
【发布时间】:2018-06-16 20:27:36
【问题描述】:

我正在制作一个库存管理系统,作为我大学项目的一部分。我有一个搜索栏,结果被输出到一个列表框中。当点击搜索到的产品时,用户将被带到一个新表单,他们将能够在其中查看产品信息,例如产品名称、价格、数量、库存。用户也可以删除和添加库存。我在下面提供了一个屏幕截图。

我是 Visual Studio 的新手,我的问题是我不确定如何在文本框中显示产品信息,请有人帮忙。

这是我目前在 Form4 中的代码,它是包含搜索结果和列表框的主页。

'Importing System.Data.OleDb, this is needed for connection with  database and is needed for database requirements.
Imports System.Data.OleDb

'This is the homepage
Public Class FrmHomePage
        'this code runs when the user searches for a certain product - " the search button event"
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        'The code below is used to open the database connection. 
        Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\jacob\Desktop\MS Office\project.mdb")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM tblProduct WHERE productID LIKE '" & txtSearch_Bar.Text & "'", con)
        con.Open()
        Dim sdr As OleDbDataReader = cmd.ExecuteReader()


        'This if statement makes sure that the product the user is searching for is inside the database, if it isnt then a "no matches" message is displayed.
        If Not sdr.HasRows Then
            MessageBox.Show("No Matches")
            Exit Sub
        End If

        'this clears the listbox so, if the user searches same product again, it wouldn't duplicate.
        lstbSearchResult.Items.Clear()
        'Checking for the item in database and outputting it to the listbox.
        While (sdr.Read())
            lstbSearchResult.Items.Add(sdr("ProductID"))
        End While

        'closing the database connection
        con.Close()

    End Sub

    'this private sub is basically a side menu which slides out when the user clicks the button "menu".
    'Inside the menu the user will have different options such As settings, About us, And more.
    Private Sub btnMenu_Click(sender As System.Object, e As System.EventArgs) Handles btnMenu.Click
        PnlSide_Menu.Location = New Point(-183, 0)
        Do Until PnlSide_Menu.Location.X = -10
        PnlSide_Menu.Location = New Point(PnlSide_Menu.Location.X + 1, 0)
        Loop

        Do Until PnlSide_Menu.Location.X = 0
            PnlSide_Menu.Location = New Point(PnlSide_Menu.Location.X + 1, 0)
            Refresh()
            System.Threading.Thread.Sleep(20)
        Loop

    End Sub

    'private sub for closing the the side menu/side bar.
    Private Sub BtnBack_Click(sender As System.Object, e As System.EventArgs) Handles BtnBack.Click
        PnlSide_Menu.Location = New Point(0, 0)
        Do Until PnlSide_Menu.Location.X = -170
            PnlSide_Menu.Location = New Point(PnlSide_Menu.Location.X - 1, 0)
            Refresh()
        Loop

        Do Until PnlSide_Menu.Location.X = -183
            PnlSide_Menu.Location = New Point(PnlSide_Menu.Location.X - 1, 0)
            System.Threading.Thread.Sleep(20)
        Loop

    End Sub

    'this is the logout button function. 
    'when clicked the user will be taken back to the login page.
    Private Sub btnLogOut_Click(sender As Object, e As EventArgs) Handles btnLogOut.Click
        Me.Close()
        FrmLogin.Show()
    End Sub

    Private Sub lstbSearchResult_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstbSearchResult.SelectedIndexChanged

    End Sub

下面的这段代码是我打算做上面提到的事情的地方。

    'Click event of the result in listbox.
    Private Sub lstbSearchResult_Click(sender As Object, e As EventArgs) Handles lstbSearchResult.Click
        Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\jacob\Desktop\MS Office\project.mdb")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM tblProduct WHERE productID LIKE '" & txtSearch_Bar.Text & "'", con)
        con.Open()
        Dim sdr As OleDbDataReader = cmd.ExecuteReader()

        con.Close()

        frmProductForm.Show()
    End Sub

End Class

【问题讨论】:

  • 您可能想研究数据绑定。您还应该阅读How to Ask 并选择touri am unsure how ... 不是问题,只是表示你没有做足够的研究。这不是一个教程网站
  • 你怎么了?我不明白。每次你是第一个看到我的帖子的人,你甚至帮不上忙,而是批评我的帖子。说真的,如果可以,请提供帮助。我不是专业人士,也不是什么都做对了,我犯了一些错误,我是初学者。所以请停止劝阻。顺便说一句,感谢“数据绑定”@Plutonix
  • 第一个看到我的帖子的人,你甚至帮不上忙,然后顺便说一句,感谢“数据绑定”
  • 网站不是这样运作的。 应该不鼓励发布过于宽泛、偏离主题和/或研究不足的帖子(通过投票)。当然,你可以通过做更多的研究和提出更好的问题来减轻很多这种情况。阅读How to Ask,获取tour,或者访问help center了解详情。除了一篇帖子之外,所有帖子的评分均为负分,很快这将限制您发布任何内容的能力。
  • 理想情况下,库存管理(库存)数据库不会保存汇总数据。输入进出库存交易,然后根据原始数据记录计算净余额。您的项目需要使用 VB.net 吗?

标签: sql vb.net forms ms-access listbox


【解决方案1】:

我将我的解释放入代码中。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        'The code below is used to open the database connection. 
        'Change Dim to Using - this takes care of disposing of objects you create with the End Using
        Using con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\jacob\Desktop\MS Office\project.mdb")
            'You download every field in the table but you only use the productID field
            'the idea with database communication is to get in and out as quickly as possible
            'only ask for the data you need
            'Your user is actually typing in and searching for a productID?
            'A productID in a database is usually an Integer of a Long
            'Check what datatype this is. If this is a number, what does it mean to find a number LIKE another number?
            Using cmd As OleDbCommand = New OleDbCommand("SELECT * FROM tblProduct WHERE productID LIKE '" & txtSearch_Bar.Text & "'", con)
                con.Open()
                Using sdr As OleDbDataReader = cmd.ExecuteReader()
                    'This if statement makes sure that the product the user is searching for is inside the database, if it isnt then a "no matches" message is displayed.
                    If Not sdr.HasRows Then
                        MessageBox.Show("No Matches")
                        Exit Sub
                    End If

                    'this clears the listbox so, if the user searches same product again, it wouldn't duplicate.
                    lstbSearchResult.Items.Clear()
                    'Checking for the item in database and outputting it to the listbox.
                    While (sdr.Read())
                        'Use a class to hold all your Product data
                        Dim p As New Product
                        p.ProductID = sdr.GetInt32(0) 'I am just guessing at the order of your fields
                        p.ProductName = sdr.GetString(1)
                        p.Price = sdr.GetDouble(2)
                        p.Section = sdr.GetInt32(3)
                        p.Supplier = sdr.GetString(4)
                        lstbSearchResult.Items.Add(p)
                    End While
                End Using
            End Using
            'closing the database connection
            con.Close()
        End Using
    End Sub
    'Click event of the result in listbox.
    Private Sub lstbSearchResult_Click(sender As Object, e As EventArgs) Handles lstbSearchResult.Click
        'Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\jacob\Desktop\MS Office\project.mdb")
        'Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM tblProduct WHERE productID LIKE '" & txtSearch_Bar.Text & "'", con)
        'con.Open()
        'Dim sdr As OleDbDataReader = cmd.ExecuteReader()

        'con.Close()
        'You just got all this info in Button1.Click
        'and it is all saved in your list box
        frmProductForm.Show()
        Dim p As Product = DirectCast(lstbSearchResult.SelectedItem, Product)
        frmProductForm.txtProductName.Text = p.ProductName 'The names of the text boxes are guesses
        frmProductForm.txtProductID.Text = p.ProductID
        'etc.
    End Sub

End Class
Public Class Product

    Public Sub New()
        'Default constructor
    End Sub

    Public Sub New(intProductID As Integer, strProductName As String, dblPrice As Double, intCurrentStock As Integer, intSection As Integer, strSupplier As String)
        ProductID = intProductID
        ProductName = strProductName
        Price = dblPrice
        CurrentStock = intCurrentStock
        Section = intSection
        Supplier = strSupplier
    End Sub

    Private _ProductID As Integer
    Public Property ProductID As Integer
        Get
            Return _ProductID
        End Get
        Set(value As Integer)
            _ProductID = value
        End Set
    End Property

    Private _ProductName As String
    Public Property ProductName As String
        Get
            Return _ProductName
        End Get
        Set(value As String)
            _ProductName = value
        End Set
    End Property

    Private _Price As Double
    Public Property Price As Double
        Get
            Return _Price
        End Get
        Set(value As Double)
            _Price = value
        End Set
    End Property

    Private _CurrentStock As Integer
    Public Property CurrentStock As Integer
        Get
            Return _CurrentStock
        End Get
        Set(value As Integer)
            _CurrentStock = value
        End Set
    End Property

    Private _Section As Integer
    Public Property Section As Integer
        Get
            Return _Section
        End Get
        Set(value As Integer)
            _Section = value
        End Set
    End Property

    Private _Supplier As String
    Public Property Supplier As String
        Get
            Return _Supplier
        End Get
        Set(value As String)
            _Supplier = value
        End Set
    End Property
    Public Overrides Function ToString() As String 'This is what the list box calls to get what to put in its text
        Return $"{_ProductID}  {_ProductName}"
    End Function

End Class

【讨论】:

    猜你喜欢
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多