如何基于从组合框中选择值来自动填充文本框 [英] how to autofill textboxes based on selecting a value from combobox

查看:97
本文介绍了如何基于从组合框中选择值来自动填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格中有一个组合框和一个文本框,我正在尝试,如果我从组合框中选择一个值,然后根据该选择,查询应该触发并在文本框中填充结果

i尝试了以下代码,但它不起作用..

 私有  Sub  ComboName_SelectedIndexChanged( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs) Handles  ComboName.SelectedIndexChanged 
尝试
conn.Open()
Dim query As String = 选择Gr_No FROM Student_Mast呃WHERE Class ='& ComboClass.Text& 'and Division ='& ComboDiv.Text& '和Session ='& ComboSession.Text& '和Name ='& ComboName.Text& '
cmd = SqlCommand(query,conn)
Dim rd As SqlDataReader = cmd.ExecuteReader(CommandBehavior .CloseConnection)
while rd.Read()
TxtGrNo.Text(rd( Gr_No)。ToString)
结束 while
rd.Close()
conn.Close()



Catch ex As 例外

结束 尝试
结束 Sub

解决方案

问题在线



TxtGrNo.Text(rd(Gr_No)。ToString)



应该是



TxtGrNo.Text = rd(Gr_No)。ToString()


as ryanb31声明你的错误是因为你使用TxtGrNo.Text()而不是TxtGrNo.Text,但这并不是你的代码所有的错误。记住,草率的代码更难以阅读和维护。花一点时间预先编写干净的代码可以节省很多麻烦。



 Private Sub ComboName_SelectedIndexChanged(sender as Object,e As EventArgs )
尝试
''使用using语句来关闭和处理你的对象
使用conn As New SqlConnection()
conn.Open()
''使用字符串formater而不是一堆串联。
Dim query As String = String.Format(选择Gr_No FROM Student_Master WHERE Class =''{0}''和Division =''{1}''和Session =''{2}''和名称=''{3},ComboClass.Text,ComboDiv.Text,ComboSession.Text,ComboName.Text)
使用cmd作为新的SqlCommand(query,conn)
使用rd作为SqlDataReader = cmd.ExecuteReader (CommandBehavior.CloseConnection)
而rd.Read()
''Text是属性而不是Method。
TxtGrNo.Text = rd(Gr_No)。ToString()
结束时
结束使用
结束使用
结束使用
Catch ex As Exception
''永远不要吃错误记录下来
Log.Write(ex.ToString())
''或显示它。
MessageBox.Show(ex.ToString())
结束尝试
结束子


i have a combobox and one textbox in my form and i am trying that, if i select a value from combobox and then according to that selection a query should fire and populate a result in the textbox
i have tried the following code but its not working ..

Private Sub ComboName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboName.SelectedIndexChanged
       Try
           conn.Open()
           Dim query As String = "Select Gr_No FROM Student_Master WHERE Class='" & ComboClass.Text & "' and Division='" & ComboDiv.Text & "' and Session='" & ComboSession.Text & "' and Name='" & ComboName.Text & "'"
           cmd = New SqlCommand(query, conn)
           Dim rd As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
           While rd.Read()
               TxtGrNo.Text(rd("Gr_No").ToString)
           End While
           rd.Close()
           conn.Close()



       Catch ex As Exception

       End Try
   End Sub

解决方案

Problem is in line

TxtGrNo.Text(rd("Gr_No").ToString)

it should be

TxtGrNo.Text = rd("Gr_No").ToString()


As ryanb31 stated your error is because your using TxtGrNo.Text() instead of TxtGrNo.Text, but that''s not all that''s wrong with your code. Remember sloppy code is much harder to read and maintain. Taking a little time upfront to write clean code saves a lot of headache later on.

Private Sub ComboName_SelectedIndexChanged(sender As Object, e As EventArgs)
	Try
		'' Use using statements to close and dispose of your objects
		Using conn As New SqlConnection()
			conn.Open()
			'' Use string formater instead of a bunch of concatenation.
			Dim query As String = String.Format("Select Gr_No FROM Student_Master WHERE Class=''{0}'' and Division=''{1}'' and Session=''{2}'' and Name=''{3}", ComboClass.Text, ComboDiv.Text, ComboSession.Text, ComboName.Text)
			Using cmd As New SqlCommand(query, conn)
				Using rd As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
					While rd.Read()
						'' Text is property not as Method.
						TxtGrNo.Text = rd("Gr_No").ToString()
					End While
				End Using
			End Using
		End Using
	Catch ex As Exception
		'' Never eat an error Log it
		Log.Write(ex.ToString())
		'' or display it.
		MessageBox.Show(ex.ToString())
	End Try
End Sub


这篇关于如何基于从组合框中选择值来自动填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆