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
Rajendra Shukla 22Rajendra Shukla 22 

hi trying for dynamic getting error on query ... tried different ways not working any help would be appreciated

Controlleer---------------------->
public class InterviewTwo_Controller {
    public list<Account>listAccount{set;get;}
    public String searchString{set;get;}
    public boolean showmsg{set;get;}
    public InterviewTwo_Controller(){
        listAccount =new list<Account>();
        showmsg= false;
    }
    public PageReference displayAccounts(){
        string AccountQuery='select id,name,BillingState,Phone,Website from Account where name Like '+searchString;
        if(searchString !='' && searchString != null){
          // AccountQuery= AccountQuery+'where name Like \'%'+searchString+'%\'';
           // AccountQuery= AccountQuery+searchString;
            listAccount=database.query('select id,name,BillingState,Phone,Website from Account where name Like '+searchString);
            //listAccount=database.query(searchString);
            if(listAccount.size()>0 && listAccount!=null){
                showmsg=false;
            }else{
                ApexPages.Message errMsg =new ApexPages.Message(Apexpages.Severity.WARNING,'No Account Found');
                ApexPages.addMessage(errMsg);
                showmsg=true;
            }
        }
        return null;
    }
}

VFPage------------------------->
<apex:page controller="InterviewTwo_Controller" sidebar="false" >
    <apex:form >
    <apex:sectionHeader title="InterviewTwo"/>
    <apex:pageBlock mode="Edit">
        <apex:pageBlockSection title="Search">
            <apex:inputText label="Search" value="{!searchString}" />
            <apex:commandButton value="Search" action="{!displayAccounts}" />
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Result" rendered="{!showmsg==true}" >
            <apex:pageMessages rendered="{!showmsg}">
            </apex:pageMessages><br/>
            <apex:pageBlockTable value="{!listAccount}" var="a" headerClass="headerclass">
                <apex:column headerValue="Account_Name" value="{!a.Name}" />
                <apex:column headerValue="Billing" value="{!a.BillingState}"/>
                <apex:column headerValue="Phone" value="{!a.phone}" />
                <apex:column headerValue="Website" value="{!a.Website}"/>                
            </apex:pageBlockTable>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Result" rendered="{!showmsg==false}" >
            <apex:outputLabel value="{!searchString}" />
            <apex:pageBlockTable value="{!listAccount}" var="a" headerClass="headerclass">
                <apex:column headerValue="Account Name" value="{!a.Name}" />
                <apex:column headerValue="Billing" value="{!a.BillingState}"/>
                <apex:column headerValue="Phone" value="{!a.phone}" />
                <apex:column headerValue="Website" value="{!a.Website}"/>                
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
   </apex:form>
</apex:page>
Amit Chaudhary 8Amit Chaudhary 8

Please update your apex class like below
public class InterviewTwo_Controller 
{
    public list<Account>listAccount{set;get;}
    public String searchString{set;get;}
    public boolean showmsg{set;get;}

    public InterviewTwo_Controller()
    {
        listAccount =new list<Account>();
        showmsg= false;
    }
    
    public PageReference displayAccounts()
    {
        string AccountQuery='select id,name,BillingState,Phone,Website from Account where name Like '+searchString;
        if(searchString !='' && searchString != null)
        {
            listAccount=database.query('select id,name,BillingState,Phone,Website from Account where name Like :searchString');
            //listAccount=database.query(searchString);
            if(listAccount.size()>0 && listAccount!=null)
            {
                showmsg=false;
            }else{
                ApexPages.Message errMsg =new ApexPages.Message(Apexpages.Severity.WARNING,'No Account Found');
                ApexPages.addMessage(errMsg);
                showmsg=true;
            }
        }
        return null;
    }
}
NOTE:- I just updated your query. Same is working in my dev org
User-added image

 
Rajendra Shukla 22Rajendra Shukla 22
Thanks, 

Amit, its working