0

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: Error Do I have to add some response from SQL Server?

Mr. Munoz
  • 69
  • 1
  • 3
  • 13
  • Possible duplicate of [Why am I getting “Cannot Connect to Server - A network-related or instance-specific error”?](http://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci?answertab=votes#tab-top). – Win Apr 12 '17 at 20:55
  • I don't think it is a duplicate. The problem in my page is that does not send a: "The information entered is incorrect. Please check your Username and Password." message, but it redirects (pass login screen) if info is correct. – Mr. Munoz Apr 12 '17 at 20:58
  • According to the exception message, unable to connect to SQL server is the root cause of the problem. – Win Apr 12 '17 at 21:02
  • No, I just found out what was the problem. In the properties of the Log In button there was this: CommandName="Login" - I used another button and copied some of the properties from the original, tried to log in and it worked. – Mr. Munoz Apr 12 '17 at 21:17

1 Answers1

0

The solution was easy but to find out what caused it was a little time consuming. The properties in the original button from the Login Control was like this:

<asp:Button ID="btnLogin" runat="server" BackColor="#CCCCCC" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" Font-Names="Arial" Font-Size="Small" ForeColor="#333333" Text="Log In" ValidationGroup="Login1" OnClick="btnLogin" CommandName="Login"

I tried copying the same function that logs in the user into another button without those properties and it worked, sending the failure message to the label. So, I omitted this property from the original button and it worked: CommandName="Login"

Mr. Munoz
  • 69
  • 1
  • 3
  • 13