0

I am new to VB.net and I want to check a text box is not null or empty I have used so many ways to find out But none helped me out These are the methods I have used

If textbox1.text.trim.Length >0 then
My sql insert query
End if

If textbox1.text<>"" then
End if

Could you help me out where I am going wrong

Thomas G
  • 9,886
  • 7
  • 28
  • 41
user9396509
  • 15
  • 1
  • 2
  • 2
    I would normally use `If Not String.IsNullOrEmpty(textbox1.Text) Then` - In what ways did your previous efforts fail? – Ste Griffiths May 15 '18 at 10:52
  • @SteGriffiths thank you it worked. I tried the code which I have mentioned in the question but it keep on putting the null values in database . Your code helped me out . – user9396509 May 15 '18 at 10:59

2 Answers2

1

Since you are using Trim - for removing white spaces - there is another method which will fit your requirements:

String.IsNullOrWhiteSpace Method (String)

Indicates whether a specified string is null, empty, or consists only of white-space characters

If String.IsNullOrWhiteSpace(textbox1.Text) = False Then
    ' your MySql query
End

Actually both of your approaches should work too.
In Winforms textBox.Text will never be null, so checking for .Length after trimming should work.

Fabio
  • 31,528
  • 4
  • 33
  • 72
0

You can use both of them:

1 :- String.IsNullOrEmpty(yourString);

1 :- sNullOrWhiteSpace(yourString);

For more details visit the LINK

Faraz Babakhel
  • 654
  • 5
  • 14