how to check if a textbox has a null value?
Page 1 of 113 Replies - 114050 Views - Last Post: 28 October 2011 - 05:44 PM
#1
how to check if a textbox has a null value?
Posted 28 November 2007 - 10:22 PM
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
#26
Re: how to check if a textbox has a null value?
Posted 28 November 2007 - 10:45 PM
- String.IsNullOrEmpty Method: This is actually one of my favorite methods when programming in .Net 2.0 (not available in 1.1)
- String.Empty
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
#27
Re: how to check if a textbox has a null value?
Posted 29 November 2007 - 04:43 AM
else
endif
#28
Re: how to check if a textbox has a null value?
Posted 29 November 2007 - 05:57 AM
Basem, on 29 Nov, 2007 - 12:22 AM, said:
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.
#29
Re: how to check if a textbox has a null value?
Posted 29 November 2007 - 06:11 AM
baavgai, on 29 Nov, 2007 - 04:57 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.
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.
#30
Re: how to check if a textbox has a null value?
Posted 29 November 2007 - 06:44 AM
I still don't see how the Text property can be anything other than String. If you should find otherwise, I'd be curious.
#31
Re: how to check if a textbox has a null value?
Posted 01 December 2007 - 04:06 PM
if textbox1.textLenght=0 then
end if
#32
Re: how to check if a textbox has a null value?
Posted 01 December 2007 - 07:04 PM
PsychoCoder, on 29 Nov, 2007 - 07:11 AM, said:
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
#33
Re: how to check if a textbox has a null value?
Posted 27 October 2011 - 12:01 AM
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
#34
Re: how to check if a textbox has a null value?
Posted 27 October 2011 - 01:04 PM
Zaphod0414, on 27 October 2011 - 05:01 PM, said:
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
#35
Re: how to check if a textbox has a null value?
Posted 27 October 2011 - 01:24 PM
Zaphod0414, on 27 October 2011 - 07:01 AM, said:
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
This post has been edited by CodingSup3rnatur@l-360: 27 October 2011 - 01:36 PM
#36
Re: how to check if a textbox has a null value?
Posted 27 October 2011 - 07:28 PM
CodingSup3rnatur@l-360, on 28 October 2011 - 06:24 AM, said:
Zaphod0414, on 27 October 2011 - 07:01 AM, said:
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
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.
#37
Re: how to check if a textbox has a null value?
Posted 28 October 2011 - 12:01 AM
END OF THIS DISCUSSION, dont necro 4 years old post
___________________________________________________
#38
Re: how to check if a textbox has a null value?
Posted 28 October 2011 - 05:44 PM
Z
|
|

New Topic/Question
Reply




MultiQuote





|