从组合框设置的数据库中检索数据到文本框

时间:2013-12-13 06:59:37

标签: vb.net combobox textbox ms-access-2007

我有一个表单,它有1个组合框和1个文本框。

和一个名为tbl_dress的表,其中列为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

这是我的表单加载时的代码: -

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\TMS Final\TMS Final\db\db_TMS.accdb"
            con.Open()

            FillCombo() ' Display data from tbl_order on form load
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

所以问题是,我如何得到Dress_Price,由在组合框中选择的Dress_Name决定。

我尝试过以下代码,但是我有错误。

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged 
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") 

    fillcon.Open() 

    Dim query As String = ("SELECT Dress_Price FROM tbl_dress WHERE Dress_Name = ' " & ComboBox1.SelectedValue.ToString & " ' ") 
    Dim cmd As New OleDb.OleDbCommand(query, fillcon) 
    cmd.CommandText = query 

    TextBox1.Text = cmd.ExecuteScalar().ToString() 

    fillcon.Close() 

    Catch ex As Exception 
        MessageBox.Show(ex.ToString) 
    End Try 

End Sub

所以我哪里出错了,我该怎么办? vb.net的新手

3 个答案:

答案 0 :(得分:1)

执行时尝试此行

TextBox1.Text = Convert.ToInt32(cmd.ExecuteScalar())。ToString()

答案 1 :(得分:0)

我的问题是你是否在那时获得了理想的价值

你能把塞子放在下面的方法附近并看到返回值

Dim query As String =(“SELECT Dress_Price FROM tbl_dress WHERE Dress_Name ='”& ComboBox1.SelectedValue.ToString&“'”)

尝试对服装名称进行硬编码并检查。也可以尝试选择Top 1

答案 2 :(得分:0)

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged 
    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") 

        fillcon.Open() 

        Dim query As String = ("SELECT Dress_Price FROM tbl_dress WHERE Dress_Name = ' " & ComboBox1.Text & " ' ") 
        Dim cmd As New OleDb.OleDbCommand(query, fillcon) 
        cmd.CommandText = query 

        TextBox1.Text = cmd.ExecuteScalar().ToString() 

        fillcon.Close() 

    Catch ex As Exception 
        MessageBox.Show(ex.ToString) 
    End Try 

End Sub