function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sainath .chalamcherlasainath .chalamcherla 

soql query output in visualforce

Hi,
I got strucked in this example.I want to show the SOQL query result in table format in visualforce.
Apex Code:
public class AccountClass1
{
public list<Account> acc{get;set;}
public List<Contact> ccc{get;set;}
public  List<Account> AccountClass2()
{
 List<Account> acc = [select Name,Phone,Type from Account where Name='capital first'];
 return acc;
}
public  List<Contact>  AccountClass3()
{
  List<Contact> ccc = [SELECT AccountId,Name FROM Contact where AccountId IN(select Id  from Account where Name='capital first')];
  return ccc;
}
}
VIsualForce Code:
<apex:page controller="AccountClass1" sidebar="True" showHeader="true">
  <apex:form >
  <apex:commandButton action="{!AccountClass2}" value="Accountquery" id="ss"/>
  <apex:pageBlock>
  <apex:pageblockTable value="{!acc}" var="a" id="ss">
  <apex:column value="{!a.Name}"/>
  <apex:column value="{!a.Phone}"/>
  <apex:column value="{!a.Type}"/>
  </apex:pageblockTable>
  </apex:pageBlock>
  <apex:commandButton action="{!AccountClass3}" value="Contactquery" id="ds" />
  <apex:pageBlock>
  <apex:pageblockTable value="{!ccc}" var="c" id="ds">
  <apex:column value="{!c.AccountId}"/>
  <apex:column value="{!c.Name}"/>
  </apex:pageblockTable>
  </apex:pageBlock>
  <script>
  </script>
  </apex:form>
</apex:page>

output 
User-added image

After clicking on AccountQuery or ContactQuery I am getting the following output
User-added image

 
Best Answer chosen by sainath .chalamcherla
Rohit SharmaGRohit SharmaG
Hi, Change as below -
public List<Account> AccountClass2() {
 acc = [select Name,Phone,Type from Account where Name='capital first'];
return acc;
}

All Answers

Rohit SharmaGRohit SharmaG
Hi, Change as below -
public List<Account> AccountClass2() {
 acc = [select Name,Phone,Type from Account where Name='capital first'];
return acc;
}
This was selected as the best answer
Pankaj_GanwaniPankaj_Ganwani
Please use below mentioned code:
 
public class AccountClass1
{
public list<Account> acc{get;set;}
public List<Contact> ccc{get;set;}
public  void getAccountClass2()
{
 acc = [select Name,Phone,Type from Account where Name='capital first'];
 
}
public  void  getAccountClass3()
{
  ccc = [SELECT AccountId,Name FROM Contact where AccountId IN(select Id  from Account where Name='capital first')];
}
}
 
<apex:page controller="AccountClass1" sidebar="True" showHeader="true">
  <apex:form >
  <apex:commandButton action="{!AccountClass2}" value="Accountquery" rerender="ss"/>
  <apex:pageBlock>
  <apex:pageblockTable value="{!acc}" var="a" id="ss">
  <apex:column value="{!a.Name}"/>
  <apex:column value="{!a.Phone}"/>
  <apex:column value="{!a.Type}"/>
  </apex:pageblockTable>
  </apex:pageBlock>
  <apex:commandButton action="{!AccountClass3}" value="Contactquery" rerender="ds" />
  <apex:pageBlock>
  <apex:pageblockTable value="{!ccc}" var="c" id="ds">
  <apex:column value="{!c.AccountId}"/>
  <apex:column value="{!c.Name}"/>
  </apex:pageblockTable>
  </apex:pageBlock>
  <script>
  </script>
  </apex:form>
</apex:page>