3 Replies - 9833 Views - Last Post: 05 January 2008 - 10:43 AM Rate Topic: -----

#1 rjbzzr400  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-December 07

Empty textbox check doesn't work

Post icon  Posted 05 January 2008 - 06:49 AM

Hi, I'm trying to do an empty textbox check. Everytime the button is pressed with an empty text box is crashes and
highlights this part in yellow:

enteredMoney = CDbl(txtMoney.Text)

and says:

Conversion from string "" to type 'Double' is not valid.

I've tried "if txtmoney.text = "" then" and it does the samething

Anyhelp will be greatly appreciated :)

Private enteredMoney As Double		
enteredMoney = CDbl(txtMoney.Text)

 If pinCode = ("1111") Then
			If Len(Trim(txtMoney.Text)) = 0 Then '
			MessageBox.Show("Please enter an amount", "Error")
			End If
			currentBalance1a = currentBalance1 + overDraft1  '
			If enteredMoney > currentBalance1a Then   
				MessageBox.Show("Insufficient funds available", "Warning")
			Else
				currentBalance1a = currentBalance1a - enteredMoney
				currentBalance1 = currentBalance1a - overDraft1		 amount.
				txtBalance.Text = currentBalance1.ToString("##,#00.00")
			End If




Is This A Good Question/Topic? 0
  • +

Replies To: Empty textbox check doesn't work

#2 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Re: Empty textbox check doesn't work

Posted 05 January 2008 - 10:14 AM

Hi rjbzzr400

Im Not sure where you are checking for nothing entered into your textbox. The first thing you need to do is to check that data has been entered before attemting any calculations.
See code below

Public Class Form1
	Private enteredMoney As Double
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		If String.IsNullOrEmpty(txtmoney.Text) Then
			MessageBox.Show("Insufficient funds available", "Warning")
			txtmoney.Focus()
			Return
		End If

		enteredMoney = CDbl(txtmoney.Text)
		TextBox1.Text = enteredMoney

	End Sub
End Class



You can also consider using the try catch method for trapping errors

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Empty textbox check doesn't work

Posted 05 January 2008 - 10:28 AM

Hi there
If you encountered this with something in the txtMoney.
The highlighted line has no errors, but I think you may entered an illegal value(maybe value starts with a none numerical character such as $...)
Try getting its value before you convert it to double
enteredMoney = CDbl(val(txtMoney.Text))


Now if you got enteredMoney = 0 and it's not what you have in txtMoney.Text, that's mean you have to review the part that set txtMoney value
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Empty textbox check doesn't work

Posted 05 January 2008 - 10:43 AM

Try this and see if it solves any of your problems:

Private enteredMoney As Double
enteredMoney = IIf(String.IsNullOrEmpty(txtMoney.Text),enteredMoney = 0D,enteredMoney = CDbl(txtMoney.Text))
	Public Sub Calc()
		If pinCode = ("1111") Then
			If String.IsNullOrEmpty(txtMoney.Text) Then '
				MessageBox.Show("Please enter an amount", "Error")
			End If
			currentBalance1a = currentBalance1 + overDraft1  '
			If enteredMoney > currentBalance1a Then
				MessageBox.Show("Insufficient funds available", "Warning")
			Else
				currentBalance1a = currentBalance1a - enteredMoney
			currentBalance1 = currentBalance1a - overDraft1		 amount.
				txtBalance.Text = currentBalance1.ToString("##,#00.00")
			End If
		End If
	End Sub



Notice I made use of the IIf function, this should help alleviate some of your problems. Hope this helps a little
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1