每当列表框选择的索引发生更改时,都会将数据库值检索到文本框

时间:2016-02-25 10:36:57

标签: c# asp.net listbox

所以我只想在列表框的选定索引更改时查看我的数据库中的值到项目中的某些文本框

我有这些代码,但每当我点击列表框中的某个项目时,没有任何文本框显示数据或任何内容

                protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringHRMS"].ConnectionString);
                conn.Open();
                string selectone = "SELECT * FROM Applicant WHERE ApplicationNo = '" + ListBox1.SelectedValue.ToString() + "'";
                SqlCommand cmd = new SqlCommand(selectone, conn);

                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    asln.Text = reader["LastName"].ToString();
                    asfn.Text = reader["FirstName"].ToString();
                    asmn.Text = reader["MiddleName"].ToString();
                    ashea.Text = reader["HEA"].ToString();
                    astitle.Text = reader["Title"].ToString();
                    aspos.Text = reader["Position"].ToString();
                    asaddress.Text = reader["Address"].ToString();
                    asbday.Text = reader["Birthday"].ToString();
                    asgender.Text = reader["Gender"].ToString();
                    asage.Text = reader["Age"].ToString();
                    asht.Text = reader["Height"].ToString();
                    aswt.Text = reader["Weight"].ToString();
                    asemail.Text = reader["Email"].ToString();
                    ascontact.Text = reader["ContactNumber"].ToString();


                }

                reader.Close();
                conn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }
    }

0 个答案:

没有答案