我再次无法得到我的查询的答案。我编写了代码来从数据库中检索数据并通过选择下拉列表将其加载到文本框中。如果我在下拉列表中选择了我的数据,它不会给出准确的答案,而是显示选定的索引 0。我的意思是它会显示“-Select-”。文本框中没有做任何事情。我检查了我的 asp 页面上的 OnSelectedIndex 和 AutoPostBack=True。请纠正我..这是我的代码:
//This is for retrieval of data to dropdown list.
public void engi()
{
DropDownList1.Items.Clear();
ListItem l = new ListItem();
l.Text = "-Select-";
DropDownList1.Items.Add(l);
DropDownList1.SelectedIndex = 0;
Conhr.Open();
SqlCommand cmd = new SqlCommand("select EmployeeName from tbl_EmploeeDetails where Designation like('%Engineer') ", Conhr);
//SqlCommand cmd=new SqlCommand("select Sitecode from MRsite where Sitealiasname in (select Componetcode from tbl_Component where Sitecode='" + TextBox1.Text.Trim() + "'");
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
ListItem n = new ListItem();
n.Text = dr["EmployeeName"].ToString().Trim();
DropDownList1.Items.Add(n);
}
dr.Close();
Conhr.Close();
}
//This is for retrieval data for text box by selecting DropDown List
public void des()
{
Conhr.Open();
string s3;
s3 = "select Designation from tbl_EmploeeDetails where EmployeeName='" + DropDownList1.SelectedItem.Text + "'";
SqlCommand c3 = new SqlCommand(s3, Conhr);
SqlDataReader d3;
d3 = c3.ExecuteReader();
while (d3.Read())
{
TextBox1.Text = d3["Designation"].ToString().Trim();
}
d3.Close();
Conhr.Close();
}
//Called the method for data retrieval for Textbox
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
des();
}