Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi how to populate an dropdownlist from an method declared as an IEnumerable?
Am getting an error: "Cannot implicitly convert type 'void' to 'string'"
on this line ddlUserType.DataValueField = ddlUserType.DataValueField = _UserTypeInfoList.Insert(0, new UserTypeInfo { UserType ="" }); from code behind file
This is the code am using from a class called UserTypeInfo:

public IEnumerable<UserTypeInfo> GetUserType()
    {
        using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), "[GetUserType]"))
        {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    yield return new UserTypeInfo
                    {
                        UserTypeId = Convert.ToInt32(reader["UserTypeId"]),
                        UserType = Convert.ToString(reader["UserType"])
                      
                    };
                }
            }
        }


And this the codebehind that am using on PageLoad of a asp.x file:

XML
if (!Page.IsPostBack)
        {

            UserTypeInfo _UserTypeInfo = new UserTypeInfo();
            List<UserTypeInfo> _UserTypeInfoList = _UserTypeInfo.GetUserType().ToList<UserTypeInfo>();
            ddlUserType.DataValueField = _UserTypeInfoList.Insert(0, new UserTypeInfo { UserType ="" });

        }


And Html code:

XML
<asp:Label ID="lblUserType" runat="server" class="label">User Type :</asp:Label>
<asp:DropDownList ID="ddlUserType" runat="server"></asp:DropDownList>


Need help!!
Posted

1 solution

The reason for the error is that 'Insert' method returns 'void' as here: http://msdn.microsoft.com/en-us/library/sey5k5z4%28v=vs.110%29.aspx[^]

set ddlUserType.DataValueField as

ddlUserType.DataValueField="UserType";
 
Share this answer
 
Comments
El Dev 3-Mar-14 2:38am    
Hi Vedat, It is not populating the dropdownlist...This is the code am using:

UserTypeInfo _UserTypeInfo = new UserTypeInfo();
List<usertypeinfo> _UserTypeInfoList = _UserTypeInfo.GetUserType().ToList<usertypeinfo>();
ddlUserType.DataValueField = "UserTypeId";
ddlUserType.DataTextField = "UserType";
Vedat Ozan Oner 3-Mar-14 2:42am    
set ddlUserType datasource as '_UserTypeInfoList'
El Dev 3-Mar-14 2:53am    
Thanks vedat!
Vedat Ozan Oner 3-Mar-14 2:54am    
you are welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900