リストボックスに置いたアイテムのデータベース値をテキストボックスに表示しようとしています。 (vb.net)データ型を変更して条件式のデータ型の不一致を修正するには
私のデータベーステーブル名は 'productlog'です。このテーブルには、3列、productid、productname、priceがあります。私が作成したリストボックスに表示するproductnameを取得しました.3つのテキストボックスに3つの列を表示しようとしています。しかし、私はExecuteReader行の "条件式のデータ型の不一致"エラーを受け取ります。ここに私のコードは次のとおりです。私は、「商品コード」と「価格」は両方とも整数であり、私がやっていることは文字列のためのものですので、それはあるかもしれない、見上げた他の質問に基づいて
Public Class shop
Dim provider As String
Dim datafile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
Private Sub listboxitems_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listboxitems.SelectedIndexChanged
Dim lbconn As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.Oledb.12.0; Data Source = C:\Users\USER PC\Desktop\orderDB1.accdb")
Dim lbcmd As New OleDb.OleDbCommand("SELECT productid, product, price FROM productlog WHERE productid =' & listboxitems.Text & ' AND product ='" & listboxitems.Text & "' AND price =' & listboxitems.Text & '", lbconn)
Dim lbreader As OleDbDataReader
lbconn.Open()
lbreader = lbcmd.ExecuteReader() 'error appearing right here'
While lbreader.Read
txtproductid.Text = lbreader.GetInt32("productid")
txtproduct.Text = lbreader.GetString("product")
txtprice.Text = lbreader.GetInt32("price")
End While
lbconn.Close()
End Sub
。私は二重引用符( '"& txtproductid.Text"')を取り除き、私が調べた別の質問に基づいて 'txtproductid.Text'に変換しようとしました。私が見た別の答えは、文字列を整数に変換することでした - 'lbcmd.Parameters.AddwithValue( "@ productid"、ConvertInt32( "txtproductid.Text"))'正しいのかどうかはわかりませんが、 。このエラーを回避するにはどうすればよいですか?ありがとう。
更新されたコード:あなたがここでやっているようにSQLクエリを作成するために、文字列の連結を使用してはならない
Private Sub listboxitems_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listboxitems.SelectedIndexChanged
Using lbconn As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.Oledb.12.0; Data Source = C:\Users\USER PC\Desktop\orderDB1.accdb")
Using lbcmd As New OleDb.OleDbCommand("SELECT productid, product, price FROM productlog WHERE productid = ? AND product = ? AND price = ?", lbconn)
'Set your values here. The parameters must be added in the same order that they
'appear in the sql SELECT command
Dim prodidparam As New OleDbParameter("@productid", Me.txtproductid.Text)
Dim prodparam As New OleDbParameter("@product", Me.txtproduct.Text)
Dim priceparam As New OleDbParameter("@price", Me.txtprice.Text)
lbcmd.Parameters.Add(prodidparam)
lbcmd.Parameters.Add(prodparam)
lbcmd.Parameters.Add(priceparam)
'Open the connection
lbconn.Open()
Using lbreader As OleDbDataReader = lbcmd.ExecuteReader()
While lbreader.Read
txtproductid.Text = lbreader.GetInt32("productid").ToString()
txtproduct.Text = lbreader.GetString("product")
txtprice.Text = lbreader.GetInt32("price").ToString()
End While
End Using
End Using
End Using
End Sub
10進数または倍数ではないInt32 – Plutonix
@Plutonix私は3つの製品の価格列に10,15,20を入れます。実際には2倍または10進数の場合、どのようにデータ型を変更できますか? – JermaineAIT
@Plutonix私はTry and Catchを試してみましたが、もうエラーはありませんでした。しかし、私はまだ値を表示するテキストボックスを取得することはできません。 – JermaineAIT