2013-12-13 31 views
0

我有一個名爲PriceTesting(使用Microsoft Access 2007)數據庫:包含與列名爲<code>tbl_dress</code>表將數據綁定微軟接取到文本框

Dress_ID, Dress_Name, Dress_Price 

在窗體中,我創建了一個組合框和文本框。

到目前爲止,我已經成功地使用此代碼Dress_Name數據與綁定組合框: -

Private Sub FillCombo() 
     Try 
      Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\DressTest\DressTest\db\PriceTesting.accdb") 
      Dim query As String = ("SELECT Dress_Name FROM tbl_dress") 
      Dim da As New OleDb.OleDbDataAdapter(query, fillcon) 
      Dim ds As New DataSet 
      da.Fill(ds) 
      ComboBox1.ValueMember = "Dress_Name" 
      ComboBox1.DataSource = ds.Tables(0) 
      ComboBox1.SelectedIndex = 0 
     Catch ex As Exception 
      MsgBox("ERROR : " & ex.Message.ToString) 
     End Try 
    End Sub 

和它的作品......每次加載窗體,組合框包含Dress_Name列中的數據。

現在我想的texbox.text = Dress_Price WHERE Dress_Name = combox.selecteditem

任何想法如何做到這一點? 我心目中僅僅是這樣的: -

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged 

query = " SELECT Dress_Price FROM tbl_dress WHERE Dress_Name = ' " & Combobox1.Text & " ' " 
Textbox1.text = query 

End Sub 
+0

另外,這個問題與Visual Studio無關。 –

回答

0

試試這個,並確保在您的查詢在你的想法添加Dress_Price

Private Sub FillCombo() 
    Try 
     Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\DressTest\DressTest\db\PriceTesting.accdb") 
     Dim query As String = ("SELECT Dress_Name, Dress_Price FROM tbl_dress") 
     Dim da As New OleDb.OleDbDataAdapter(query, fillcon) 
     Dim ds As New DataSet 
     da.Fill(ds) 
     ComboBox1.ValueMember = "Dress_Name" 
     ComboBox1.DataSource = ds.Tables(0) 
     ComboBox1.SelectedIndex = 0 

     texbox.DataBindings.Clear() 
     texbox.DataBindings.Add("Text", ds.Tables(0), "Dress_Price") 

    Catch ex As Exception 
     MsgBox("ERROR : " & ex.Message.ToString) 
    End Try 
End Sub 

或者只是改變代碼這樣,一定要在你的查詢添加Dress_Price

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged 
    dim dt as DataTable = Combobox1.DataSource 
    dim dr as DataRow = dt.Rows(Combobox1.SelectedIndex) 
    Textbox1.text = iif(dr.isNull("Dress_Price"), "", dr.isNull("Dress_Price").Tostring) 
End Sub 
+0

它的工作.. thx很多..我將在我的實際項目中實現這個.. :) – WaN

0
Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\DressTest\DressTest\db\PriceTesting.accdb") 
Dim query As String = " SELECT Dress_Price FROM tbl_dress WHERE Dress_Name = ' " & Combobox1.Text & "'" 
Dim da As New OleDb.OleDbDataAdapter(query, fillcon) 
Dim dt As New DataTable 
da.Fill(dt) 
Textbox1.text = dt.rows(0).item(0) 

搜索參數,以避免SQL注入攻擊。也可以嘗試來提高查詢和使用ID而不是dress_name的

+0

這只是一個測試的形式,如果它的作品,我會在我的實際項目執行的代碼...我需要使用Dress_Name用於實際工程.. 「搜索參數,以避免SQL注入」 U可以解釋更多關於這個... 我試過了一個代碼,使用excutescalar這個發現谷歌..但其無效.. – WaN

+0

我得到了一個錯誤:在位置0沒有排。 – WaN