Omer Rehman

Omer Rehman

  • NA
  • 3
  • 1.7k

Getting text from a Textbox

Oct 9 2012 3:31 AM
Hi I am trying to get some text from a textbox using C# and ASP but it seems to come up as nothing everything. I am getting an Object reference not set errors in my cs file where i defined string st.... Any ideas?

my aspx code is:login.aspx

<asp:Login runat="server" ViewStateMode="Enabled" RenderOuterTable="false">
            <LayoutTemplate>
                <p class="validation-summary-errors">
                    <asp:Literal runat="server" ID="FailureText" />
                </p>
                <fieldset>
                    <legend>Log in Form</legend>
                    <ol>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
                            <asp:TextBox runat="server" ID="UserName" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                        </li>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
                            <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
                        </li>
                        <li>
                            <asp:CheckBox runat="server" ID="RememberMe" />
                            <asp:Label runat="server" AssociatedControlID="RememberMe" CssClass="checkbox">Remember me?</asp:Label>
                        </li>
                    </ol>
                    <asp:Button runat="server" CommandName="Login" Text="Log in" ID="Submit" OnClick="Unnamed6_Click" />
                </fieldset>
            </LayoutTemplate>
        </asp:Login>

My login.aspx.cs code is :

 protected void Unnamed6_Click(object sender, EventArgs e)
        {
           
            //TextBox User_N = (TextBox)this.FindControl("UserName");
            //TextBox User_P = (TextBox)this.FindControl("Password");

            OleDbConnection connect = new OleDbConnection();
            connect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Diary Model_CurrentBuild.mdb;Persist Security Info=False;";
            connect.Open();
            // string sqlstring = "Insert
            OleDbCommand command = new OleDbCommand();
            command.Connection = connect;
           // string a = Convert.ToString(this.FindControl("UserName"));
           
            TextBox box = (TextBox)this.FindControl("UserName");
            string st = box.Text;
            command.CommandText = "Select * from STAFF where staffName = '" + st + "' OR password = '" + this.FindControl("Password") + "'";
            OleDbDataReader reader = command.ExecuteReader();
           
            if (reader.Read())
            {
                Response.Redirect("../Default.aspx");
            }

           
            connect.Close();


        }



Answers (2)