从组合框设置的数据库中检索数据到文本框
Posted
技术标签:
【中文标题】从组合框设置的数据库中检索数据到文本框【英文标题】:Retrieve data from database set by combobox to textbox 【发布时间】:2013-12-13 06:59:37 【问题描述】:我有一个表单,它有 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 新手
【问题讨论】:
【参考方案1】:在执行时试试这一行
TextBox1.Text=Convert.ToInt32(cmd.ExecuteScalar()).ToString()
【讨论】:
【参考方案2】:我的问题是你是否在那个时候获得了想要的价值
你能把塞子放在下面的方法附近看看返回值吗
Dim query As String = ("SELECT Dress_Price FROM tbl_dress WHERE Dress_Name = ' " & ComboBox1.SelectedValue.ToString & " ' ")
尝试硬编码礼服名称并检查。也可以试试 Select Top 1
【讨论】:
在代码行打断点,查看query的值 或硬编码您的查询,即 Dim query As String = ("SELECT Dress_Price FROM tbl_dress WHERE Dress_Name =SomeDressName") 并查看您是否得到想要的结果 在我的表中.. 我指定 Dress_Name 是 Tuxedo 、 Dress 和 Bikini ,而 Dress_Price 分别是 200 、 150 、 50 .. 但代码没有返回确切的值..跨度> 你可以试试这行 Dim query As String = ("SELECT Dress_Price FROM tbl_dress WHERE Dress_Name =Bikini ") 以及 TextBox1.Text= Convert.ToDouble(cmd.ExecuteScalar()).ToString() 试试这个 Dim 查询 As String = ("SELECT Dress_Price FROM tbl_dress WHERE Dress_Name =[Bikini]")【参考方案3】: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
【讨论】:
以上是关于从组合框设置的数据库中检索数据到文本框的主要内容,如果未能解决你的问题,请参考以下文章
从组合框中选择项目时,从 xml 文件中获取数据到文本框。如何做到这一点?
使用ajax从mysql DB检索和打印数据到文本框(如何在同一个php页面中使用它两次)
从文本框值查看组合框值 Microsoft Access 2007