---
title: "Binding Data text field and Data value field to asp:Dropdownlist"  
description: "Binding Data text field and Data value field to asp:Dropdownlist"  
author: "Anonymous User"  
published: 2014-11-03  
updated: 2014-11-03  
canonical: https://www.mindstick.com/forum/2497/binding-data-text-field-and-data-value-field-to-asp-dropdownlist  
category: "asp.net"  
tags: ["c#", "dropdown"]  
reading_time: 2 minutes  

---

# Binding Data text field and Data value field to asp:Dropdownlist

\
I have created a helper [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to Bind [Drop down](https://www.mindstick.com/forum/263/drop-down-list-problem) 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;        obj.Add(iss);        //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]);        DDL.DataBind();    }
```

\
In this way if i send the [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) [name](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide) and [Dropdownlist](https://www.mindstick.com/articles/324837/how-to-bind-dropdownlist-in-mvc-core-from-database-using-entity-framework) id to the function, drop down should be binded by the List of Issuetype [entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach), You can see the [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties) of Issuetype in code.\
\
But however i am not able to Set the [DataValueField](https://www.mindstick.com/forum/12846/dropdownlist-datavaluefield-in-asp-dot-net-c-sharp) and DataTextField correctly. Every time it is saying [index](https://www.mindstick.com/blog/198/index-in-sql-server) out of range.\
\

## Replies

### Reply by Anonymous User

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);        Array.Sort(propertyInfos,        delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)        { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });             foreach (PropertyInfo propertyInfo in propertyInfos)            {                Console.WriteLine(propertyInfo.Name);            }        }
```


---

Original Source: https://www.mindstick.com/forum/2497/binding-data-text-field-and-data-value-field-to-asp-dropdownlist

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
