Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can I set a default value to zero if nothing is entered into the textbox?
Posted
Comments
CHill60 10-Mar-15 9:22am    
What have you tried?
What language?
Web or Windows?
What kind of textbox?
Member 10617989 10-Mar-15 9:33am    
textbox in vb2010
something like that
If TextBox1.Text = "" Then TextBox1.Text = 0 End If If TextBox2.Text = "" Then TextBox2.Text = 0 End If
CHill60 10-Mar-15 9:36am    
And you are saying your code doesn't work? What happens?
Use the Reply link so that I am notified when you respond
Member 10617989 10-Mar-15 9:44am    
yah the code is doesnt work!
CHill60 10-Mar-15 10:04am    
"yah the code is doesn't work!" does not explain "What happens"!! I was expecting a description of the error
In your code you are trying to set text to a numeric ... try If TextBox1.Text = "" Then TextBox1.Text = "0"

See: Int32.TryParse[^] method.

Example:
C#
int result = 0;

if (!Int32.TryParse(TextBox1.Text, result))
    //conversion failed, default value of result is equal to 0 (zero)
else
    //conversion OK, result is eqaul to the value entered into TextBox ;)
 
Share this answer
 
v2
As per the comments to the post, OP had written code
VB
If TextBox1.Text = "" Then
    TextBox1.Text = 0 
End If 
If TextBox2.Text = "" Then 
    TextBox2.Text = 0 
End If

I.e. they were assigning a numeric 0 to a string.
Changing the code to
VB
If TextBox1.Text = "" Then
    TextBox1.Text = "0" 
End If 
If TextBox2.Text = "" Then 
    TextBox2.Text = "0" 
End If
solved the problem
 
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