+ Start a Discussion
sampath pallisampath palli 

problem in data verification using SOQL to login


I have an requirement i need to verify details existing in sobject using SOQL for example that i have an custom object with fields name,password and i have vf page like login page. before login first i need to check weather the given name and password which are given in login page is existed in object or not,If details are existed then go to home page Else it show message enter valid details
Please go through my code:
apex code:

public class loginapex_Rum {
    public String rname{set;get;}
    public String pwd{set;get;}
    public list<Registartion__c>result{set;get;}
    public pagereference register(){
        pageReference p=new pageReference('/apex/RegistrationPage_Rum');
        return p;
    }
    public pageReference go(){
        pageReference p=new pageReference('/apex/Home');
        return p;
    }
 
    public void getdetails(){
        result=[select Rum_Name__c,Password__c from Registartion__c];
        for(Registartion__c r:result){
            if(r.Rum_Name__c==rname&&r.Password__c==pwd){
                go();
            }else{
                
            }
        }
    }

}


vf page:

<apex:page controller="loginapex_Rum">
    <apex:form >
    <apex:pageBlock title="Login" >
        <apex:pageBlockSection columns="1">
            <apex:pageBlockSectionItem >
            <apex:outputLabel value="Enter Rum Name"/>
            <apex:inputText value="{!rname}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Enter Password" />
                <apex:inputSecret value="{!pwd}"/>
            </apex:pageBlockSectionItem>
          <apex:pageBlockSectionItem >
            <apex:commandButton value="Login" action="{!getdetails}"/>
            <apex:commandButton value="SignUp" action="{!register}"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="2">
        <apex:pageBlockSectionItem >
            <nbsp></nbsp>
            <apex:commandLink value="Forget Password"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:form>
</apex:page>
Best Answer chosen by sampath palli
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.

public class loginapex_Rum
{
    public String rname{set;get;}
    public String pwd{set;get;}
    public list<Registartion__c> result{set;get;}
    public pagereference register()
    {
        pageReference p=new pageReference('/apex/RegistrationPage_Rum');
        return p;
    }
    public pageReference go(){
        pageReference p=new pageReference('/apex/Home');
        return p;
    }
 
    public pageReference getdetails()
    {
        result=[select Rum_Name__c,Password__c from Registartion__c where Rum_Name__c = :rname and Password__c =:pwd ];
        if(result.size() > 0 )
        {
            go();
        }
        else
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Invalid Password'));
            return null;
        }
    }

}

Let us know if this will help you

All Answers

Nagendra Prasad ChNagendra Prasad Ch
Here is the code,

Controller,
public class loginapex_Rum {
    public String rname{set;get;}
    public String pwd{set;get;}
    public Registartion__c result{set;get;}
	
    public pagereference register(){
        pageReference p=new pageReference('/apex/RegistrationPage_Rum');
        return p;
    }
    public pageReference go(){
        pageReference p=new pageReference('/apex/Home');
        return p;
    }
 
    public void getdetails(){
	
		// Query only current user registration record if exists
        result=[select Rum_Name__c,Password__c from Registartion__c where Rum_Name__c = :rname];
       
            if(result == null){
			//No username in database 
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Usename not registered'));
			return null;
			}
			else if(result.Password__c==pwd){
                go();
            }else{
             // Error msg  for Invalid password 
				ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Invalid Password'));
					return null;			 
            }
        
    }

}

VF page,
 
<apex:page controller="loginapex_Rum">
    <apex:form >
	<apex:pageMessages ></apex:pageMessages>
    <apex:pageBlock title="Login" >
        <apex:pageBlockSection columns="1">
            <apex:pageBlockSectionItem >
            <apex:outputLabel value="Enter Rum Name"/>
            <apex:inputText value="{!rname}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Enter Password" />
                <apex:inputSecret value="{!pwd}"/>
            </apex:pageBlockSectionItem>
          <apex:pageBlockSectionItem >
            <apex:commandButton value="Login" action="{!getdetails}"/>
            <apex:commandButton value="SignUp" action="{!register}"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="2">
        <apex:pageBlockSectionItem >
            <nbsp></nbsp>
            <apex:commandLink value="Forget Password"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:form>
</apex:page>


 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
1) http://www.sfdcpoint.com/salesforce/show-error-message-visualforce-page/
2) http://www.infallibletechie.com/2012/10/how-to-display-error-messages-in.html
3) http://www.infallibletechie.com/2013/04/how-to-add-error-message-in-visualforce.html


To display error messages in the Visualforce page add below tag where you want to display the error message.
Visualforce page:
<apex:pageMessages />


Apex Controller:
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.');
ApexPages.addMessage(myMsg); 
 
<apex:page controller="loginapex_Rum">
    <apex:form >
    <apex:pageBlock title="Login" >
<apex:pageMessages />
        <apex:pageBlockSection columns="1">
            <apex:pageBlockSectionItem >
            <apex:outputLabel value="Enter Rum Name"/>
            <apex:inputText value="{!rname}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Enter Password" />
                <apex:inputSecret value="{!pwd}"/>
            </apex:pageBlockSectionItem>
          <apex:pageBlockSectionItem >
            <apex:commandButton value="Login" action="{!getdetails}"/>
            <apex:commandButton value="SignUp" action="{!register}"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="2">
        <apex:pageBlockSectionItem >
            <nbsp></nbsp>
            <apex:commandLink value="Forget Password"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class loginapex_Rum {
    public String rname{set;get;}
    public String pwd{set;get;}
    public list<Registartion__c>result{set;get;}
    public pagereference register(){
        pageReference p=new pageReference('/apex/RegistrationPage_Rum');
        return p;
    }
    public pageReference go(){
        pageReference p=new pageReference('/apex/Home');
        return p;
    }
 
    public void getdetails(){
        result=[select Rum_Name__c,Password__c from Registartion__c];
        for(Registartion__c r:result){
            if(r.Rum_Name__c==rname&&r.Password__c==pwd){
                go();
            }else{
                
				ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Invalid password.');
				ApexPages.addMessage(myMsg); 
            }
        }
    }

}


Let us know if this will help you
 
sampath pallisampath palli
Hi Ch Nagendra Prasad
Thanks for your reply 
when i run your suggested code it throws error:"Initial term of field expression must be a concrete SObject: List<Registartion__c>"

 
Nagendra Prasad ChNagendra Prasad Ch
Small change in controller(Line no 18),
 
public class loginapex_Rum {
    public String rname{set;get;}
    public String pwd{set;get;}
    public Registartion__c result{set;get;}
	
    public pagereference register(){
        pageReference p=new pageReference('/apex/RegistrationPage_Rum');
        return p;
    }
    public pageReference go(){
        pageReference p=new pageReference('/apex/Home');
        return p;
    }
 
    public void getdetails(){
	
		// Query only current user registration record if exists
        result=[select id,Rum_Name__c,Password__c from Registartion__c where Rum_Name__c = :rname][0];
       
            if(result == null){
			//No username in database 
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Usename not registered'));
			return null;
			}
			else if(result.Password__c==pwd){
                go();
            }else{
             // Error msg  for Invalid password 
				ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Invalid Password'));
					return null;			 
            }
        
    }

}

 
sampath pallisampath palli
Hi Ch Nagendra Prasad thanks for ur reply

Again it's fires error like"Illegal assignment from Registartion__c to List<Registartion__c>" in line No:18

is their any alternate way to verify login details
Nagendra Prasad ChNagendra Prasad Ch
There is no List in the code given by me. I removed the list and made it Registartion__c Sobject. Paste the code as it is.
 
public class loginapex_Rum {
    public String rname{set;get;}
    public String pwd{set;get;}
    public Registartion__c result{set;get;}
	
    public pagereference register(){
        pageReference p=new pageReference('/apex/RegistrationPage_Rum');
        return p;
    }
    public pageReference go(){
        pageReference p=new pageReference('/apex/Home');
        return p;
    }
 
    public void getdetails(){
	
		// Query only current user registration record if exists
        result=[select Rum_Name__c,Password__c from Registartion__c where Rum_Name__c = :rname][0];
       
            if(result == null){
			//No username in database 
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Usename not registered'));
			return null;
			}
			else if(result.Password__c==pwd){
                go();
            }else{
             // Error msg  for Invalid password 
				ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Invalid Password'));
					return null;			 
            }
        
    }

}

 
Amit Chaudhary 8Amit Chaudhary 8
Hi Sampath,

Please try below code.
Controller :-
public class loginapex_Rum 
{
    public String rname{set;get;}
    public String pwd{set;get;}
    public list<Registartion__c> result{set;get;}
    public pagereference register()
	{
        pageReference p=new pageReference('/apex/RegistrationPage_Rum');
        return p;
    }
    public pageReference go(){
        pageReference p=new pageReference('/apex/Home');
        return p;
    }
 
    public void getdetails()
	{
        result=[select Rum_Name__c,Password__c from Registartion__c where Rum_Name__c = :rname and Password__c =:pwd ];
		if(result.size() > 0 )
		{
			go();
		}
		else
		{
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Invalid Password'));
		}
    }

}
Page :-
<apex:page controller="loginapex_Rum">
    <apex:form >
    <apex:pageBlock title="Login" >
<apex:pageMessages />
        <apex:pageBlockSection columns="1">
            <apex:pageBlockSectionItem >
            <apex:outputLabel value="Enter Rum Name"/>
            <apex:inputText value="{!rname}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Enter Password" />
                <apex:inputSecret value="{!pwd}"/>
            </apex:pageBlockSectionItem>
          <apex:pageBlockSectionItem >
            <apex:commandButton value="Login" action="{!getdetails}"/>
            <apex:commandButton value="SignUp" action="{!register}"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="2">
        <apex:pageBlockSectionItem >
            <nbsp></nbsp>
            <apex:commandLink value="Forget Password"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:form>
</apex:page>
Let us know if this will help you

Thanks
Amit Chaudhary


 
sampath pallisampath palli
Hi Ch Nagendra Prasad

I tried ur code but it gives error like "Void method must not return a value" And i checked by removing the return statements and when i trying to login it gets error please check this clip
User-added image
sampath pallisampath palli
HI Amit Chaudhary 8

Thanks for ur reply,
I tried your code it don't get any error but when i tried to login with existing details it doesn't go to the home page, I think go() method doesn't calling in getdetails() method, And if i give wrong details in login page it don't fires error message
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.

public class loginapex_Rum
{
    public String rname{set;get;}
    public String pwd{set;get;}
    public list<Registartion__c> result{set;get;}
    public pagereference register()
    {
        pageReference p=new pageReference('/apex/RegistrationPage_Rum');
        return p;
    }
    public pageReference go(){
        pageReference p=new pageReference('/apex/Home');
        return p;
    }
 
    public pageReference getdetails()
    {
        result=[select Rum_Name__c,Password__c from Registartion__c where Rum_Name__c = :rname and Password__c =:pwd ];
        if(result.size() > 0 )
        {
            go();
        }
        else
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Invalid Password'));
            return null;
        }
    }

}

Let us know if this will help you
This was selected as the best answer
sampath pallisampath palli
Hi Amit Chaudhary 
Thanks for ur reply I tried ur code and made small change in code we need to use "return go()"