By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,347 Members | 2,091 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,347 IT Pros & Developers. It's quick & easy.

why does "Conversion from string "" to type 'Decimal' is not valid." keep appearing

P: 2
i'm creating gpa and cgpa calculator for my school project. and i'm a newbie to this visual basic thingy. so do help me, pls.

here's my code

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim CreditHour1 As Integer
  3.     Dim CreditHour2 As Integer
  4.     Dim CreditHour3 As Integer
  5.     Dim CreditHour4 As Integer
  6.     Dim CreditHour5 As Integer
  7.     Dim CreditHour6 As Integer
  8.     Dim CreditHour7 As Integer
  9.     Dim TotalCreditHours As Integer
  10.  
  11.  
  12.     Private Sub ButtonTotal_Click(sender As System.Object, e As System.EventArgs) Handles ButtonTotal1.Click
  13.  
  14.         CreditHour1 = TextBoxCreditHour1.Text
  15.         CreditHour2 = TextBoxCreditHour2.Text
  16.         CreditHour3 = TextBoxCreditHour3.Text
  17.         CreditHour4 = TextBoxCreditHour4.Text
  18.         CreditHour5 = TextBoxCreditHour5.Text
  19.         CreditHour6 = TextBoxCreditHour6.Text
  20.         CreditHour7 = TextBoxCreditHour7.Text
  21.         TotalCreditHours = CreditHour1 + CreditHour2 + CreditHour3 + CreditHour4 + CreditHour5 + CreditHour6 + CreditHour7
  22.         LabelCreditHours2.Text = TotalCreditHours
  23.  
  24.  
  25.     End Sub
  26.  
  27.     Private Sub ButtonExit_Click(sender As System.Object, e As System.EventArgs) Handles ButtonExit.Click
  28.         Close()
  29.     End Sub
  30.  
  31.     Private Sub ButtonTotal2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonTotal2.Click
  32.  
  33.         Dim TotalGradePoints1 As Double
  34.         Dim TotalGradePoints2 As Double
  35.         Dim TotalGradePoints3 As Double
  36.         Dim TotalGradePoints4 As Double
  37.         Dim TotalGradePoints5 As Double
  38.         Dim TotalGradePoints6 As Double
  39.         Dim TotalGradePoints7 As Double
  40.         Dim TotalTotalGradePoints As Double
  41.  
  42.         TotalGradePoints1 = TextBoxTotalGradePoints1.Text
  43.         TotalGradePoints2 = TextBoxTotalGradePoints2.Text
  44.         TotalGradePoints3 = TextBoxTotalGradePoints3.Text
  45.         TotalGradePoints4 = TextBoxTotalGradePoints4.Text
  46.         TotalGradePoints5 = TextBoxTotalGradePoints5.Text
  47.         TotalGradePoints6 = TextBoxTotalGradePoints6.Text
  48.         TotalGradePoints7 = TextBoxTotalGradePoints7.Text
  49.         LabelTotalGradePoints2.Text = TotalTotalGradePoints
  50.  
  51.         CreditHour1 = TextBoxCreditHour1.Text
  52.         CreditHour2 = TextBoxCreditHour2.Text
  53.         CreditHour3 = TextBoxCreditHour3.Text
  54.         CreditHour4 = TextBoxCreditHour4.Text
  55.         CreditHour5 = TextBoxCreditHour5.Text
  56.         CreditHour6 = TextBoxCreditHour6.Text
  57.         CreditHour7 = TextBoxCreditHour7.Text
  58.  
  59.         Select Case ComboBox1.Text
  60.             Case "A"
  61.                 TotalGradePoints1 = 4 * CreditHour1
  62.             Case "A-"
  63.                 TotalGradePoints1 = 3.75 * CreditHour1
  64.             Case "B+"
  65.                 TotalGradePoints1 = 3.5 * CreditHour1
  66.             Case "B"
  67.                 TotalGradePoints1 = 3.0 * CreditHour1
  68.             Case "B-"
  69.                 TotalGradePoints1 = 2.75 * CreditHour1
  70.             Case "C+"
  71.                 TotalGradePoints1 = 2.5 * CreditHour1
  72.             Case "C"
  73.                 TotalGradePoints1 = 2.0 * CreditHour1
  74.             Case "C-"
  75.                 TotalGradePoints1 = 1.75 * CreditHour1
  76.             Case "D"
  77.                 TotalGradePoints1 = 1 * CreditHour1
  78.             Case "F"
  79.                 TotalGradePoints1 = 0 * CreditHour1
  80.  
  81.         End Select
  82.  
  83.         Select Case ComboBox2.Text
  84.             Case "A"
  85.                 TotalGradePoints2 = 4 * CreditHour2
  86.             Case "A-"
  87.                 TotalGradePoints2 = 3.75 * CreditHour2
  88.             Case "B+"
  89.                 TotalGradePoints2 = 3.5 * CreditHour2
  90.             Case "B"
  91.                 TotalGradePoints2 = 3.0 * CreditHour2
  92.             Case "B-"
  93.                 TotalGradePoints2 = 2.75 * CreditHour2
  94.             Case "C+"
  95.                 TotalGradePoints2 = 2.5 * CreditHour2
  96.             Case "C"
  97.                 TotalGradePoints2 = 2.0 * CreditHour2
  98.             Case "C-"
  99.                 TotalGradePoints2 = 1.75 * CreditHour2
  100.             Case "D"
  101.                 TotalGradePoints2 = 1 * CreditHour2
  102.             Case "F"
  103.                 TotalGradePoints2 = 0 * CreditHour2
  104.  
  105.         End Select
  106.  
  107.         Select Case ComboBox3.Text
  108.             Case "A"
  109.                 TotalGradePoints3 = 4 * CreditHour3
  110.             Case "A-"
  111.                 TotalGradePoints3 = 3.75 * CreditHour3
  112.             Case "B+"
  113.                 TotalGradePoints3 = 3.5 * CreditHour3
  114.             Case "B"
  115.                 TotalGradePoints3 = 3.0 * CreditHour3
  116.             Case "B-"
  117.                 TotalGradePoints3 = 2.75 * CreditHour3
  118.             Case "C+"
  119.                 TotalGradePoints3 = 2.5 * CreditHour3
  120.             Case "C"
  121.                 TotalGradePoints3 = 2.0 * CreditHour3
  122.             Case "C-"
  123.                 TotalGradePoints3 = 1.75 * CreditHour3
  124.             Case "D"
  125.                 TotalGradePoints3 = 1 * CreditHour3
  126.             Case "F"
  127.                 TotalGradePoints3 = 0 * CreditHour3
  128.  
  129.         End Select
  130.  
  131.         Select Case ComboBox4.Text
  132.             Case "A"
  133.                 TotalGradePoints4 = 4 * CreditHour4
  134.             Case "A-"
  135.                 TotalGradePoints4 = 3.75 * CreditHour4
  136.             Case "B+"
  137.                 TotalGradePoints4 = 3.5 * CreditHour4
  138.             Case "B"
  139.                 TotalGradePoints4 = 3.0 * CreditHour4
  140.             Case "B-"
  141.                 TotalGradePoints4 = 2.75 * CreditHour4
  142.             Case "C+"
  143.                 TotalGradePoints4 = 2.5 * CreditHour4
  144.             Case "C"
  145.                 TotalGradePoints4 = 2.0 * CreditHour4
  146.             Case "C-"
  147.                 TotalGradePoints4 = 1.75 * CreditHour4
  148.             Case "D"
  149.                 TotalGradePoints4 = 1 * CreditHour4
  150.             Case "F"
  151.                 TotalGradePoints4 = 0 * CreditHour4
  152.  
  153.         End Select
  154.  
  155.         Select Case ComboBox5.Text
  156.             Case "A"
  157.                 TotalGradePoints5 = 4 * CreditHour5
  158.             Case "A-"
  159.                 TotalGradePoints5 = 3.75 * CreditHour5
  160.             Case "B+"
  161.                 TotalGradePoints5 = 3.5 * CreditHour5
  162.             Case "B"
  163.                 TotalGradePoints5 = 3.0 * CreditHour5
  164.             Case "B-"
  165.                 TotalGradePoints5 = 2.75 * CreditHour5
  166.             Case "C+"
  167.                 TotalGradePoints5 = 2.5 * CreditHour5
  168.             Case "C"
  169.                 TotalGradePoints5 = 2.0 * CreditHour5
  170.             Case "C-"
  171.                 TotalGradePoints5 = 1.75 * CreditHour5
  172.             Case "D"
  173.                 TotalGradePoints5 = 1 * CreditHour5
  174.             Case "F"
  175.                 TotalGradePoints5 = 0 * CreditHour5
  176.  
  177.         End Select
  178.  
  179.         Select Case ComboBox6.Text
  180.             Case "A"
  181.                 TotalGradePoints6 = 4 * CreditHour6
  182.             Case "A-"
  183.                 TotalGradePoints6 = 3.75 * CreditHour6
  184.             Case "B+"
  185.                 TotalGradePoints6 = 3.5 * CreditHour6
  186.             Case "B"
  187.                 TotalGradePoints6 = 3.0 * CreditHour6
  188.             Case "B-"
  189.                 TotalGradePoints6 = 2.75 * CreditHour6
  190.             Case "C+"
  191.                 TotalGradePoints6 = 2.5 * CreditHour6
  192.             Case "C"
  193.                 TotalGradePoints6 = 2.0 * CreditHour6
  194.             Case "C-"
  195.                 TotalGradePoints6 = 1.75 * CreditHour6
  196.             Case "D"
  197.                 TotalGradePoints6 = 1 * CreditHour6
  198.             Case "F"
  199.                 TotalGradePoints6 = 0 * CreditHour6
  200.  
  201.         End Select
  202.  
  203.         Select Case ComboBox7.Text
  204.             Case "A"
  205.                 TotalGradePoints7 = 4 * CreditHour7
  206.             Case "A-"
  207.                 TotalGradePoints7 = 3.75 * CreditHour7
  208.             Case "B+"
  209.                 TotalGradePoints7 = 3.5 * CreditHour7
  210.             Case "B"
  211.                 TotalGradePoints7 = 3.0 * CreditHour7
  212.             Case "B-"
  213.                 TotalGradePoints7 = 2.75 * CreditHour7
  214.             Case "C+"
  215.                 TotalGradePoints7 = 2.5 * CreditHour7
  216.             Case "C"
  217.                 TotalGradePoints7 = 2.0 * CreditHour7
  218.             Case "C-"
  219.                 TotalGradePoints7 = 1.75 * CreditHour7
  220.             Case "D"
  221.                 TotalGradePoints7 = 1 * CreditHour7
  222.             Case "F"
  223.                 TotalGradePoints7 = 0 * CreditHour7
  224.  
  225.         End Select
  226.  
  227.     End Sub
  228.  
  229.  
  230. End Class
Oct 19 '14 #1
Share this Question
Share on Google+
3 Replies


iam_clint
Expert 100+
P: 1,208
You don't do any verification on any of the text boxes.
Expand|Select|Wrap|Line Numbers
  1. TotalGradePoints1 = TextBoxTotalGradePoints1.Text
  2. TotalGradePoints2 = TextBoxTotalGradePoints2.Text
  3. TotalGradePoints3 = TextBoxTotalGradePoints3.Text
  4. TotalGradePoints4 = TextBoxTotalGradePoints4.Text
  5. TotalGradePoints5 = TextBoxTotalGradePoints5.Text
  6. TotalGradePoints6 = TextBoxTotalGradePoints6.Text
  7. TotalGradePoints7 = TextBoxTotalGradePoints7.Text
  8. LabelTotalGradePoints2.Text = TotalTotalGradePoints
  9.  
  10. CreditHour1 = TextBoxCreditHour1.Text
  11. CreditHour2 = TextBoxCreditHour2.Text
  12. CreditHour3 = TextBoxCreditHour3.Text
  13. CreditHour4 = TextBoxCreditHour4.Text
  14. CreditHour5 = TextBoxCreditHour5.Text
  15. CreditHour6 = TextBoxCreditHour6.Text
  16. CreditHour7 = TextBoxCreditHour7.Text
  17.  
if a textbox is blank it will give you a conversion error since you are trying to assign the text value of a text box to a double.

look at double.tryparse(), and double.parse
Oct 19 '14 #2

P: 2
how do we do double.tryparse?
Oct 20 '14 #3

Frinavale
Expert Mod 5K+
P: 9,731
You use the Double.TryParse method like this:

Expand|Select|Wrap|Line Numbers
  1. Dim value As String = TextBoxCreditHour1.Text
  2. Dim number As Double
  3.  
  4. If Double.TryParse(value, number) Then
  5.   ' the value could successfully be converted into a Double
  6. Else
  7.  ' the value could NOT be converted into a Double
  8. End If
-Frinny
Oct 20 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.