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:



6 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
  2. how to insert the data into this object

    ReplyDelete
  3. Suppose If i Have 5 Fields in my Custom Setting and i only want to use Particular 2-3 Fields From them in the Controller of a Vf page.
    How can i Do that??

    ReplyDelete