I have Two Text Boxes where the user enters their name. I need to validate that the boxes are not left empty and also no numeric data is entered. If it either is entered the click event Cost will determine invalid values and display message box error. Obviously my problem is in red. I keep getting an “Invalid Cast Exception Conversion to string dd to type long is not valid” Also; I know the syntax to test for Numeric data…but how do you test for and empty textbox (string) and to make sure it is alphabetical data? And are my code tags correct?

Public Class Form1



    Private Sub btnCalcCost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcCost.Click
        Dim numMonths As Integer
        Dim decTotalCost As Decimal
        Dim memCost As Integer
        Dim singleMem As Decimal = 38D
        Dim FamilyMem As Decimal = 58D
        Dim seniorMem As Decimal = 27D



        If IsNumeric(Me.txtNumberOfMon.Text) Then numMonths = Convert.ToInt16(Me.txtNumberOfMon.Text)

If IsNumeric(Me.txtFirstName.Text Or Me.txtLastName.Text) Then

        ElseIf (strLastName Or strFirstName) = "" Then
            MessageBox.Show("Error")        

If numMonths > 0 Then
            If Me.radSingleMem.Checked Then
                memCost = singleMem
            ElseIf Me.radFamilyMem.Checked Then
                memCost = FamilyMem
            ElseIf Me.radSeniorMem.Checked Then
                memCost = seniorMem
            End If


            decTotalCost = numMonths * memCost
            Me.lblTotalCost.Text = decTotalCost.ToString("c")
        Else

            MessageBox.Show("You entered non-numeric number or " _
            & numMonths.ToString() & _
       ". Enter number greater than zero.")
            Me.txtNumberOfMon.Text = ""
            Me.txtNumberOfMon.Focus()
        End If

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.txtNumberOfMon.Clear()
        Me.txtFirstName.Clear()
        Me.txtLastName.Clear()
        Me.lblTotalCost.Text = ""
        Me.radSingleMem.Checked = False
        Me.radFamilyMem.Checked = False
        Me.radSeniorMem.Checked = False
    End Sub
End Class

i was modified your code, i add two event handling in keypress event :

Public Class Form1
'This code not allowed you entered numeric input
Private Sub txtFirstName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFirstName.KeyPress
        If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
              Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
              And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
              Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
            'space accepted
            If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
                e.Handled = True
            End If
        End If
        If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) _
       Or (Microsoft.VisualBasic.Asc(e.KeyChar) = 44) _
       Or (Microsoft.VisualBasic.Asc(e.KeyChar) = 45) _
       Or (Microsoft.VisualBasic.Asc(e.KeyChar) = 46) Then
            e.Handled = False
        End If
    End Sub
	
'This code not allowed you entered numeric input
Private Sub txtLastName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLastName.KeyPress
        If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
              Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
              And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
              Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
            'space accepted
            If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
                e.Handled = True
            End If
        End If
        If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) _
       Or (Microsoft.VisualBasic.Asc(e.KeyChar) = 44) _
       Or (Microsoft.VisualBasic.Asc(e.KeyChar) = 45) _
       Or (Microsoft.VisualBasic.Asc(e.KeyChar) = 46) Then
            e.Handled = False
        End If
    End Sub
	
Private Sub btnCalcCost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcCost.Click
        Dim numMonths As Integer
        Dim decTotalCost As Decimal
        Dim memCost As Integer
        Dim singleMem As Decimal = 38D
        Dim FamilyMem As Decimal = 58D
        Dim seniorMem As Decimal = 27D



        If IsNumeric(Me.txtNumberOfMon.Text) Then numMonths = Convert.ToInt16(Me.txtNumberOfMon.Text)

		
		If txtfirstName.text = "" Or txtLastName.text = "" Then
            MessageBox.Show("Error")        
		else
			If numMonths > 0 Then
				If Me.radSingleMem.Checked Then
					memCost = singleMem
				ElseIf Me.radFamilyMem.Checked Then
					memCost = FamilyMem
				ElseIf Me.radSeniorMem.Checked Then
					memCost = seniorMem
				End If
			decTotalCost = numMonths * memCost
            Me.lblTotalCost.Text = decTotalCost.ToString("c")
			Else
				MessageBox.Show("You entered non-numeric number or " _
				& numMonths.ToString() & _
				". Enter number greater than zero.")
				Me.txtNumberOfMon.Text = ""
				Me.txtNumberOfMon.Focus()
			End If
		End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.txtNumberOfMon.Clear()
        Me.txtFirstName.Clear()
        Me.txtLastName.Clear()
        Me.lblTotalCost.Text = ""
        Me.radSingleMem.Checked = False
        Me.radFamilyMem.Checked = False
        Me.radSeniorMem.Checked = False
    End Sub
End Class
Comments
We need more programmers Jx_man. Always willing to help someone that is eager to learn.
If IsNumeric(Me.txtFirstName.Text Or Me.txtLastName.Text) Then

        ElseIf (strLastName Or strFirstName) = "" Then
            MessageBox.Show("Error")

should be:

If IsNumeric(Me.txtFirstName.Text) Or IsNumeric(Me.txtLastName.Text) Or strLastName = "" Or strFirstName = "" Then
                   MessageBox.Show("Error")
 End If
This article has been dead for over six months. Start a new discussion instead.