I want to bind two column from database into dropdownlist. Here’s my code:
SqlConnection con=new SqlConnection(CommonRefference.Constr());
string query = "Select Id,Name+' ('+Distribution_name+') 'as Name1 from BR_supervisor ";
SqlCommand cmd = new SqlCommand(query,con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
DropDownList3.DataSource = query;
DropDownList3.DataTextField = "name1";
DropDownList3.DataValueField = "Id";
DropDownList3.DataBind();
}
con.Close();
But it gives the following error
DataBinding: ‘System.Char’ does not contain a property with the name ‘name1’
How to do it? Anyone helps me is highly appreciated
Its Name1 not name1
change this to
EDIT:
You are binding a string(query) to datasource here
and string dont have a property with the name ‘name1’,so error
SOLUTION :
refer this,I have changed this for your requirement
For more explanation refer this link Populate-ASP.NET-dropdownlist