+ Start a Discussion
Vijaya Kumar RegantiVijaya Kumar Reganti 

System.Query Exception: List has no rows for assignment to sObject

when I try to click on the Login button which I ahve created with out giving any credentials,This is the exception  I got.So please help  me to handle this error.

 

Class :

 

public with sharing class logincls {

Public boolean show {get;set;}
Public string username {get;set;}
public string password {get;set;}

Public pagereference login(){

    Registrations__c reg = [Select id,Password__c from Registrations__c where user_name__c =: username];
   
    if(reg.password__c == password){
    
        pagereference ref = new pagereference('/apex/products');
        return ref;
    }
    
    else{
    
        show = true;
        return null;
    }
  }
 
     public Logincls(){
    
        show = false;
 
    }
    
  }

 

VF Page:

 

<apex:page sidebar="false" controller="logincls">
     <apex:form >
     <apex:pagemessages ></apex:pagemessages>
         <apex:outputPanel >
         <table align="center">
             <tr>
                 <td>
                 </td>
                 <td><apex:outputLabel rendered="{!show}" style="color:red;fint-size:20">**Warning: Invalid Credentials</apex:outputlabel>
                 </td>
             </tr>    
             <tr>
                 <td>
                 <apex:outputLabel >User Name</apex:outputLabel>
                 </td>
                 <td>
                 <apex:inputText value="{!username}"/>
                 </td>
             </tr>
             <tr>
                 <td>
                     <apex:outputLabel >Password</apex:outputLabel>
                 </td>
                 <td>
                     <apex:inputsecret value="{!password}"/>
                 </td>
             </tr>
             <tr >
                 <td align="center" colspan="2">    
                     <apex:commandButton value="Login" action="{!login}"/>
                 </td>
             </tr>
              <tr>
                 <td>
                 </td>
             </tr>
              <tr>
                 <td>
                 </td>
             </tr>
              <tr>
                 <td>
                 </td>
             </tr>
             <tr >
                 <td align="center" colspan="2">
                     <apex:commandLink value="New User Registration" action="/apex/register"/>  &nbsp;&nbsp; &nbsp;
                     <apex:commandLink value="Forgot Password ??" action="/apex/forgot"/>      
                 </td>
             </tr>
            
             </table>    
         </apex:outputPanel>
     </apex:form>
</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
Vijaya Kumar RegantiVijaya Kumar Reganti

Hi. I got my own solution as below using TRY & CATCH blocks and ApexPages.AddMessage() methodto display a meaningful error message which is understandable by the end user.

 

public with sharing class logincls {

Public boolean show {get;set;}
Public string username {get;set;}
public string password {get;set;}

Public pagereference login(){

    system.debug('----------userName--------->'+username);
    system.debug('----------Password--------->'+password);
    try{
       Registrations__c reg = [Select id,Password__c from Registrations__c where user_name__c =: username];
      
    if(reg.password__c == password){
    
        pagereference ref = new pagereference('/apex/products');
        return ref;
    }
    
    else{
    
        show = true;
        return null;
    }
    }
    catch(exception e){
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.severity.warning,'Please enter the Credentials'));
    }
    
   
        return null;  
  }
   public Logincls(){
    
        show = false;
      
 
    }
 
  }

All Answers

souvik9086souvik9086

Hi,

 

Try this

 

Public pagereference login(){
if(username != NULL && password != NULL){
    Registrations__c reg = [Select id,Password__c from Registrations__c where user_name__c =: username];
   
    if(reg.password__c == password){
    
        pagereference ref = new pagereference('/apex/products');
        return ref;
    }
    
    else{
    
        show = true;
        return null;
    }

}

else{
    
        show = true;
        return null;
    }
  }

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

 

Avidev9Avidev9
Public pagereference login() {
    if (username != NULL && password != NULL) {
        List<Registrations__c> regList = [Select id, Password__c from Registrations__c where user_name__c = : username];

        if (!regList.isEmpty() && regList[0].password__c == password) {

            pagereference ref = new pagereference('/apex/products');
            return ref;
        } else {

            show = true;
            return null;
        }
    } else {

        show = true;
        return null;
    }
}

 Try the above code. THis makes sure that code works even if no record is returned from the query

Vijaya Kumar RegantiVijaya Kumar Reganti

Sorry it didn't work.can u suggest how to use TRY and CATCH blocks???

Avidev9Avidev9
Didnt worked ?
I guess you have to define what to do when there are no records. regList.isEmpty() this returns true when there are no records returned by query .

So I guess you can play with the code to achieve the desired result
Vijaya Kumar RegantiVijaya Kumar Reganti

I have created two input text boxes for Username & Password.

If the user inputs no data in that and clicks on the Login button, then this error occers. I have to handle this exception and display a meaningful message to enter the credentials and click once again using TRY and CATCH blocks.

 

Thanks.

 

 

Vijaya Kumar RegantiVijaya Kumar Reganti

Hi. I got my own solution as below using TRY & CATCH blocks and ApexPages.AddMessage() methodto display a meaningful error message which is understandable by the end user.

 

public with sharing class logincls {

Public boolean show {get;set;}
Public string username {get;set;}
public string password {get;set;}

Public pagereference login(){

    system.debug('----------userName--------->'+username);
    system.debug('----------Password--------->'+password);
    try{
       Registrations__c reg = [Select id,Password__c from Registrations__c where user_name__c =: username];
      
    if(reg.password__c == password){
    
        pagereference ref = new pagereference('/apex/products');
        return ref;
    }
    
    else{
    
        show = true;
        return null;
    }
    }
    catch(exception e){
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.severity.warning,'Please enter the Credentials'));
    }
    
   
        return null;  
  }
   public Logincls(){
    
        show = false;
      
 
    }
 
  }

This was selected as the best answer