我希望能够在我的数据库中搜索产品名称或产品ID号,然后将有关产品的任何相关信息加载到文本框中。
我当前的代码没有构建错误,但它似乎没有返回任何信息。它可能只是一些小而且我是一个白痴,但我无法理解这个问题。
Private Sub btnSearchProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchProducts.Click
ds.Clear()
If txtProductName.Text = "" And txtProductID.Text = "" Then
MessageBox.Show("Please enter either a Product ID or Product Name..", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
If txtProductID.Text <> "" Then
For Each ch As Char In txtProductID.Text
If Not Char.IsDigit(ch) Then
txtProductID.Clear()
MsgBox("Please enter a valid Product ID. Only integers are a valid input", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Next
End If
' Connect to DB
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Computing Project\Database1.accdb"
Dim Command As New OleDb.OleDbCommand
Try
Dim sql As String = "SELECT * FROM tbl_stock WHERE S_ID = '" & txtProductID.Text & "'OR S_ProductName = '" & txtProductName.Text & "'"
Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)
'Open Database Connection
sqlCom.Connection = conn
conn.Open()
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Stock")
Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()
If sqlRead.Read() Then
txtProductID.Text = CInt(ds.Tables("Stock").Rows(0).Item(0))
txtProductName.Text = ds.Tables("Stock").Rows(0).Item(3)
txtProductPrice.Text = "£" & CDec(ds.Tables("Stock").Rows(0).Item(2))
Else
If txtProductID.Text <> "" Then
MsgBox("Sorry, " & txtProductID.Text & " is not a valid Product ID", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtProductID.Text = ""
End If
If txtProductName.Text <> "" Then
MsgBox("Sorry, " & txtProductName.Text & " is not a valid Product Name", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtProductName.Text = ""
End If
End If
Catch ex As Exception
End Try
End If
End Sub
非常感谢任何帮助
答案 0 :(得分:0)
关于这一点的一些想法,可能会指出你正确的方向。