为什么文本框没有加载任何数据? [英] Why textbox is not loading any data?
问题描述
我想在我的 txtlname.textbox 中加载一些数据,这取决于我的 textbox1.text,或者如果我按下按钮,txtlname 将根据我在 textbox1 中输入的内容加载.
I want to load some data in my txtlname.textbox, which depends on my textbox1.text, or if I press the button to the txtlname will load depending on what I input in my textbox1.
Private Sub Button2_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button2.Click
disconnect()
connect()
Try
da = New Odbc.OdbcDataAdapter("Select distinct lname FROM tblemployee WHERE emp_id ='" & Trim(TextBox1.Text.TrimEnd()) & "'", con)
dr = cmd.ExecuteReader
If dr.Read Then
txtlname.Text = dr("lname").ToString
txtlname.Refresh()
End If
Catch ex As Exception
Debug.WriteLine("Plz log in again" & ex.Message)
End Try
End Sub
End Class
推荐答案
首先,我会更改您的查询以使用参数化命令
First of all, I would change your query to use a parameterized command
da = New Odbc.OdbcDataAdapter("Select distinct lname FROM tblemployee WHERE emp_id =?", con)
da.SelectCommand.Parameters.AddWithValue("@p1", TextBox1.Text.Trim())
dr = cmd.ExecuteReader
If dr.Read Then
txtlname.Text = dr("lname").ToString
txtlname.Refresh()
Else
MessageBox.Show("No record found!")
End If
然后,我添加了一条消息,以防 dr.Read
返回 false.(没有符合 where 条件的记录)至少你会知道文本框保持空白是否有一个简单的原因.
Then, I have added a message in case dr.Read
return false. (No record matching the where condition)
At least you will know if there is a simple reason why the textbox remains blank.
最后一点.如果 emp_id
是表 tblEmployee
中的数字字段,则传递给 SelectCommand
参数集合的值应转换为整数,因为原样,它作为字符串传递,这可能是丢失结果的来源.
A final note. If emp_id
is a numeric field in the table tblEmployee
the value passed to the SelectCommand
parameters collection should be converted to an intenger because, as is, it is passed as a string and this could be the source of missing results.
这篇关于为什么文本框没有加载任何数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!