我试图将数据加载到文本框中,在选择下拉列表时,我创建了一个用于检索的类,并在下拉列表中调用它选择的索引已更改。但我无法得到我想要的答案。如果我在按钮单击事件中调用了该类,它就可以正常工作。所以请纠正我。我犯了什么错误。这是我的代码:
public void so()
{
con.Open();
string s2;
s2 = "select Source from tbl_component where Componetcode='" + Mcodeddl.SelectedItem.Text + "'";
SqlCommand c2 = new SqlCommand(s2, con);
SqlDataReader d2;
d2 = c2.ExecuteReader();
while (d2.Read())
{
TextBox1.Text = d2["Source"].ToString().Trim();
}
d2.Close();
con.Close();
}
//i have called the so class here
protected void Mcodeddl_SelectedIndexChanged(object sender, EventArgs e)
{
so();
}