VB.NET textbox default value 0

Posted on 2011-10-26
Last Modified: 2012-05-12
How to make a textbox if empty to a value 0 (zero)
Question by:HKH1967
    LVL 33

    Expert Comment

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

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    To add to jppinto's example, you may consider trimming the value before you test its length--in case the user entered all spaces.
    LVL 83

    Accepted Solution

    If you are using this in a calculation then you may want to use the IIF statement

    Dim result As Integer = Integer.Parse(IIF(String.IsNullOrEmpty(txtA.Text), 0, txtA.Text)) + Integer.Parse(IIF(String.IsNullOrEmpty(txtB.Text), 0, txtB.Text))

    Author Comment


    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


    Expert Comment

    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

    LVL 74

    Expert Comment

    by:käµfm³d 👽

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


    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  = )

    Expert Comment

    i guess it should have been

       If TextBox1.Text.trim  = "" Then

    OR simply

       If TextBox1.Text = "" Then

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now