Textbxo事件更改代码不起作用 [英] Textbxo event changed code is not working
问题描述
我的代码如下
My code as follows
protected void textbox1_TextChanged(object sender, EventArgs e)
{
SQl = "select stud_name from student where stud_active <> 'D' and stud_id = '" + txt_studid.Text.ToString().Trim() + "'";
Dr = SCon.ReadSql(SQl);
if (Dr.Read())
{
lblstud_name.Text = Dr[0].ToString();
}
else
{
lblstud_name.Text = "Invalid student id";
lblerr.Text = "invalid studentid";
}
Dr.Close();
}
学生ID textbox1
标签
当我在textbox1中键入学生ID时,我想显示数据库中的studname。
为此我写了以上内容代码。
但是当我输入学生ID时,标签名称不会显示在标签上。
来自我上面的错误是什么我做了。
请帮帮我。
Student id textbox1
Label
When i type the student id in textbox1 i want to display the studname from the database.
for that i written the above code.
But when i type the student id, the studname is not displayed in the label.
from my above what is the mistake i made.
please help me.
推荐答案
你注意到的问题可能是数据related:检查数据库表中的实际stud_id值,然后查看stud_active列。由于你指定了AND,它在返回任何行之前要求两个条件都为真。
但是......为什么要将Text属性转换为字符串,当它已经是?
The problem you have noticed is likely to be data related: check your database table for the actual stud_id value and look at the stud_active column. Since you are specifying AND it requires both conditions to be true before it returns any rows.
But... why are you converting a Text property to a string, when it is already?
txt_studid.Text.ToString().Trim()
将字符串转换为字符串是......嗯。 ..毫无意义......
请为此,请不要那样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
Converting a string to a string is...um...rather pointless...
And please, for your own sake, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
这篇关于Textbxo事件更改代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!