Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have ddlCountryDropdown which is used to dispaly country name. I have table tblCountry which has coulmn name CountryName which i want to bind to ddlCountryDropdown at page load event.I am create one folder DTO in that i add class file MasterDTO.Cs in that i write below code using entity framework.but i don't how to bind data to dropdown at page_load event.please help me to solved my problems.

XML
public List<tblCountry> GetCountries()
       {

           List<tblCountry> d = null;
           using (var context = new HealthEntities())
           {
               d = context.tblCountries
                   .ToList<tblCountry>();

           }

           return d;
       }
Posted

try this..
add this code in your page_load event.


C#
MasterDTO obj = new MasterDTO;
ddlCountryDropdown.DataSourse = obj.GetCountries();
ddlCountryDropdown.DataTextField = "CountryName"; // for display
ddlCountryDropdown.DataValueField = "CountryId";  // for value u can bind country id if you have. other wise assign countryName.
ddlCountryDropdown.DataBind();



hope this will work...
 
Share this answer
 
Comments
Manohar Khillare 7-Feb-13 7:07am    
This give me error "The underlying provider failed on Open." at MasterDTO.Cs
d = context.tblCountries.ToList<tblCountry>();
Wasim1989 7-Feb-13 7:13am    
do you check your table properly mapped to your entityclass?
Wasim1989 7-Feb-13 7:17am    
try this..
IQueryable<tblCountry> iList= dataModel.tblCountry;
List<tblCountry> countryList = iList.ToList();
Manohar Khillare 7-Feb-13 7:33am    
where this code is need to place
Wasim1989 7-Feb-13 7:36am    
public List<tblCountry> GetCountries()
{
using (var context = new HealthEntities())
{
IQueryable<tblCountry> iList= context.tblCountry;
List<tblCountry> countryList = iList.ToList();
return countryList;
}
}

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