I need some help figuring out the reason my login form is not working properly. When the user enters the correct credentials, it will redirect, but when the information is incorrect, instead of sending a failure message to the label [lbFailureText], the page stays loading until it sends the error message on the browser (picture attached). I am using Login Control, it seems to be it is trying to redirect somewhere when the information is not correct instead of sending a message to the label.
Here is the code c# behind:
protected void btnLogin(object sender, EventArgs e)
{
string username = ((TextBox)Login1.FindControl("UserName")).Text.Trim();
string password = ((TextBox)Login1.FindControl("Password")).Text.Trim();
Label failureText = ((Label)Login1.FindControl("lbFailureText"));
failureText.Text = "";
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["someConnectionString"].ToString()))
{
SqlCommand cmd = new SqlCommand("UserAuthentication", conn);
cmd.CommandType = CommandType.StoredProcedure;
// add parameters and set their values
cmd.Parameters.AddWithValue("@Username", username);
cmd.Parameters.AddWithValue("@Password", password);
// open connection
conn.Open();
using (SqlDataReader dataReader = cmd.ExecuteReader())
{
if (dataReader.Read())
{
Session["Username"] = username;
Session["Password"] = password;
Response.Redirect("Tests.aspx");
}
else
if (!dataReader.Read())
{
failureText.Text = "The information entered is incorrect. Please check your Username and Password.";
}
dataReader.Close();
}
conn.Close();
Here the aspx code:
<asp:Login ID="Login1" runat="server" ViewStateMode="Enabled" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt">
<LayoutTemplate>
<table cellpadding="4" cellspacing="0" style="border-collapse:collapse;">
<tr>
<td>
<table cellpadding="0">
<tr>
<td class="auto-style1" colspan="2" style="color:White;background-color:#333333; font-weight:bold;">Log In</td>
</tr>
<tr>
<td align="right" class="auto-style5">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName" AutoCompleteType="Disabled" Wrap="False" CssClass="auto-style2">Username:</asp:Label>
</td>
<td class="auto-style4">
<asp:TextBox ID="UserName" runat="server" BorderStyle="Dotted" Font-Size="Medium" Width="186px"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="Username is required." ToolTip="Username is required." ValidationGroup="Login1" ForeColor="#FF3300">Username is required.</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" class="auto-style5">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" CssClass="auto-style2">Password:</asp:Label>
</td>
<td>
<asp:TextBox ID="Password" runat="server" BorderStyle="Dotted" Font-Size="Medium" TextMode="Password" Width="186px"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1" ForeColor="#FF3300">Password is required.</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" class="auto-style3">
<asp:Button ID="btnLogin" runat="server" BackColor="#CCCCCC" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" CommandName="Login" Font-Names="Arial" Font-Size="Small" ForeColor="#333333" Text="Log In" ValidationGroup="Login1" OnClick="btnLogin" />
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color:Red;">
<asp:Label ID="lbFailureText" runat="server"></asp:Label>
<br />
<%--<asp:Label ID="lbFailureMessage" runat="server"></asp:Label>--%>
</td>
</tr>
<tr>
<td class="auto-style7" colspan="2">Click here for technical support</td>
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<TextBoxStyle BorderStyle="Dotted" />
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
</asp:Login>
Here the picture from the browser:
Do I have to add some response from SQL Server?