Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
C#
var query = (from Customer in context.tbl_Customer

                            select new
                            {
                                Value = Customer.CustomerID,
                                Text = Customer.CName
                            });
               return query;


Above query returns value and text

C#
public static void FillDDLCustomer(DropDownList ddl, Boolean is_Select, Boolean is_All)
       {
           try
           {
               ddl.DataSource = _ComboRepo.FillDDLCustomer();
               ddl.DataValueField = "Value";
               ddl.DataTextField = "Text"; //Here I wanna bind Text as well as Value
               ddl.DataBind();
               if (is_Select)
               {
                   ddl.Items.Insert(0, new ListItem("------Select------", "0"));
               }
               else
               {
                   ddl.Items.Insert(0, new ListItem("------Select All------", "0"));
               }
           }
           catch (Exception ex) { }
       }



How to bind returned text and Value to DataTextField?
Posted

Try the below code
C#
var query = (from Customer in context.tbl_Customer
 
                            select new
                            {
                                Value = Customer.CustomerID,
                                Text=string.Format("{0} {1}" ,Customer.CustomerID,Customer.CName)

                            });
               return query;

Hope it helps
 
Share this answer
 
Comments
m-shraddha 25-Sep-14 6:08am    
Both DataSource and DataSourceID are defined on 'ddlCustomer'. Remove one definition.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'ddlCustomer'. Remove one definition.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: Both DataSource and DataSourceID are defined on 'ddlCustomer'. Remove one definition.]
System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +1779734
System.Web.UI.WebControls.DataBoundControl.OnLoad(EventArgs e) +19
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Control.LoadRecursive() +146
System.Web.UI.Control.LoadRecursive() +146
System.Web.UI.Control.LoadRecursive() +146
System.Web.UI.Control.LoadRecursive() +146
System.Web.UI.Control.LoadRecursive() +146
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
ChauhanAjay 25-Sep-14 6:18am    
Check this link for the error
http://www.codeproject.com/Questions/583412/BothplusDataSourceplusandplusDataSourceIDplusarepl

Hope it helps
Do it following way
if (is_Select)
       {
           ddl.Items.Insert(0, new ListItem { Text = "------Select------", Value = "0" });
       }
       else
       {
           ddl.Items.Insert(0, new ListItem { Text = "------Select All------", Value = "0" });
       }
 
Share this answer
 
Comments
m-shraddha 25-Sep-14 6:09am    
not working
[no name] 25-Sep-14 6:36am    
is any issue why not working??
m-shraddha 26-Sep-14 2:38am    
your code is to simply bind text and value. But what I am expecting is different. I want to bind two columns to the DataTextField of dropdownlist
[no name] 26-Sep-14 2:44am    
whatever you want to show in DataTextField then change Text = "------Select------", this one according you it's simple dear

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