Click here to Skip to main content
12,395,645 members (82,286 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: .NET .NET4
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.

public List<tblCountry> GetCountries()
       {
 
           List<tblCountry> d = null;
           using (var context = new HealthEntities())
           {
               d = context.tblCountries
                   .ToList<tblCountry>();
 
           }
 
           return d;
       }
Posted 6-Feb-13 23:47pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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


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...
  Permalink  
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;
}
}
Manohar Khillare 7-Feb-13 7:50am
   
again it showing me same error
Wasim1989 7-Feb-13 7:53am
   
you should refer those links which i posted in second answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100