数据未绑定到下拉列表 [英] Data not binding to dropdown list

查看:89
本文介绍了数据未绑定到下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


这篇关于数据未绑定到下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆