Click here to Skip to main content
11,704,768 members (44,573 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: .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
           return d;
Posted 6-Feb-13 23:47pm
Rate this: bad
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.

hope this will work...
Manohar Khillare at 7-Feb-13 7:07am
This give me error "The underlying provider failed on Open." at MasterDTO.Cs
d = context.tblCountries.ToList<tblCountry>();
Wasim1989 at 7-Feb-13 7:13am
do you check your table properly mapped to your entityclass?
Wasim1989 at 7-Feb-13 7:17am
try this..
IQueryable<tblCountry> iList= dataModel.tblCountry;
List<tblCountry> countryList = iList.ToList();
Manohar Khillare at 7-Feb-13 7:33am
where this code is need to place
Wasim1989 at 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 at 7-Feb-13 7:50am
again it showing me same error
Wasim1989 at 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
0 Sergey Alexandrovich Kryukov 679
1 OriginalGriff 455
2 Andy Lanng 245
3 CPallini 242
4 Maciej Los 225
0 OriginalGriff 8,864
1 Sergey Alexandrovich Kryukov 8,245
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606

Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2015
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