HOW TO CHANGE FAILURE TEXT FOR TWO DIFFERENT POSSIBILITIES?

Brad Pitt

Total Post:61

Points:429
Posted by  Brad Pitt
C# 
 1090  View(s)
Ratings:
Rate this:

I have ASP Login Control in my page and a 

<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>

What I want to do is that when I perform authentication in 

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    DAL = new DataAccessLayer();
    conObj = DAL.openConnection();
    string query = "SELECT * FROM tblUser WHERE UEmail = '"+Login1.UserName+"'"; 
    SqlDataReader SQLDR = DAL.ExecuteQueryReturnValue(query); 
    if (SQLDR.Read())
    {
        if (SQLDR["UPassword"].ToString() == Login1.Password.ToString())
        { 
            Session["userName"] = SQLDR["UEmail"].ToString();
            e.Authenticated = true;
        }
        else {
            // Change the 'FailureText' to "password is incorrect"
            e.Authenticated = false;
        } 
    }
    else
    {
        // Here I want to change the 'FailureText' to "User does not exists"
        e.Authenticated = false;
    }
   }

set the text of FailureText to 

"Incorrect Password"

Or set to

"User does not exist"

for both possibilities.

  1. Simons Hood

    Post:70

    Points:494
    Re: How to change Failure text for two different possibilities?

    You can declare a string variable within the method and set it accordingly: 

    protected void Login1_Authenticate(object sender,AuthenticateEventArgs e)
    {
        DAL = new DataAccessLayer();
        conObj =DAL.openConnection();
        string query ="SELECT * FROM tblUser WHERE UEmail ='"+Login1.UserName+"'";
        string failureText=string.empty;
     
        SqlDataReader SQLDR = DAL.ExecuteQueryReturnValue(query);
     
        if (SQLDR.Read())
        {
            if(SQLDR["UPassword"].ToString() == Login1.Password.ToString())
            { 
                Session["userName"] = SQLDR["UEmail"].ToString();
                e.Authenticated = true;
            }
            else {
                // Change the 'FailureText' to "password is incorrect"            failureText="password is incorrect";
                e.Authenticated = false;
            }
     
        }
        else
        {
            // Here I want to change the 'FailureText' to "User does not exists"        failureText="User does not exists";
            e.Authenticated = false;
        }
     
        if(!e.Authenticated)
        {
            // Use failureText where you need it        FailureText.Text=string.Format("<h1>{0}</h1>", failureText);
        }
        else
        {
            FailureText.Text=string.empty;
        }
       }

      Modified On Apr-07-2018 04:38:02 AM
  1. Jayden Bell

    Post:110

    Points:774
    Re: How to change Failure text for two different possibilities?

    Try this: 

    if (SQLDR.HasRow())
    {
        if(SQLDR.Read())
        {
            if(SQLDR["UEmail"].ToString() == Login1.UserName.ToString())
            {
                if(SQLDR["UPassword"].ToString() == Login1.Password.ToString())
                {
                    Session["userName"] = SQLDR["UEmail"].ToString();
                    e.Authenticated = true;
                }
                else
                {
                    FailureText.Text = "password is incorrect";
                    e.Authenticated = false;
                }
            }
        }
    }
    else
    {
        FailureText.Text = "User does not exists";
        e.Authenticated = false;
    }

      Modified On Apr-07-2018 04:36:51 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!