Click here to Skip to main content
15,913,939 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a login page in ASP.NET, The login works for the users but I am trying to display an error message for Invalid User Name and Password. It seems to be not working. Can someone help me? What am I doing wrong?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
            con.Open();

            string cmdStr = "select count (*) from TableSecurity where EmailAddress='" + TextBoxEA.Text + "'";
            SqlCommand userExist = new SqlCommand(cmdStr, con);

            SqlCommand insertcmd = new SqlCommand("insert into TableSecurity", con);
            SqlDataAdapter dap = new SqlDataAdapter(insertcmd);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            con.Close();

            if (temp == 1)

                Response.Write(Label1.Text ="Invalid User Name and Password");
   
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con.Open();

        if (true)
        {
            SqlCommand level = new SqlCommand("select AccessLevel, Password from TableSecurity where EmailAddress = @EmailAddress AND Password = @Password", con);
            level.Parameters.Add(new SqlParameter("EmailAddress", TextBoxEA.Text));
            level.Parameters.Add(new SqlParameter("Password", TextBoxPW.Text));

            SqlDataReader reader = level.ExecuteReader();
            DataTable dt1 = new DataTable();
            dt1.Load(reader);

            foreach (DataRow dr1 in dt1.Rows)
            {
                int returnedLevel = Convert.ToInt32(dr1[0].ToString());

                if (returnedLevel == 1)
                {
                    Response.Redirect("FormAPublic.aspx");
                }
                else if (returnedLevel == 2)
                {
                    Response.Redirect("FormCPrivateNon.aspx");
                }
                else if (returnedLevel == 3)
                {
                    Response.Redirect("FormDPrivateFor.aspx");
                }
                else if (returnedLevel == 7)
                {
                    Response.Redirect("CEOPage.aspx");
                }
                else if (returnedLevel == 8)
                {
                    Response.Redirect("DBPage.aspx");
                }
            }
        }
        con.Close();
    }
    
}
Posted

1 solution

Response.Write(Label1.Text ="Invalid User Name and Password");

Why did you put Label1.Text inside the Response.Write()? That's the issue.
Don't use Response.Write to display messages. Use any Label for that.
C#
if (temp == 1){
                lblError.Text="Invalid User Name and Password";
}

I know you were out of coffee at that time.
 
Share this answer
 
Comments
Computer Wiz99 3-Oct-13 8:30am    
thatraja, Thanks for the info. I also think that the problem is the if (temp == 1). The reason being is that when I try to put in the wrong UN and PW the error message shows in both the Label1 and Response.Write but when you try to do it again it does not. I think my problem is in the insert command and the if (temp == 1). You can correct me if I am wrong.

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