且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将文本框绑定到另一个文本框

更新时间:2023-02-11 19:12:45

查询将如下所示:

the query will be like this:

cmd = sqlcommand(select EMPNAME from table_Name where ID = "  + TextBox1.Text + "   ,con)



然后
将文本框的自动回传设置为true

代码:



Then
turn autopostback of the textbox to true

Code:

protected void TextBox1_TextChanged(object sender, System.EventArgs e)
{
    txtEMPNAME.Text = <call your="" function="" where="" you="" have="" written="" the="" query=""> ;
}</call>


string ConStr = @"server="serverName";database=dbName;uid=username;pwd=password";
            DataSet ds = new DataSet();
            SqlConnection Con = new SqlConnection(ConStr);


            Con.Open();

            SqlCommand cmd = new SqlCommand("Select EMPNAME from TableName Where id='"+txtid.text+"'", Con);
            txtEMPNAME.text = Convert.ToString(cmd.ExecuteScalar());
            
               
                Con.Close();


要添加到先前的答案,从不直接将值连接到SQL语句.这将使您容易受到SQL注入,数据类型转换不匹配等的影响.

在SQL语句中使用用户输入的正确方法是使用 SqlParameter [^ ]

因此您的代码可能如下所示:
To add to previous answers, never concatenate values directly to your SQL statements. This will leave you vulnerable to SQL injections, data type conversion mismatches and so on.

The proper way to use user input in a SQL statement is to use a SqlParameter[^]

So your code could look like:
...
SqlCommand command = new SqlCommand("SELECT EmpName FROM YourTable WHERE Id = @Id", Con);
command.Parameters.AddWithValue("@Id", TheTextBox.Text);
...