选择组合框时填充文本框
Posted
技术标签:
【中文标题】选择组合框时填充文本框【英文标题】:Populate text boxes when Combo Box selection made 【发布时间】:2013-10-21 05:20:40 【问题描述】:我正在使用 Visual Basic 在 Visual Studio 2013 中开发一个应用程序,我有点犹豫,希望你能帮助我。好吧,我让我的用户使用一个 Combo Box,它一定会访问中的表格可以很好地提取数据,用于“客户名称”,但我想做的是编写代码,因此当在下面的组合框选择中选择“客户名称”时,一些文本框会自动填充“客户”地址''客户电话号码''客户帐户名称'我有一个工作代码,但不幸的是它只适用于 Int32 我希望它使用文本来识别。谁能帮忙?我将跟进我之前的代码作为帮助
谢谢詹姆斯
Private Sub cmdGetByIdentifier_Click(sender As Object, e As EventArgs) Handles cmdGetByIdentifier.Click
If Not String.IsNullOrWhiteSpace(txtIdentifier1.Text) Then
Dim Identifier As Int32 = 0
If Int32.TryParse(txtIdentifier1.Text, Identifier) Then
txtCompanyName1.Text = GetCustomerNameByIdentifier(Identifier)
Else
MessageBox.Show("'" & txtIdentifier1.Text & "' is not a valid integer.")
End If
Else
MessageBox.Show("Must enter an identifier to get a company name.")
End If
End Sub
Private Sub cmdGetCustomer_Click(sender As Object, e As EventArgs) Handles cmdGetCustomer.Click
If Not String.IsNullOrWhiteSpace(txtIdentifier2.Text) Then
Dim Identifier As Int32 = 0
If Int32.TryParse(txtIdentifier2.Text, Identifier) Then
Dim Cust As Customer = GetCustomer(Identifier)
txtCompanyName2.Text = Cust.Name
txtContactName2.Text = Cust.ContactName
Else
MessageBox.Show("'" & txtIdentifier1.Text & "' is not a valid integer.")
End If
Else
MessageBox.Show("Must enter an identifier to get a company name.")
End If
End Sub
End Class
Module DatabaseOperations
Public Function GetCustomerNameByIdentifier(ByVal Identifier As Int32) As String
Dim CompanyName As String = ""
Dim Builder As New OleDb.OleDbConnectionStringBuilder With .Provider = "Microsoft.ACE.OLEDB.12.0", .DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")
Using cn As New OleDb.OleDbConnection With .ConnectionString = Builder.ConnectionString
Using cmd As New OleDb.OleDbCommand With .Connection = cn
cmd.CommandText = "SELECT CompanyName FROM Customer WHERE Identifier =@P1"
Dim NameParameter As New OleDb.OleDbParameter With .DbType = DbType.Int32, .ParameterName = "P1", .Value = Identifier
cmd.Parameters.Add(NameParameter)
cn.Open()
CompanyName = CStr(cmd.ExecuteScalar)
End Using
End Using
Return CompanyName
End Function
Public Function GetCustomer(ByVal Identifier As Int32) As Customer
Dim Customer As New Customer
Dim CompanyName As String = ""
Dim Builder As New OleDb.OleDbConnectionStringBuilder With .Provider = "Microsoft.ACE.OLEDB.12.0", .DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")
Using cn As New OleDb.OleDbConnection With .ConnectionString = Builder.ConnectionString
Using cmd As New OleDb.OleDbCommand With .Connection = cn
cmd.CommandText = "SELECT Identifier, CompanyName,ContactName FROM Customer WHERE Identifier =@P1"
Dim NameParameter As New OleDb.OleDbParameter With .DbType = DbType.Int32, .ParameterName = "P1", .Value = Identifier
cmd.Parameters.Add(NameParameter)
cn.Open()
Dim Reader As OleDb.OleDbDataReader = cmd.ExecuteReader
If Reader.HasRows Then
Reader.Read()
Customer.Identifier = Identifier
Customer.Name = Reader.GetString(1)
Customer.ContactName = Reader.GetString(2)
End If
End Using
End Using
Return Customer
End Function
End Module
Public Class Customer
Public Property Identifier As Int32
Public Property Name As String
Public Property ContactName As String
Public Sub New()
End Sub
End Class
【问题讨论】:
有人对这个问题有什么想法吗? 创建另一个GetCustomerNameByIdentifier
,它接受一个字符串并传递该字符串。然后将您的 SQL 更改为您要查询的任何字段。
我不确定我理解你的意思@Steve,你介意解释一下吗
【参考方案1】:
像这样:
Public Function GetCustomerNameByName(ByVal Name As String) As String
Dim CompanyName As String = ""
Dim Builder As New OleDb.OleDbConnectionStringBuilder With .Provider = "Microsoft.ACE.OLEDB.12.0", .DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")
Using cn As New OleDb.OleDbConnection With .ConnectionString = Builder.ConnectionString
Using cmd As New OleDb.OleDbCommand With .Connection = cn
cmd.CommandText = "SELECT CompanyName FROM Customer WHERE CustName =@P1"
Dim NameParameter As New OleDb.OleDbParameter With .DbType = DbType.String, .ParameterName = "P1", .Value = Name
cmd.Parameters.Add(NameParameter)
cn.Open()
CompanyName = CStr(cmd.ExecuteScalar)
End Using
End Using
Return CompanyName
End Function
【讨论】:
它仍然有验证码,但数据操作码@p1 中的 cmd.CommandText="SELECT CompanyName FROM Customer WHERE CustName=@P1" is if Int32?我不确定我是否误会自己 对不起,我错过了将参数类型更改为字符串。 嗨@Steve,我刚刚运行了该代码,它似乎加载然后得到一个未处理的OleDbException,并给出以下内容: 'System.Data.OleDb.OleDbException' 类型的未处理异常发生在System.Data.dll 附加信息:没有为一个或多个必需参数提供值。 - 提示它看不到表或数据库 尝试将此行Dim NameParameter As New OleDb.OleDbParameter With .DbType = DbType.String, .ParameterName = "P1", .Value = Name
更改为此行 Dim NameParameter As New OleDb.OleDbParameter With .DbType = DbType.String, .ParameterName = "@P1", .Value = Name
。注意@P1 参数名称。
我刚刚看到了其他内容,您将该行中的值设置为 NAME,这将是您所在的表单/类的名称。如果您将其更改为 NameParameter
,您可以删除它下面的线。这不会真的给你带来麻烦,但如果时间的话,那只是浪费。以上是关于选择组合框时填充文本框的主要内容,如果未能解决你的问题,请参考以下文章