我是.net 的新手。我有以下代码,我知道它不正确:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection("Connection");
SqlCommand cmd = new SqlCommand("SELECT * FROM Users ORDER BY FirstName", conn);
conn.Open();
SqlDataReader rd = cmd.ExecuteReader();
ddlUsers.DataValueField = "ID";
ddlUsers.DataTextField = "FirstName";
ddlUsers.DataSource = rd;
ddlUsers.DataBind();
rd.Close();
SqlCommand cmd1 = new SqlCommand("SELECT [LastName], [Email], [Phone] FROM [Users] WHERE ([FirstName] = @FirstName)", conn);
cmd1.Parameters.AddWithValue("@FirstName", ddlUsers.SelectedItem.ToString());
SqlDataReader rd1 = cmd1.ExecuteReader();
while (rd1.Read())
{
lblPhoneShow.Text = rd1["Phone"].ToString();
lblEmailShow.Text = rd1["Email"].ToString();
lblLNShow.Text = rd1["LastName"].ToString();
}
conn.Close();
}
}
将数据绑定到下拉列表效果很好,但是当我选择某个名称时,标签什么也不显示。谁能解释我做错了什么?谢谢!