登录界面,控件已和数据库绑定,我想在dropdownlist里选择编号,在下面的textbox中自动显示用户名 ,然后再对用户输入密码进行验证,dropdownlist里的数据能正常显示,但textbox里不能显示用户名,代码如下:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{ string strconn = "Data Source=COMPUTER;Initial Catalog=program;Integrated Security=True";
string strxm = "select xm from employee where (id like '" + DropDownList1.SelectedValue + "')";
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = strconn;
SqlCommand myCommand = new SqlCommand(strxm, myConnection);
myConnection.Open(); /* myCommand.Connection.Open();那种对*/
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
TextBox1.Text = myReader["xm"].ToString();
/*TextBox1.Text = myReader.GetString(0);行不行*/
myConnection.Close(); }帮忙改正,谢谢!
{ string strconn = "Data Source=COMPUTER;Initial Catalog=program;Integrated Security=True";
string strxm = "select xm from employee where (id like '" + DropDownList1.Text + "')";//.SelectedValue
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = strconn;
SqlCommand myCommand = new SqlCommand(strxm, myConnection);
myConnection.Open(); /* myCommand.Connection.Open();那种对*/
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
while(myReader.Read())
{
TextBox1.Text = myReader["xm"].ToString();
}
/*TextBox1.Text = myReader.GetString(0);行不行*/
myConnection.Close(); }----------------------------------------------------------------------------
试试上面的代码。你还可以设置断点,把 strxm 拷贝到 查询分析器中执行,看能不能取到xm
myConnection.Open(); //**************反了吧
//****************
myConnection.Open();
SqlCommand myCommand = new SqlCommand(strxm, myConnection);
myReader.Read();
TextBox1.Text = myReader["xm"].ToString();
/*TextBox1.Text = myReader.GetString(0);行不行*/
若数据库表中的字段类型是字符串 类型是可以的
myReader.Close();
myConnection.Close();