Click here to Skip to main content
15,895,817 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hello,

My code behind code is unable to read textbox ids name on the aspz page (error - The name 'txtPWD' does not exist in the current context):
ASP.NET
<asp:UpdatePanel ID="TestLogin" runat="server">
        <ContentTemplate>
            <asp:Button ID="btnLogin" runat="server" Text="Login" 
                onclick="btnLgin_Click" />
            <asp:Panel ID="LoginPopup" runat="server" Style="display: none;">
                <asp:Login ID="Login2" runat="server" RememberMeText="Remember me." BackColor="White">
                    <LayoutTemplate>
                        <table style="border-collapse: collapse;" border="0" cellpadding="1" cellspacing="0">
                            <tbody>
                                <tr>
                                    <td>
                                        <table border="0" cellpadding="0">
                                            <tbody>
                                                <tr>
                                                    <td colspan="2" align="center">
                                                        You are not logged in.</td>
                                                </tr>
                                                <tr>
                                                    <td align="right">
                                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">UserName:</asp:Label>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                                            ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="ctl07$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="txtPWD" runat="server" TextMode="Password"></asp:TextBox>
                                                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                                            ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="ctl07$Login1">*</asp:RequiredFieldValidator>
                                                    </td>
                                                </tr>
                                                <tr>


code behind:
C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName =@username and Password=@password", con);
        cmd.Parameters.AddWithValue("@username", txtUserName.Text);
        cmd.Parameters.AddWithValue("@password", txtPWD.Text);


Many thanks
Posted
Comments
Richard C Bishop 3-Feb-14 11:01am    
Did your designer page get altered some how? You may just need to recreate the controls.
miss786 3-Feb-14 11:51am    
Thank you so much for your help.

1 solution

Update your web form designer.aspx.cs file.

How can you update designer.aspx.cs:

1. Remove (Cut) control from page.
2. Paste it again on same position.
3. check designer.cs file where instance has been created like:

C#
/// <summary>
        /// txtPWD control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox txtPWD;
 
Share this answer
 
v2
Comments
miss786 3-Feb-14 11:51am    
Thank you, I manage to get to it work using your suggestion.
Many thanks
Sandeep Singh Shekhawat 3-Feb-14 21:05pm    
Welcome to You!

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