Home > DeveloperSection > Forums > How to change Failure text for two different possibilities?
Brad Pitt
Brad Pitt

Total Post:61

Points:429
Posted on    December-08-2014 10:37 PM

 ASP.Net C# 
Ratings:


 2 Reply(s)
 664  View(s)
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.



Simons Hood
Simons Hood

Total Post:70

Points:494
Posted on    December-09-2014 6:10 AM

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;

    }

   }


Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    December-09-2014 7:15 AM

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 Mar-22-2016 01:38:08 AM

Don't want to miss updates? Please click the below button!

Follow MindStick