Home > DeveloperSection > Forums > Binding Data text field and Data value field to asp:Dropdownlist
Maria Susan

Total Post:29

Posted on    November-03-2014 5:25 AM

 ASP.Net C#  Dropdown 

 1 Reply(s)
 769  View(s)
Rate this:

I have created a helper function to Bind Drop down list in asp.net. See my function:

  public void BindDDL(string query, DropDownList DDL)
        List<Issuetype> obj = new List<Issuetype>();
        Issuetype iss = new Issuetype();
        iss.DeptId = 1;
        iss.Issue = "SSS";
        iss.IssuetypeId = 4;
        //BALissue Bl = new BALissue();
        //List<Issuetype> objSource = null;
        //objSource = Bl.Bind_issuetypes(query);
        DDL.DataSource = obj;
        DDL.DataValueField = Convert.ToString(obj[0]);
        DDL.DataTextField = Convert.ToString(obj[1]);


In this way if i send the query name and Dropdownlist id to the function, drop down should be binded by the List of Issuetype entity, You can see the properties of Issuetype in code.

But however i am not able to Set the DataValueField and DataTextField correctly. Every time it is saying index out of range.

Chris S
Chris S

Total Post:48

Posted on    November-03-2014 6:48 AM

Actually there is no obj[1] in your code because you have only one item in the obj list, so at

DDL.DataTextField = Convert.ToString(obj[1]);

this line you will get the exception

Instead you can use

   DDL.DataValueField = "Issue"; 
   DDL.DataTextField = "IssuetypeId";

And to get property names use Reflection

using System.Reflection;

        PropertyInfo[] propertyInfos;

        propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public | BindingFlags.Static);


        delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)

        { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });


            foreach (PropertyInfo propertyInfo in propertyInfos)





Don't want to miss updates? Please click the below button!

Follow MindStick