473,594 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ 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 2070
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
15357
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 compile. Help. Thanks.
4
17674
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 to do a generic cast or conversion on the type? Here is sort of what I want to do: object MyFunc(Type T, String Str) { object o;
1
2512
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 there a way to figure out the min/max constants of decimal(9,3) to compare against in C# to make sure the new value is "in bound"? Thanks! -zeng
7
9979
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 using CodeDOM for my scripting needs (I realize I could use yacc or something else, but I wanted to try using CodeDOM -- this is more of an exercise for me to learn this stuff). Well, I compile my in-game scripts just fine. See appendix A for my code
0
3358
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
1375
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 to format the designer output. Anyone know whats going on? I don't get any real errors...
6
5765
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 this separated into a .h and .cpp file. The code I am attempting to put into the class is code that I have used before, but simply put into the same file/class with the rest of the application. My header looks like this: <code>
3
5021
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 it works, so why is it that about once a week for about half an hour I get this error? Any Ideas? Thanks, Ady
7
2696
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 advice? Sorry for the huge post, just wanted to make sure you can understand the problem. As it is, I'm lost.... I've included the WordNode header file, so why can't I create/use WordNode objects/methods (in BST)? WordNode.h #ifndef RANDOM...
1
7093
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 someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
0
7936
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8227
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6646
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5402
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2383
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 we have to send another system
0
1203
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.