数据未绑定到下拉列表

问题描述:

private void load_uname()
    {
username.Items.Clear();

        SqlConnection con = new SqlConnection(constring);
        con.Open();
        string sql = "select [uname] from user_registration where usergroup ='" + usertype.SelectedValue.ToString() + "' ";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        username.Items.Add("<------Select---->");
        while (dr.Read())
        {

            username.Items.Add(new ListItem(dr[0].ToString()));
        }
    }



这是我的代码,如果我静态地将我的查询设置为select [uname] from user_registration where usergroup =''所有者'',它正在工作,但如果我在代码中动态尝试像上面的查询一样没有获取数据!!


this is my code which is not working if i put my query statically like this "select [uname] from user_registration where usergroup =''owners''", it''s working but if i tried dynamically like above query in the code not fetching the data!!

你在期待SelecetedValue将根据给定的字符串数据获取值的字符串值。

假设您的所有者有2个人ID为100的XYZ和ID为200的ABC



案例1:所选值将在所有者中搜索100或200.



案例2:所选项目将由XYZ或ABC搜索所有者。



由于您正在搜索字符串,但您已指定选定值,因此显示为空白。



尝试案例2,它将起作用
You are expecting a string value wheras SelecetedValue will fetch for the Value against the string data given.
Say your Owners has 2 persons XYZ with ID 100 and ABC with ID 200

CASE 1:Selected value will search in owners by 100 or 200.

CASE 2:Selected Item will search in owners by XYZ OR ABC.

Since your are searching through string but you have specified Selected Value, it is showing blank.

Try case 2, it will work