问个关于.NET编程的问题

来源:百度知道 编辑:UC知道 时间:2024/05/21 11:27:41
我想让一个文本框的值等于某个数据库表中的一个对象中保存的数据,请问应该怎样写代码??
比如说在Teacher表中,TeacherID=2的情况下,如果想让TextBox1.Text等于TeacherName中保存的数据,应该怎样写啊
再补充一下,我在文本框前还有一个DropDownList控件,我的想法是当DropDownList1.SelectIndex=1时,TextBox里的值为TeacherName中的数据,当等于2时,那TextBox的值就会为另一个字段里的数据,请问一楼这个代码能不能实现这个功能??还有就是这段代码好像不能实现在TextBox中显示出数据的功能吧,我以前这样写从来没有显示出来过

SqlConnection myconn = new SqlConnection(); //数据库连接
myconn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["sqlserver"];
//sqlserver为Web.Config里的连接数据库字符串
myconn.Open();
SqlCommand cmd = myconn.CreateCommand();
SqlDataReader rec = null;
cmd.CommandText = "select TeacherID, TeacherName from Teacher"; //查询语句
rec = cmd.ExecuteReader();
this.DropDownList1.Items.Clear();
while (rec.Read())
{
this.DropDownList1.Items.Add(new ListItem(Convert.ToString(rec["TeacherID"]), Convert.ToString(rec["TeacherName"])));//添加下拉表
}
rec.Close();
cmd.Dispose();
myconn.Close();

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.TextBox1.Text = this.DropDownList1.SelectedValue.ToString();
}
都这样了,如果不追加分的话你也太吝啬了昂.
不明白,来这里:
QQ群:40576854 梦幻 ASP.NET
1、积极发言 2、讲话文明 3、不乱聊天 4、助人为乐