1

I wonder if somebody else met the problem below with FreeTextBox like I did:

I have a FreeTextBox in my aspx page.

And the code behind to write the text in FreeTextBox (FTB) into Database:

    protected void btnWrite_Click(object sender, EventArgs e)
    {

        if(FTB.Text!="")
        {
             ...// insert FTB.Text into DataBase
             FTB.Text="";//clear the text in FTB
        }
       else LabelError.Text="Write something!";

    }

I got the problem when I do 3 steps:

  1. The first time, I let the FTB empty-> click button Write--> LabelError show: Write something! ----> the code works fine

  2. The 2nd time: I type: Hello World in FTB--> click button Write--> The FTB.Text's content is inserted into DataBase and the FTB.Text is cleared, then the Page Load again with empty FreeTextBox--> the code works fine too

  3. The 3rd time: I let the FTB empty ---> Click button Write --> the code jump into if command, The FTB.Text's content is inserted into DataBase, I check the Database, the new record is inserted with empty value ---> the code works wrong.

Try to debug in the 3rd option:

FTB.Text="";

I really dont know why even the FTB.Text="" , the code still jump inside if command and insert the FTB.Text="" into Database.

The important thing I want you all to know is: the code works fine in the first time (the FreeTextBox is empty), but it works wrong in the 3rd one (the FreeTextBox is empty after cleared by the 2nd one).

What wrong??? I wonder if there is some reason from my Chrome Browser,or Cache?

As recommended, I clear the Browser Cache, but I still got this problem.

Please help!!!

vyclarks
  • 854
  • 2
  • 15
  • 39
  • Have you tried debugging it by setting a breakpoint at the if statement, then running FTB.Text != "" in the immediate window? It's usually a good way to play around with boolean expressions. – Tobberoth Nov 06 '13 at 07:30

2 Answers2

5
 if(FTB.Text.ToString().Trim()!="")

or

if(!FTB.Text.ToString().Equals(""))
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • thank you, it work fine with all step. I wonder why if(FTB.Text!="") works wrong – vyclarks Nov 06 '13 at 07:40
  • we don't know that wether textbox contains Whitespaces are not even if it contains white spaces(generated by pressing Spacebar) they are not visible when we use trim those all spaces will be removed and works fine, because "" != " " – Sudhakar Tillapudi Nov 15 '13 at 10:54
1

Try to use string.IsNullOrEmpty

if(!string.IsNullOrEmpty(FTB.Text)
{
// insert value in DB
}
else
{
// show message
}

Or you can also use .Trim() as there may be some spaces

if(!string.IsNullOrEmpty(FTB.Text.Trim())
    {
    // insert value in DB
    }
    else
    {
    // show message
    }
nrsharma
  • 2,532
  • 3
  • 20
  • 36