Click here to Skip to main content
Rate this: bad
good
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
                   .ToList<tblCountry>();
 
           }
 
           return d;
       }
Posted 7-Feb-13 0:47am
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 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 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Afzaal Ahmad Zeeshan 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2014
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