3 Replies - 3158 Views - Last Post: 19 July 2010 - 06:56 AM Rate Topic: -----

#1 aklo  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 229
  • Joined: 23-January 09

txtbox.text.tostring() vs txtbox.tostring()

Posted 16 July 2010 - 07:38 AM

I have a weird problem with the textbox.

 String user1 = txtUsername.Text.ToString();
        String pw1 = txtPassword.Text.ToString();

        String StrConn = "Data Source=localhost;Initial Catalog=AppStore;Integrated Security=True";
        SqlConnection conn = new SqlConnection(StrConn);
        conn.Open();
        SqlCommand cmdLogin = new SqlCommand("SELECT * FROM Users WHERE username='"+  user1 +"' AND password='"+ pw1 +"'" , conn);
       
        SqlDataReader dr;
        dr = cmdLogin.ExecuteReader();

        if (dr.Read())
        {
            Label1.Text = "Success";
        }
        else
        {
            Label1.Text = "Failed";
        }
        dr.Close();
        conn.Close();



Notice the
txtUsername.text.toString() this works but if i change it to
txtUsername.toString() it doesn't work and login will always fail...I really do not know what is the difference between these 2 ...looks like the same to me.

Is This A Good Question/Topic? 0
  • +

Replies To: txtbox.text.tostring() vs txtbox.tostring()

#2 danielair  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 118
  • Joined: 12-July 09

Re: txtbox.text.tostring() vs txtbox.tostring()

Posted 16 July 2010 - 08:27 AM

When you use "txtUsername.text.toString()" it will take the text from within the text box and convert it into a sting type. But when you try to use "txtUsername.toString()" it will try to convert the actual textUsername textbox, which is an object, into a string which and be done and will cause an error.

Hope that helps,
Daniel
Was This Post Helpful? 3
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: txtbox.text.tostring() vs txtbox.tostring()

Posted 16 July 2010 - 10:16 AM

*
POPULAR

Just a note...the Text property of a TextBox(or any other control that has that property) is already a string. So there is no reason to do a ToString on a property that is already a string. it's redundant.
Was This Post Helpful? 5
  • +
  • -

#4 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: txtbox.text.tostring() vs txtbox.tostring()

Posted 19 July 2010 - 06:56 AM

View Postdanielair, on 16 July 2010 - 02:27 PM, said:

When you use "txtUsername.text.toString()" it will take the text from within the text box and convert it into a sting type. But when you try to use "txtUsername.toString()" it will try to convert the actual textUsername textbox, which is an object, into a string which and be done and will cause an error.

Hope that helps,
Daniel


Please note that txtUserName.ToString() will not cause an error. If you use the ToString method on the control it will return a string containing the name of the control. For example, txtUserName.ToString() will return "System.Web.UI.WebControls.TextBox". By default the ToString method for any control will return the name of the control as a string. It doesn't cause the error.

The error that you are getting (you failed to mention the error message/details so it's hard to tell what's wrong) probably occurs when you try to execute your SQL query with "System.Web.UI.WebControls.TextBox" as the parameter...

-Frinny

This post has been edited by Frinavale: 23 July 2010 - 11:47 AM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1