13 Replies - 158184 Views - Last Post: 28 October 2011 - 05:44 PM Rate Topic: -----

#1 Basem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-November 07

how to check if a textbox has a null value?

Post icon  Posted 28 November 2007 - 10:22 PM

[if me.textbox1.text= system.dbnull then
msgbox("please inter a value")]

it says system.dbnull can't be used as an expression so how can i check the if the textbox has a null value
Is This A Good Question/Topic? 0
  • +

#26 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: how to check if a textbox has a null value?

Posted 28 November 2007 - 10:45 PM

Thats because the System.DBNull cannot be used as an expression, considering its a class. There are a couple of ways you can check for an empty value in a TextBox, so lets list themNow lets look at some examples:

String.IsNullOrEmpty

If String.IsNullOrEmpty(TextBox1.Text) Then
	  'Show your message here
Else
	  'Show your message here
End If



String.Empty

If TextBox1.Text = String.Empty  Then
	  'Show your message here
Else
	  'Show your message here
End If



Or ole trusty:

If TextBox1.Text = ""  Then
	  'Show your message here
Else
	  'Show your message here
End If




Hope that helps some :)

EDIT: Be careful if using If TextBox1.Text = "" because the key combination of Ctrl+K will enter a NULL value into a TextBox, thus that check will always fail.

This post has been edited by PsychoCoder: 28 November 2007 - 10:58 PM

Was This Post Helpful? 1

#27 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: how to check if a textbox has a null value?

Posted 29 November 2007 - 04:43 AM

if textbox.text = "" then
else
endif
Was This Post Helpful? 0
  • +
  • -

#28 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: how to check if a textbox has a null value?

Posted 29 November 2007 - 05:57 AM

View PostBasem, on 29 Nov, 2007 - 12:22 AM, said:

[if me.textbox1.text= system.dbnull then
msgbox("please inter a value")]


The text property of the textbox class is a string. It wont even accept System.DBNull.Value as a value. If you set it equal to some other object, it will be cast as string.

PsychoCoder's Ctrl+K thing didn't work for me. I don't believe I can make the property null. Still, it never hurts to try, so I wrote some test code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Dim msg As String = "String.IsNullOrEmpty: " & String.IsNullOrEmpty(TextBox1.Text)
	msg &= ControlChars.NewLine & "TextBox1.Text = String.Empty: " & (TextBox1.Text = String.Empty)
	msg &= ControlChars.NewLine & "TextBox1.Text = """": " & (TextBox1.Text = "")
	MessageBox.Show(msg)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
	'this does not work!
	'TextBox1.Text = System.DBNull.Value
End Sub



Based on this, TextBox1.Text = "" is just as viable as anything else, I couldn't find a way to trick it.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#29 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: how to check if a textbox has a null value?

Posted 29 November 2007 - 06:11 AM

View Postbaavgai, on 29 Nov, 2007 - 04:57 AM, said:

PsychoCoder's Ctrl+K thing didn't work for me. I don't believe I can make the property null. Still, it never hurts to try, so I wrote some test code.


There is actually a key combination that will enter a null value into a TextBox (not I got to find it, it might actually be Ctrl+0 (zero) so honestly, and from a professional standpoint I (remember this is personal preference) wouldn't pass a Code review for a developer if he used If TextBox1.Text = "" as his check, simply because there are key combinations that can break this, and thats not good coding standards.

When working in 2.0 we have made String.IsNullOrEmpty the decacto standard at work, yes I know that means we still have 1.1 stuff but it is slowly being converted lol.
Was This Post Helpful? 0
  • +
  • -

#30 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: how to check if a textbox has a null value?

Posted 29 November 2007 - 06:44 AM

Actually, I hadn't known about String.IsNullOrEmpty before, so thanks. I like the idea of it and it saves an "and".

I still don't see how the Text property can be anything other than String. If you should find otherwise, I'd be curious.
Was This Post Helpful? 0
  • +
  • -

#31 kaymaf  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 03-October 07

Re: how to check if a textbox has a null value?

Posted 01 December 2007 - 04:06 PM

just use
if textbox1.textLenght=0 then
end if
Was This Post Helpful? 0
  • +
  • -

#32 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: how to check if a textbox has a null value?

Posted 01 December 2007 - 07:04 PM

View PostPsychoCoder, on 29 Nov, 2007 - 07:11 AM, said:

There is actually a key combination that will enter a null value into a TextBox (not I got to find it, it might actually be Ctrl+0 (zero) so honestly, and from a professional standpoint I (remember this is personal preference) wouldn't pass a Code review for a developer if he used If TextBox1.Text = "" as his check, simply because there are key combinations that can break this, and thats not good coding standards.


Sorry, im 16 and just started real programming for the first time. I didn't know that they made something that appears null, but that sounds really dumb to me. Thx for the tip tho :D
Was This Post Helpful? 0
  • +
  • -

#33 Zaphod0414  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 26-October 11

Re: how to check if a textbox has a null value?

Posted 27 October 2011 - 12:01 AM

Firstly, thanks for the heads up on the String.IsNullorEmpty() method - it works like a charm. There is, however, one method that's even better for checking text box input:

If String.IsNullorWhitespace(txtBoxName.text) Then
     'run this code
Else
     'run this code instead
End If



This will return true and run the proceeding code if the value of txtBoxName.text is empty, null, or contains only whitespace, and will return false otherwise. If you're using this as a break out trap you can omit the else portion entirely, as you want to pop an error message and stop (exit sub) if the check returns true (ie the text box is empty or contains only invalid data), and you just want to ignore the code in the if statement and continue executing if the check returns false (ie the text box is NOT empty or invalid). You can even go the extra mile and add an or condition to check for number-only entries. That whole sandwich would look a little like this:

If String.IsNullorWhitespace(txtBoxName.text) Or IsNumeric(txtBoxName.text) Then
     MsgBox("Error")
     End Sub
End If



Cheers,
Z
Was This Post Helpful? 0
  • +
  • -

#34 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: how to check if a textbox has a null value?

Posted 27 October 2011 - 01:04 PM

View PostZaphod0414, on 27 October 2011 - 05:01 PM, said:

Firstly, thanks for the heads up on the String.IsNullorEmpty() method - it works like a charm. There is, however, one method that's even better for checking text box input:

If String.IsNullorWhitespace(txtBoxName.text) Then
     'run this code
Else
     'run this code instead
End If



This will return true and run the proceeding code if the value of txtBoxName.text is empty, null, or contains only whitespace, and will return false otherwise. If you're using this as a break out trap you can omit the else portion entirely, as you want to pop an error message and stop (exit sub) if the check returns true (ie the text box is empty or contains only invalid data), and you just want to ignore the code in the if statement and continue executing if the check returns false (ie the text box is NOT empty or invalid). You can even go the extra mile and add an or condition to check for number-only entries. That whole sandwich would look a little like this:

If String.IsNullorWhitespace(txtBoxName.text) Or IsNumeric(txtBoxName.text) Then
     MsgBox("Error")so
     End Sub
End If



Cheers,
Z

Or try this:
if TextBox1.Text = String.Empty then
'...
End If


J-Bo
Was This Post Helpful? 0
  • +
  • -

#35 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: how to check if a textbox has a null value?

Posted 27 October 2011 - 01:24 PM

View PostZaphod0414, on 27 October 2011 - 07:01 AM, said:

Firstly, thanks for the heads up on the String.IsNullorEmpty() method - it works like a charm. There is, however, one method that's even better for checking text box input:

If String.IsNullorWhitespace(txtBoxName.text) Then
     'run this code
Else
     'run this code instead
End If



Yes, that is a good method. However, that method unfortunately didn't exist back in 2007 ;) It was a .NET 4.0 addition... Please try to avoid bringing 4 year old threads back to life in the future :) But yes, it is a good point :)

This post has been edited by CodingSup3rnatur@l-360: 27 October 2011 - 01:36 PM

Was This Post Helpful? 0
  • +
  • -

#36 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: how to check if a textbox has a null value?

Posted 27 October 2011 - 07:28 PM

View PostCodingSup3rnatur@l-360, on 28 October 2011 - 06:24 AM, said:

View PostZaphod0414, on 27 October 2011 - 07:01 AM, said:

Firstly, thanks for the heads up on the String.IsNullorEmpty() method - it works like a charm. There is, however, one method that's even better for checking text box input:

If String.IsNullorWhitespace(txtBoxName.text) Then
     'run this code
Else
     'run this code instead
End If



Yes, that is a good method. However, that method unfortunately didn't exist back in 2007 ;) It was a .NET 4.0 addition... Please try to avoid bringing 4 year old threads back to life in the future :) But yes, it is a good point :)

I didn't. Some guy (the one who posted before me) did.
What about this*:
If TextBox1.Text = Nothing Then
'Do What You Want
End If



Ohh, you weren't talking to me were you.
Was This Post Helpful? 0
  • +
  • -

#37 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: how to check if a textbox has a null value?

Posted 28 October 2011 - 12:01 AM

Take what CodingSup3rnatur@l-360 said as a formal announcement :)

END OF THIS DISCUSSION, dont necro 4 years old post
___________________________________________________
Was This Post Helpful? 0
  • +
  • -

#38 Zaphod0414  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 26-October 11

Re: how to check if a textbox has a null value?

Posted 28 October 2011 - 05:44 PM

The thread is old, yes, but hardly dead. It is still seen by many, as it ranks very prominently in google's results. I was drawn here quickly when searching for this answer, so I see no harm in updating the answer so that others may benefit from it as I have. I prefer to put information where it can be easily found, otherwise what's the point?

Z
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1