Link to home
Start Free TrialLog in
Avatar of HKH1967
HKH1967Flag for Saudi Arabia

asked on

VB.NET textbox default value 0

How to make a textbox if empty to a value 0 (zero)
Avatar of jppinto
jppinto
Flag of Portugal image

If Len(txtName.Text) = 0 Then
    txtName.Text = "0"
End If

Avatar of kaufmed
To add to jppinto's example, you may consider trimming the value before you test its length--in case the user entered all spaces.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HKH1967

ASKER

ippinto:

i try it and it gives below error msg

Conversion from string "" to type 'Decimal' is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Conversion from string "" to type 'Decimal' is not valid.

Source Error:


Line 174:
Line 175:    Protected Sub txtDocAmount_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDocAmount.TextChanged
Line 176:        If CDec(txtAgentCommission.Text) = "" Then
Line 177:            txtAgentCommission.Text = "0"
Line 178:        End If
 

Source File: E:\Projects\NcbWork\Default.aspx.vb    Line: 176

Avatar of bilal_fazlani
bilal_fazlani

let me know if I am wrong..
u want to make the textbox text as "0" when it is empy, right ?

why don u try this , its simple.

 
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text = "".Trim Then

            TextBox1.Text = "0"

        End If
    End Sub

Open in new window

@bilal_fazlani

Is it not a bit redundant to trim an empty string literal  ; )

@HKH1967

Why are you using CDec? You are trying to compare a decimal to a string. That doesn't make sense. Take another look at what jppinto posted  = )
ohhhh...yeah
i guess it should have been

   If TextBox1.Text.trim  = "" Then

OR simply

   If TextBox1.Text = "" Then
Avatar of HKH1967

ASKER

thanks