-2

Login error in asp.net like the name UserName does not exist in the current context and The name Password does not exist in the current context. Asp.code

 <asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt" Height="242px" Width="448px">
    <LayoutTemplate>
        <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
            <tr>
                <td>
                    <table cellpadding="0" style="height:242px;width:448px;">
                        <tr>
                            <td align="center" colspan="2" style="color:White;background-color:#6B696B;font-weight:bold;">Log In</td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" align="center">
                                <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
                            </td>
                        </tr>

                        <tr>
                            <td align="center" colspan="2">
                                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1" OnClick="LoginButton_Click" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </LayoutTemplate>
    <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
</asp:Login>

C# code

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Project_DBConnectionString"].ConnectionString)

    SqlCommand cmd = new SqlCommand("select * from signin where Username =@username and Password=@password", con);

    cmd.Parameters.AddWithValue("@username", UserName.Text);
    cmd.Parameters.AddWithValue("@password", Password.Text);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        Response.Redirect("Default.aspx");
    }
    else
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
    }
4
  • what are UserName and Password and where are they defined? Jul 19, 2014 at 10:00
  • 1
    Please do not write the whole question in Subject
    – Shaharyar
    Jul 19, 2014 at 10:00
  • Doesn't exists in the current context is the compile time error. It occurs when you an object or a variable which is out of scope.
    – Shaharyar
    Jul 19, 2014 at 10:07
  • I strongly suspect you are storing your passwords as a plain text. Never store passwords in plain text! Use SHA-512 hash. Jul 19, 2014 at 10:18

1 Answer 1

1

On the login control you need to use the FindControl to get this two TextBox and they are not direct available to the page.

So use this to get access to them

TextBox TheUserName = Login1.FindControl("UserName") as TextBox;
TextBox ThePassword = Login1.FindControl("Password") as TextBox;

and then use them on your code

cmd.Parameters.AddWithValue("@username", TheUserName.Text);
cmd.Parameters.AddWithValue("@password", ThePassword.Text);
1
  • @user3797140 So please check the answer as correct, and vote.
    – Aristos
    Jul 23, 2014 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.