Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Friends,
Go through my code,

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        clsUser objUser = new clsUser();
        Hashtable hstUser = new Hashtable();
        hstUser.Add("username", Login1.UserName);
        hstUser.Add("pwd", Login1.Password);
        objUser.setDetails(hstUser);

        if (Validate(objUser) == true)
        {
            Response.Redirect("..\\Pages\\frmRegistration.aspx");
        }
        else
            e.Authenticated = false;
    }
    private bool Validate(clsUser objUser) //Line1
    {
        UserBLL objUserBLL = new UserBLL();
        int intResult = objUserBLL.ValidateLogin(objUser);
        if(intResult > 0)
            return true;
        else if(intResult < 0)
            return false;
    }


I'm getting error at Line1 saying:
'_Default.Validate(clsUser)': not all code paths return a value	<br />


Can anyone help where I'm going wrong?

Thanks in advance.
Posted

If you declare a function to return a particular type of value (ie. something other than void), then you must ensure that all possible ways of exiting the function (implicit finish at the end of the function, or explicit with the use of a return statement) return a value compatible with the declared data type (eg. null would be compatible with both the string and Object data types).

Declare one variable and use it in return.
private bool Validate(clsUser objUser) //Line1
        {
            bool flag = false;

            UserBLL objUserBLL = new UserBLL();
            int intResult = objUserBLL.ValidateLogin(objUser);
            if (intResult > 0)
                flag = true;
            else if (intResult < 0)
                flag = false;

            return flag;
        }
 
Share this answer
 
v3
Comments
dhage.prashant01 4-May-11 6:21am    
Thank You, but can u tell what does that error really meant?
Toniyo Jackson 4-May-11 6:27am    
If you declare a function to return a particular type of value (ie. something other than void), then you must ensure that all possible ways of exiting the function (implicit finish at the end of the function, or explicit with the use of a return statement) return a value compatible with the declared data type (eg. null would be compatible with both the string and Object data types).
dhage.prashant01 4-May-11 6:35am    
In my code there was two exit point, because when i removed if part from else clause my code working fine
Toniyo Jackson 4-May-11 6:38am    
You have written if and else if condition. If these both conditions are not satisfying means what will happen? So there should a exit point.
dhage.prashant01 4-May-11 6:43am    
Nice explanation.
write
return true
at the end of your validate function like

C#
private bool Validate(clsUser objUser) //Line1
    {
        UserBLL objUserBLL = new UserBLL();
        int intResult = objUserBLL.ValidateLogin(objUser);
        if(intResult > 0)
            return true;
        else if(intResult < 0)
            return false;
       return true;
    }
 
Share this answer
 
v2
Comments
dhage.prashant01 4-May-11 5:59am    
It gives another error: cannot convert null to bool
Ashishmau 4-May-11 6:00am    
write return true instead of null
Toniyo Jackson 4-May-11 6:07am    
Its not a correct way. What will happen if intResult < 0?
dhage.prashant01 4-May-11 6:18am    
Thank U.
But may i know, what the error really mean?
dhage.prashant01 4-May-11 6:19am    
yes its not working when intResult < 0.
Any other way

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900