How to display Custom Setting data using Apex in Visualforce page?

Sample Code:

Visualforce Page:

<apex:page controller="sample" sidebar="false" >
<apex:form >
    <apex:pageblock >
        <apex:pageblockTable value="{!code}" var="c">
            <apex:column value="{!c.Name}"/>       
            <apex:column value="{!c.Code__c}"/>
        </apex:pageblockTable>
    </apex:pageblock>
</apex:form>
</apex:page>


Apex Controller:

public class sample
{
    public List<CountryCodes__c> code {get;set;}
  
    public sample()
    {
        Map<String,CountryCodes__c> allCodes = CountryCodes__c.getAll();
        code = allCodes.values();
    } 
  
}


Output:



4 comments:

  1. hi, I am getting this error after using this code
    Error: customsetting Compile Error: Not of type Custom Settings at line 10 column 47

    ReplyDelete
    Replies
    1. hi,
      You go Develop->custom settings create new custom setting as CountryCodes and create this custom setting related fileds like Name(type text(42)), Code(type text(4)).After you execute this code.....

      Delete