Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
if text box value not given defaultly take value = "0"

not converted date to "0"

C#
if (txtDate.Text.ToString() == "")
               txtDate.Text. = "0";
Posted
Updated 14-May-12 0:45am
v4
Comments
Sergey Alexandrovich Kryukov 14-May-12 1:47am    
Makes no sense. And what, too lazy to check up syntax? With documentation or using simple logical thinking?
--SA

No. A compiler will tell you what's wrong. Must be if (someCondition) {/* ... */} else {/* ... */}. Not a mistake in syntax, but not good style: don't use "", use string.Empty.

—SA
 
Share this answer
 
v2
Comments
Mohammad A Rahman 14-May-12 21:17pm    
Nice
Sergey Alexandrovich Kryukov 14-May-12 22:55pm    
Thank you, Mohammad.
--SA
VJ Reddy 15-May-12 4:54am    
Good points. 5!
Sergey Alexandrovich Kryukov 15-May-12 11:48am    
Thank you, VJ.
--SA
I think the string.IsNullOrWhiteSpace method can be used with .Net 4 or above and string.IsNullOrEmpty method can be used with .Net 2.0 or 3.5.

C#
//For .Net 4 or above
if (string.IsNullOrWhiteSpace(txtDate.Text))
    txtDate.Text = "0";

//For .Net 2.0, 3.5
if (string.IsNullOrEmpty(txtDate.Text.Trim()))
    txtDate.Text = "0";


It is preferable to use string.IsNull... method as it will not throw error when the text to be tested is null. On the other hand text == "" or text == string.Empty throws error if text is null.
 
Share this answer
 
v2
Comments
2011999 14-May-12 13:43pm    
not working above code
VJ Reddy 14-May-12 14:38pm    
It shall work. Please see here
http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx and here
http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx
Mohammad A Rahman 14-May-12 21:17pm    
To the point :)
101
VJ Reddy 14-May-12 21:40pm    
Thank you, Rahman :)
Sandeep Mewara 15-May-12 10:34am    
5!
Hi,


C#
if(txtOSIInterstFromBank.Text==String.Empty)
{
        //Do What you want to do here....
}
 
Share this answer
 
v2
Comments
2011999 14-May-12 6:43am    
if (txtDate.Text.ToString() == "")
txtDate.Text. = "0";

is it not converted to '0'
bbirajdar 14-May-12 12:56pm    
Will this work , if a user enters a 'space' in the textbox?
not given defaultly means the value is empty then:

C#
if (txtOSIInterstFromBank.Text.Trim() == "")
          txtOSIInterstFromBank.Text = "0";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900