产品文本框,然后产品目录显示建议我有关产品vb.net代码需要的信息 [英] product text box, then Product catalog appear that Suggest me information about the product vb.net code require

查看:60
本文介绍了产品文本框,然后产品目录显示建议我有关产品vb.net代码需要的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入产品文本框时,产品目录 出现建议我   有关产品的信息。 产品名称,产品库存,产品单价,数量,制造商,数据库中的奖金。  vb.net代码是
强烈要求。数据网格视图是否显示产品目录。 在给定的图片中显示

解决方案

你好,


在这种情况下(因为你没有' t表示你目前是否有一种处理数据的方法)下面只显示了一种方法。


从SQL-Server读取(将数据提供者改为OleDb for ms-访问等)有一种方法只将产品名称放入ListBox,也可以是一个自动完整的文本框或组合框。因此,在这种情况下,可以通过产品标识符填充ListBox控件
。是的我知道你要求TextBox,但最好实现自动完成,请参阅我的MSDN

代码示例
,用于实现自动完成。


注意我没有自动完成文本框,因为我的时间有限(工作)。



列表框类。

公共类产品
公共属性Id()为整数
公共属性名称为String
Public Overrides Function ToString()As String
返回名称
结束函数
结束类




表格代码

公共类Form1 
属性dataOperations() As New DataOperations
Private Sub Form1_Load(sender As Object,e As EventArgs)Handles MyBase.Load
ListBox1.DataSource = dataOperations.Products
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object,e As EventArgs)_
Handles ListBox1.SelectedIndexChanged

如果ListBox1.DataSource Is Nothing Then
退出Sub
结束如果

Dim result = dataOperations。
GetProduct(CType(ListBox1.SelectedItem,Product).Id)。
AsEnumerable()。
FirstOrDefault()

categoryTextBox.Text = result.Field(Of String)(" CategoryName")

End Sub
End Class

此案例中的数据库是Microsoft north wind。


获取数据的代码


< pre class ="prettyprint lang-vb"> Imports System.Data.SqlClient
Public Class DataOperations
Inherits BaseSqlServerConnections
Public Function GetProducts()As DataTable
mHasException = False

Dim dtproducts = New DataTable
Dim selectStatement =" SELECT P.ProductID,P.ProductName," &安培;
" P.CategoryID,C.CategoryName,P.UnitPrice," &安培;
"P.UnitsInStock FROM Products AS P" &安培;
" INNER JOIN Categories AS C ON P.CategoryID = C.CategoryID"

尝试
使用cn作为新的SqlConnection使用{.ConnectionString = ConnectionString}
使用cmd作为新的SqlCommand使用{.Connection = cn}
cmd.CommandText = selectStatement
cn.Open()
dtproducts.Load(cmd.ExecuteReader())
结束使用
结束使用
Catch ex As Exception
mHasException = True
mLastException = ex
结束尝试

返回dtproducts

结束函数
公共函数GetProduct(ByVal productIdentifier As Integer)As DataTable
mHasException = False

Dim dtproducts = New DataTable

Dim selectStatement =" SELECT P.ProductID,P.ProductName," &安培;
" P.CategoryID,C.CategoryName,P.UnitPrice," &安培;
"P.UnitsInStock FROM Products AS P" &安培;
" INNER JOIN Categories AS C ON P.CategoryID = C.CategoryID WHERE P.ProductID = @ Id"

尝试
使用cn作为新的SqlConnection使用{.ConnectionString = ConnectionString}
使用cmd作为新的SqlCommand使用{.Connection = cn}
cmd.CommandText = selectStatement
cmd.Parameters.AddWithValue(" @ Id",productIdentifier)
cn.Open()
dtproducts.Load(cmd.ExecuteReader())
End using
结束使用
Catch ex As Exception
mHasException = True
mLastException = ex
结束尝试

返回dtproducts

结束函数
公共函数Products()As List(Of Product)
mHasException = False
Dim productList As New List(Of Product)
Dim selectStatement =" SELECT ProductID,ProductName FROM产品AS P"
尝试
使用cn作为新的SqlConnection使用{.ConnectionString = ConnectionString}
使用cmd作为新的SqlCommand使用{.Connection = cn}
cmd.CommandText = selectStatement
cn.Open()

Dim reader = cmd.ExecuteReader()
当reader.Read()
productList.Add(New Product()With {.Id = reader。 GetInt32(0),。Name = reader.GetString(1)})
结束时

结束使用
结束使用
Catch ex As Exception
mHasException = True
mLastException = ex
结束尝试

返回productList
结束函数
结束类

连接类

公共类BaseSqlServerConnections 
继承BaseExceptionsHandler

'''< sum玛丽>
'''这指向您的数据库服务器
'''< / summary>
受保护的DatabaseServer As String =" .\SQLEXPRESS" '改变你的服务器
'''< summary>
'''包含所需表格的数据库名称
'''< / summary>
Protected DefaultCatalog As String =" NorthWindAzure2"
Public ReadOnly属性ConnectionString As String
Get
Return


" Data Source = {DatabaseServer}; Initial Catalog = {DefaultCatalog}; Integrated Security =真QUOT;
结束获取
结束物业

结束类

例外类

 Imports System.Data.SqlClient 
Public Class BaseExceptionsHandler
受保护的mHasException As Boolean
Public ReadOnly属性HasException()As Boolean
Get
返回mHasException
结束获取
结束属性
受保护的mLastException异常
Public ReadOnly属性LastException()As Exception
获取
返回mLastException
结束获取
End Property
Public ReadOnly Property SqlException()As SqlException
Get
返回CType(mLastException,SqlException)
End Get
End Property
Public ReadOnly属性HasSqlException()As Boolean
Get
如果LastException IsNot Nothing则
返回TypeOf LastException是SqlException
其他
返回False
结束如果
结束获取
结束物业
公共ReadOnly属性LastExceptionMessage()As String
获取
返回LastException.Message
结束获取
结束属性
公共ReadOnly属性IsSuccessFul()As Boolean
获取
返回Not HasException
结束获取
结束属性
结束类


注意异常和连接类的想法是有一种简单的方法来连接数据库并检查异常和这些类通常放在一个类项目中并在表单项目中引用,否则这可能是
看起来有些过分但不是。









When I type the product text box, then Product catalog  appear that Suggest me   information about the product.  product name , product stock, product unit price, quantity, manufacturer, bonus from the database.  vb.net code is highly oblige. Did data grid view show product catalog.  as show in given picture

解决方案

Hello,

In this case (since you didn't indicate if you currently have a method to work with data) the following shows but one method to do this.

Reading from SQL-Server (change the data provider to say OleDb for ms-access etc) there is a method to get only product names into a ListBox, could also be a auto complete text box or combo box. So moving through, in this case a ListBox controls can be populated via the product identifier. Yes I know you asked for a TextBox but it's best to implement auto-complete, see my MSDN code sample for implementing auto-complete.

Note I didn't do a auto complete text box as my time is limited (at work).

Class for list box.

Public Class Product
    Public Property Id() As Integer
    Public Property Name As String
    Public Overrides Function ToString() As String
        Return Name
    End Function
End Class


Form code

Public Class Form1
    Property dataOperations() As New DataOperations
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.DataSource = dataOperations.Products
    End Sub
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
        Handles ListBox1.SelectedIndexChanged

        If ListBox1.DataSource Is Nothing Then
            Exit Sub
        End If

        Dim result = dataOperations.
                GetProduct(CType(ListBox1.SelectedItem, Product).Id).
                AsEnumerable().
                FirstOrDefault()

        categoryTextBox.Text = result.Field(Of String)("CategoryName")

    End Sub
End Class

Database in this case is Microsoft north wind.

Code to get at data

Imports System.Data.SqlClient
Public Class DataOperations
    Inherits BaseSqlServerConnections
    Public Function GetProducts() As DataTable
        mHasException = False

        Dim dtproducts = New DataTable
        Dim selectStatement = "SELECT P.ProductID, P.ProductName, " &
                              "P.CategoryID, C.CategoryName, P.UnitPrice, " &
                              "P.UnitsInStock FROM Products AS P " &
                              "INNER JOIN  Categories AS C ON P.CategoryID = C.CategoryID"

        Try
            Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                Using cmd As New SqlCommand With {.Connection = cn}
                    cmd.CommandText = selectStatement
                    cn.Open()
                    dtproducts.Load(cmd.ExecuteReader())
                End Using
            End Using
        Catch ex As Exception
            mHasException = True
            mLastException = ex
        End Try

        Return dtproducts

    End Function
    Public Function GetProduct(ByVal productIdentifier As Integer) As DataTable
        mHasException = False

        Dim dtproducts = New DataTable

        Dim selectStatement = "SELECT P.ProductID, P.ProductName, " &
                              "P.CategoryID, C.CategoryName, P.UnitPrice, " &
                              "P.UnitsInStock FROM Products AS P " &
                              "INNER JOIN  Categories AS C ON P.CategoryID = C.CategoryID WHERE P.ProductID = @Id"

        Try
            Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                Using cmd As New SqlCommand With {.Connection = cn}
                    cmd.CommandText = selectStatement
                    cmd.Parameters.AddWithValue("@Id", productIdentifier)
                    cn.Open()
                    dtproducts.Load(cmd.ExecuteReader())
                End Using
            End Using
        Catch ex As Exception
            mHasException = True
            mLastException = ex
        End Try

        Return dtproducts

    End Function
    Public Function Products() As List(Of Product)
        mHasException = False
        Dim productList As New List(Of Product)
        Dim selectStatement = "SELECT  ProductID, ProductName FROM Products AS P"
        Try
            Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                Using cmd As New SqlCommand With {.Connection = cn}
                    cmd.CommandText = selectStatement
                    cn.Open()

                    Dim reader = cmd.ExecuteReader()
                    While reader.Read()
                        productList.Add(New Product() With {.Id = reader.GetInt32(0), .Name = reader.GetString(1)})
                    End While

                End Using
            End Using
        Catch ex As Exception
            mHasException = True
            mLastException = ex
        End Try

        Return productList
    End Function
End Class

Connection class

Public Class BaseSqlServerConnections
    Inherits BaseExceptionsHandler

    ''' <summary>
    ''' This points to your database server
    ''' </summary>
    Protected DatabaseServer As String = ".\SQLEXPRESS" ' CHANGE TO YOUR SERVER
    ''' <summary>
    ''' Name of database containing required tables
    ''' </summary>
    Protected DefaultCatalog As String = "NorthWindAzure2"
    Public ReadOnly Property ConnectionString As String
        Get
            Return


"Data Source={DatabaseServer};Initial Catalog={DefaultCatalog};Integrated Security=True" End Get End Property End Class

Exception class

Imports System.Data.SqlClient
Public Class BaseExceptionsHandler
    Protected mHasException As Boolean
    Public ReadOnly Property HasException() As Boolean
        Get
            Return mHasException
        End Get
    End Property
    Protected mLastException As Exception
    Public ReadOnly Property LastException() As Exception
        Get
            Return mLastException
        End Get
    End Property
    Public ReadOnly Property SqlException() As SqlException
        Get
            Return CType(mLastException, SqlException)
        End Get
    End Property
    Public ReadOnly Property HasSqlException() As Boolean
        Get
            If LastException IsNot Nothing Then
                Return TypeOf LastException Is SqlException
            Else
                Return False
            End If
        End Get
    End Property
    Public ReadOnly Property LastExceptionMessage() As String
        Get
            Return LastException.Message
        End Get
    End Property
    Public ReadOnly Property IsSuccessFul() As Boolean
        Get
            Return Not HasException
        End Get
    End Property
End Class

NOTE The exception and connection classes idea is to have an easy way to connection to a database and check for exceptions and these classes are generally placed into a class project and referenced in your forms project, otherwise this may appear as overkill but it's not.


这篇关于产品文本框,然后产品目录显示建议我有关产品vb.net代码需要的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆