将数据库中文本的值设置到另一个文本框中 [英] Set value of text from database into another textbox
本文介绍了将数据库中文本的值设置到另一个文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我有一个WebApp,其中包含2个文本框(txtName和txtID). txtName是通过queryString填充的,即
Hi , i have a WebApp which contains 2 textboxes (txtName and txtID). txtName is populated from a queryString i.e.
String Brkc = Request.QueryString["Name"];
txtName.Text = Name;
我有一个存储过程,可从数据库中查找文本框中填充的任何名称的ID.
问题是,我想用txtName中文本的ID填充txtID文本框.
I have a stored procedure to find the ID from the database for whichever name is populated in the textbox.
The problem is , i want to populate the txtID textbox with the ID from the text that is in txtName.
thanks.
推荐答案
0.打开连接.
1.使用查询"select id from table where name = ''" + txtName.Text +"''"
或您的过程创建命令.
2.使用String id = cmd.ExecuteScalar();
3.在任何需要的地方使用idtxtID.Text = id;
0. Open connection.
1. Create command with query"select id from table where name = ''" + txtName.Text +"''"
or your procedure.
2. UseString id = cmd.ExecuteScalar();
3. Use id wherever you wanttxtID.Text = id;
您在此处使用了多少个名称?您可以在Cache.cs文件中创建一个静态Dictionary.只需在创建另一个用户时更新字典,这将减轻数据库的负担,因此您的Cache.cs将具有以下内容:
How many names are you using here? You could create a static Dictionary in a Cache.cs file. Just update the dictionary when another user is created, this will take a load off the DB, so your Cache.cs would have the following:
private Dictionary<string,> _users = null;
public Dictionary<string,> Users
{
get
{
if (_users == null)
MethodThatPopulatesUsers();
return _users;
}
}
public void ClearUserCache()
{
_users = null;
}
private void MethodThatPopulatesUsers()
{
//Make the call to the DB and populate the dictionary with the username as the Key and the ID as the value.
}
然后,在您的文本框所在的位置上,在OnTextChanged事件上只有:
Then, where your textbox is, on the OnTextChanged event just have:
if (Cache.Users.ContainsKey(txtTextbox1.Text))
txtTextbox2.Text = Cache.Users[txtTextbox1.Text];
这篇关于将数据库中文本的值设置到另一个文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文