Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a login error message that is not showing up. If you enter the wrong username and password the form just clears out password textbox and will not show the error message. Is there something i forgot to do or am I missing something?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
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)
    {
       

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

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



        if (true)
        {
            SqlCommand level = new SqlCommand("select INST_ID, 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");
                }
                else  
                {
                    Label1.Text = "Invalid UserName and Password!!!";
                }
                con.Close();
            }
        }
    }
}
Posted
Updated 7-Oct-13 2:58am
v2
Comments
ridoy 4-Oct-13 16:23pm    
if you have those static values and condition for 1,2,3,7,8 then why use 0 for invalidity.Just use else instead of else if, that should work.
Computer Wiz99 7-Oct-13 8:57am    
Sorry for the late response. Had computer problem. Ok. I did what you said and the error message still does not show up when putting in the wrong username and password. I have updated the code.
Can you debug and see what is the value of returnedLevel?

1 solution

The column index you have specified for the Datarow seems to be wrong.

int returnedLevel = Convert.ToInt32(dr1[0].ToString());


Change the index to 1 instead of 0 // 0 as per the select query is for INST_ID

Also,use else instead of elseif just to check whether the it shows the error incase 1,2,3,7,8 not returned

Please check and revert . Hope this helps...
 
Share this answer
 

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