473,473 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

2 New Member
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
3 2053
iam_clint
1,208 Recognized Expert Top Contributor
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
silverkaito
2 New Member
how do we do double.tryparse?
Oct 20 '14 #3
Frinavale
9,735 Recognized Expert Moderator Expert
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

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

Similar topics

3
by: Harry Keck | last post by:
I would imagine that this is a pretty trivial operation, but I can not figure out how to cast a string variable to a Decimal. I tried Decimal dVariable = (Decimal)strStringVariable, but it does not...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
1
by: Zeng | last post by:
Hello, I have many fields in my SQL database that are of type decimal(9,3). How do I detect a decimal value (type decimal in C#) will overflow the db column of sql type decimal (9,3)? Is...
7
by: Clint Herron | last post by:
Howdy! I posted this question on CSharpCorner.com, but then realized I should probably post it on a more active newsgroup. This will be my only cross-post. I'm creating a game engine, and...
0
by: gui.besse | last post by:
It seems that we can't bind a collection of instance of different type. Let's have an example: // My POCO public interface ITest { string Name { get;set;} } public class A : ITest {
0
by: schneider | last post by:
Collection editor problems. I keep getting "Object does not match target type." I think it has something to do with the TypeConverter, because it work fine with-out it. I need the converter...
6
by: sandy | last post by:
I am creating a class (or so I hope) which is to be a JobCollection, linked list of 'Job'. Job is another class already created. JobCollection is just the linked list manager. I have to have...
3
by: Ady1 | last post by:
Hi, I'm getting this error intermitantly in a custom configuration section in my ASP.NET website. From what I can see 0.175 is a valid string to be converted to a decimal! And most of the time...
7
by: LoneHunter01 | last post by:
When I try to compile the below files, the complier says: the WordNode h in BST.cpp: "WordNode" does not name a type also in BST.cpp: "WordNode" undeclared, first use of this function So, any...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.