c# code in asp.net for displaying database values in dropdown list

核能气质少年 提交于 2019-12-24 17:00:12

问题


hello everybody im trying to display the database values in my dropdown list, i have created stored procedure for it and trying to get the values by calling that stored procedure in my code behind but im not able to display it. please help me out

   protected void DrpClientName_SelectedIndexChanged(object sender, EventArgs e)   
   {

        MTMSDTO objc = new MTMSDTO();
        {
            objc.ClientName = Convert.ToString(Session["ClientName"]);
            DataSet ClientN = obj.GetClientList();
            DataView Projview = new DataView();
            Projview.Table = ClientN.Tables[0];
            DrpClientName.DataSource = Projview;
            DrpClientName.DataBind();
        }
    } 

回答1:


Dont write the code in dropdowns selected index changed event write it in a function and call it in pageload event

you have to set the DataTextField and DatavalueField

objc.ClientName = Convert.ToString(Session["ClientName"]);
            DataSet ClientN = obj.GetClientList();
            DataView Projview = new DataView();
            Projview.Table = ClientN.Tables[0];
            DrpClientName.DataSource = Projview;
            DrpClientName.DataTextField="Description";
            DrpClientName.DataValueField="ID";
            DrpClientName.DataBind();



回答2:


you didn't write what you want to display... you have to use DisplayMember ValueMember.

try this:

objc.ClientName = Convert.ToString(Session["ClientName"]);
DataSet ClientN = obj.GetClientList();
DataView Projview = new DataView();
Projview.Table = ClientN.Tables[0];
DrpClientName.DataSource = Projview;
DrpClientName.DisplayMember = "Column name that you want to display";
DrpClientName.ValueMember = "Column name that you want to get the values from";

Good luck.



来源:https://stackoverflow.com/questions/17184599/c-sharp-code-in-asp-net-for-displaying-database-values-in-dropdown-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!