3 Replies - 30766 Views - Last Post: 25 August 2009 - 10:53 AM Rate Topic: -----

#1 stoke1863  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-January 09

Conversion from string "" to type 'Double' is not va

Post icon  Posted 03 January 2009 - 11:32 AM

Hi there my program is a simple adding calculation.

I have some products that when ticked ask for a input box that ask for the quanity of the type needed.

The user then selects the calculate button to calculate the price of the selections

My program works fine when every item is selected (via tick boxes)

but crashes with the error "Conversion from string "" to type 'Double' is not valid. " when a selection is not ticked here is my code

i have been told that my conversion procedure is outdated but this is what my lecturer has taught us and his marking requirments need to keep using this old method

Public Class Form1 
 
	
	Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged 
		'declare variables 
 
		Dim network As Double 
 
		Dim network_num As Double 
 
		Dim network_total As Double 
 
		'Make Text boxes Visable when ticked 
 
		textbox_network_no.Visible = True 
 
		TextBox_netcost.Visible = True 
 
		TextBox_networksub.Visible = True 
 
		'Error trapping 
 
		Try 
 
			'Inout box displayed to ask how much network cards are needed 
 
			network = InputBox("How Many 100Mbit Network Cards Would You Like?") 
 
		Catch ex As Exception 
 
			'simple message 
 
			MsgBox("sorry could not accept the input") 
 
			'repeat error message 
 
			network = InputBox("How Many 100Mbit Network Cards Would You Like?") 
 
		End Try 
 
		'set network_num to 35 
 
		network_num = CDbl(35) 
 
		'display network num in textbox 
 
		TextBox_netcost.Text = CDbl(network_num) 
 
		'display ammount ordered in textbox 
 
		textbox_network_no.Text = CDbl(network) 
 
		'calculation for network_total 
 
		network_total = network * network_num 
 
		TextBox_networksub.Text = CDbl(network_total) 
 
	End Sub 
 
	Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged 
 
		Dim hdd As Double 
 
		Dim hdd_num As Double 
 
		Dim hdd_total As Double 
 
		'Make Text boxes Visable when ticked 
 
		textbox_hdd_no.Visible = True 
 
		TextBox_hddcost.Visible = True 
 
		TextBox_hddsub.Visible = True 
 
		'Error trapping 
 
		Try 
 
			'Inout box displayed to ask how much network cards are needed 
 
			hdd = InputBox("How Many Hard Drives Would You Like?") 
 
		Catch ex As Exception 
 
			'simple message 
 
			MsgBox("sorry could not accept the input") 
 
			'repeat error message 
 
			hdd = InputBox("How Many Hard Drives Would You Like?") 
 
		End Try 
 
		'set hdd_num to 120 
 
		hdd_num = CDbl(120) 
 
		'display hdd num in textbox 
 
		TextBox_hddcost.Text = CDbl(hdd_num) 
 
		'display ammount ordered in textbox 
 
		textbox_hdd_no.Text = CDbl(hdd) 
 
		'calculation for hdd_total 
 
		hdd_total = hdd * hdd_num 
 
		TextBox_hddsub.Text = CDbl(hdd_total) 
	End Sub 
 
	Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged 
 
		Dim MOT As Double 
 
		Dim MOT_num As Double 
 
		Dim MOT_total As Double 
 
		'Make Text boxes Visable when ticked 
 
		textbox_MOT_no.Visible = True 
 
		TextBox_MOTcost.Visible = True 
 
		TextBox_MOTsub.Visible = True 
 
		'Error trapping 
 
		Try 
 
			'Inout box displayed to ask how much MOT are needed 
 
			MOT = InputBox("How MOT Health check Would You Like?") 
 
		Catch ex As Exception 
 
			'simple message 
 
			MsgBox("sorry could not accept the input") 
 
			'repeat error message 
 
			MOT = InputBox("How MOT Health check Would You Like?") 
 
		End Try 
 
		'set MOT_num to 39 
 
		MOT_num = CDbl(39) 
 
		'display MOT num in textbox 
 
		TextBox_MOTcost.Text = CDbl(MOT_num) 
 
		'display ammount ordered in textbox 
 
		textbox_MOT_no.Text = CDbl(MOT) 
 
		'calculation for MOT_total 
 
		MOT_total = MOT * MOT_num 
 
		TextBox_MOTsub.Text = CDbl(MOT_total) 
	End Sub 
 
	Private Sub Btn_calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_calculate.Click 
		Dim MOT_total As Double 
		Dim hdd_total As Double 
		Dim network_total As Double 
		Dim total As Double 
 
		MOT_total = CDbl(TextBox_MOTsub.Text) 
		hdd_total = CDbl(TextBox_hddsub.Text) 
		network_total = CDbl(TextBox_networksub.Text) 
 
		total = MOT_total + hdd_total + network_total 
 
		TextBox_total.Text = CDbl(total) 
 
	End Sub 
End Class 




thank you.

Im new to VB so id imagine ther are 1000000 mistakes

Is This A Good Question/Topic? 1
  • +

Replies To: Conversion from string "" to type 'Double' is not va

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,852
  • Joined: 18-April 07

Re: Conversion from string "" to type 'Double' is not va

Posted 03 January 2009 - 11:50 AM

For the most part you are using VB.NET so I will move this thread over to our VB.NET thread. This thread is for older VB 6 and older.

But as for your error, you have to check a textbox's value before you can simply put it into a CDbl() function for conversion. If a textbox is empty when it goes to try and convert it will have a problem.

My guess from your code is that when all three items are checked you are going to have values in all three textboxes TextBox_networksub, TextBox_hddsub and TextBox_MOTsub. If you leave any of the checkboxes unchecked one of those three textboxes are also going to be left empty.

Then in your calculate you try and access those textboxes and if any were left empty, you have your error on these lines...

MOT_total = CDbl(TextBox_MOTsub.Text)
hdd_total = CDbl(TextBox_hddsub.Text)
network_total = CDbl(TextBox_networksub.Text) 



So what you need to do is either make sure those textboxes always have a value (even if it is zero) or check them for being empty PRIOR to using CDbl on them.

If text is NOT null or empty of a value, convert it. Otherwise set total to zero...

if not String.IsNullOrEmpty(TextBox_MOTsub.Text) then
     MOT-total = CDbl(TextBox_MOTsub.Text)
else
     MOT-total = 0
end if



I hope you get the idea from this. Good luck with the project! :)

"At DIC we be textbox checking code ninjas... we are also female body inspectors... that would be FBI to you people!" :snap:

p.s. Yes CDbl is outdated and your instructor should probably be slapped for forcing you to use it in .NET. ;)

This post has been edited by Martyr2: 03 January 2009 - 11:51 AM

Was This Post Helpful? 1
  • +
  • -

#3 stoke1863  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-January 09

Re: Conversion from string "" to type 'Double' is not va

Posted 03 January 2009 - 11:57 AM

sorry for wrong section.


What a fantastic reply by the way!

i have asked this question a few times and people are just telling me to use trypase

1. I have never seen this used EVER
2. my Lecturer has taught me the cdbl way (even if its wrong i have to basicly follow his procedure or i probably wont get marked)


you have shown me the correct way of doing this and its perfectly understandbale even to me.

If i had been taugh trypase i would do it . Ive read up on it via MSDN and its about as clear as muddy water on how the syntax is used.


THANK YOU


now comes the hard bit using SQL to get data from a database :blink:

This post has been edited by stoke1863: 03 January 2009 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 DVictim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-August 09

Re: Conversion from string "" to type 'Double' is not va

Posted 25 August 2009 - 10:53 AM

I am getting the same thing only with this code

PictureBox1.ImageLocation = "http://www.yugiohcardmaker.net/ycmaker/createcard.php?name=" + NameTB.Text + "&cardtype=" + CardtypeLB.Text


I want to create an 'Image Generation' Program with a little help from YugiohCartMaker.net, and when i try to generate, this error appears...

Conversion from string "http://www.yugiohcardmaker.net/y" to type 'Double' is not valid.


Also a question... why does it stop with "/y" and not with "/ycmaker/createcard.php?name="? :/

Thanks :)

This post has been edited by DVictim: 25 August 2009 - 10:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1