Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,050 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,715 people online right now. Registration is fast and FREE... Join Now!




InvalidCastException unhandled

 
Reply to this topicStart new topic

InvalidCastException unhandled, help: Conversion from string "" to type 'Single' is

Mishka
2 Mar, 2008 - 07:42 PM
Post #1

New D.I.C Head
*

Joined: 2 Mar, 2008
Posts: 11


My Contributions
Hi.
I am a beginer with VB and am not sure how this exception is handled. can anyone make some sugestions of what to try doing? I would really appreciate any help with this.
CODE
Public Class Form1
    Private Sub btnCalcTicketRev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcTicketRev.Click

        'Declare variables for calculations.
        Dim decResult1 As Decimal ' Adult ticket sales
        Dim decResult2 As Decimal ' Child ticket sales
        Dim decResult3 As Decimal ' Total gross sales
        Dim decResult4 As Decimal ' Adult net sales
        Dim decResult5 As Decimal ' Child net sales
        Dim decResult6 As Decimal ' Total net sales
        Const decCOM_RATE As Decimal = 0.2D    ' Commission Rate

        Try
            ' Calculate and display the adult ticket sales. Handle
            ' error if the fields are blank.
            decResult1 = CDec(CSng(txtAdTickPrice.Text) * CSng(txtAdTickSold.Text))
            lblGrossAdSales.Text = decResult1.ToString
        Catch ex As Exception
            MessageBox.Show("Adult Price Per Ticket and Adult Tickets Sold must be numbers") ' -"Error")
        End Try

        Try
            ' Calculate and display the child ticket sales. Handle
            ' error if the fields are blank.
            decResult1 = CDec(CSng(txtChiTickPrice.Text) * CSng(txtChiTickSold.Text))
            lblGrossAdSales.Text = decResult1.ToString
        Catch ex As Exception
            MessageBox.Show("Child Price Per Ticket and Child Tickets Sold must be numbers") ' -"Error")
        End Try

        ' perform multiplication

        decResult1 = CDec(CSng(txtAdTickPrice.Text) * CSng(txtAdTickSold.Text))
        lblGrossAdSales.Text = FormatCurrency(decResult1)

        ' perform multiplication

        decResult2 = CDec(CSng(txtChiTickPrice.Text) * CSng(txtChiTickSold.Text))
        lblGrossChiSales.Text = FormatCurrency(decResult2)

        ' perform addition

        decResult3 = CDec(CSng(lblGrossAdSales.Text) + CSng(lblGrossChiSales.Text))
        lblTotalGross.Text = FormatCurrency(decResult3)

        ' perform multiplication

        decResult4 = CDec(CSng(lblGrossAdSales.Text) * CSng(decCOM_RATE))
        lblNetAdSales.Text = FormatCurrency(decResult4)

        ' perform multiplication

        decResult5 = CDec(CSng(lblGrossChiSales.Text) * CSng(decCOM_RATE))
        lblNetChiSales.Text = FormatCurrency(decResult5)

        ' perform multiplication

        decResult6 = CDec(CSng(lblTotalGross.Text) * CSng(decCOM_RATE))
        lblTotalNet.Text = FormatCurrency(decResult6)
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        ' End the application, by closing the window.
        Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ' Clear the adult ticket sales fields.
        txtAdTickPrice.Clear()
        txtAdTickSold.Clear()

        ' Clear the child ticket sales fields.
        txtChiTickPrice.Clear()
        txtChiTickSold.Clear()

        ' Clear the gross ticket revenue fields
        lblGrossAdSales.Text = String.Empty
        lblGrossChiSales.Text = String.Empty
        lblTotalGross.Text = String.Empty

        ' Clear the net ticket revenue fields
        lblNetAdSales.Text = String.Empty
        lblNetChiSales.Text = String.Empty
        lblTotalNet.Text = String.Empty

        ' Reset the focus to the first field.
        txtAdTickPrice.Focus()

    End Sub
End Class





User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: InvalidCastException Unhandled
2 Mar, 2008 - 07:49 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to the VB.Net Forum smile.gif

Also, it should tell you where the exception occurred, we will need that information please smile.gif
User is offlineProfile CardPM
+Quote Post

Mishka
RE: InvalidCastException Unhandled
2 Mar, 2008 - 08:03 PM
Post #3

New D.I.C Head
*

Joined: 2 Mar, 2008
Posts: 11


My Contributions
QUOTE(PsychoCoder @ 2 Mar, 2008 - 08:49 PM) *

Moved to the VB.Net Forum smile.gif

Also, it should tell you where the exception occurred, we will need that information please smile.gif


Hi and thanks... the exception occurs on either one of these two lines... depending which text box is left empty by the user.


CODE
' perform multiplication

        decResult1 = CDec(CSng(txtAdTickPrice.Text) * CSng(txtAdTickSold.Text))
        lblGrossAdSales.Text = FormatCurrency(decResult1)

        ' perform multiplication

        decResult2 = CDec(CSng(txtChiTickPrice.Text) * CSng(txtChiTickSold.Text))
        lblGrossChiSales.Text = FormatCurrency(decResult2)

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: InvalidCastException Unhandled
2 Mar, 2008 - 08:14 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You need to check to see if the textbox is empty before doing your calculations, the best & cleanest way to do this is using the String.IsNullOrEmpty() Method, like so


vb

' perform multiplication
'Check to make sure the TextBox isnt empty
If Not String.IsNullOrEmpty(txtAdTickPrice.Text) Then
decResult1 = Convert.ToDecimal(txtAdTickPrice.Text) * Convert.ToDecimal(txtAdTickSold.Text)
lblGrossAdSales.Text = decResult1.ToString("c")
Else
MessageBox.Show("Please provide a value")
End If



Also, you're using a lot of legacy VB6 libraries, you might want to look into using the native .Net libraries, such as the Convert.ToDecimal() Method and the Convert.ToSingle() Method
User is offlineProfile CardPM
+Quote Post

Mishka
RE: InvalidCastException Unhandled
3 Mar, 2008 - 02:48 PM
Post #5

New D.I.C Head
*

Joined: 2 Mar, 2008
Posts: 11


My Contributions
QUOTE(PsychoCoder @ 2 Mar, 2008 - 09:14 PM) *

You need to check to see if the textbox is empty before doing your calculations, the best & cleanest way to do this is using the String.IsNullOrEmpty() Method, like so


vb

' perform multiplication
'Check to make sure the TextBox isnt empty
If Not String.IsNullOrEmpty(txtAdTickPrice.Text) Then
decResult1 = Convert.ToDecimal(txtAdTickPrice.Text) * Convert.ToDecimal(txtAdTickSold.Text)
lblGrossAdSales.Text = decResult1.ToString("c")
Else
MessageBox.Show("Please provide a value")
End If



Also, you're using a lot of legacy VB6 libraries, you might want to look into using the native .Net libraries, such as the Convert.ToDecimal() Method and the Convert.ToSingle() Method



I tried this and I am still getting the Invalid Cast Exception... but now the error is occuring on this Null block. Where should this block be entered in the sequence of the code?

Thanks for the help.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: InvalidCastException Unhandled
3 Mar, 2008 - 03:51 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Which block of code is now throwing the error? You forgot to post the code.
User is offlineProfile CardPM
+Quote Post

Mishka
RE: InvalidCastException Unhandled
3 Mar, 2008 - 07:24 PM
Post #7

New D.I.C Head
*

Joined: 2 Mar, 2008
Posts: 11


My Contributions
Sorry about that... I posted a reply from work and had app on computer at home. The error occurs when I do not enter a value to one or more of the text boxes. So depending on which are left blank it can halt the application in two places.
The message I get is System.InvalidCastException was unhandled
Message="Conversion from string "" to type 'Single' is not valid."

Source="Microsoft.VisualBasic"

CODE
       ' perform multiplication
        'Check to make sure the TextBox isnt empty  
        If Not String.IsNullOrEmpty(txtAdTickPrice.Text) Then
            decResult1 = Convert.ToDecimal(txtAdTickPrice.Text) * Convert.ToDecimal(txtAdTickSold.Text)
            lblGrossAdSales.Text = decResult1.ToString("c")
        Else
            MessageBox.Show("Please provide a value")
        End If
      
        decResult1 = CDec(CSng(txtAdTickPrice.Text) * CSng(txtAdTickSold.Text))
        lblGrossAdSales.Text = FormatCurrency(decResult1)

        ' perform multiplication
        'Check to make sure the TextBox isnt empty  
        If Not String.IsNullOrEmpty(txtChiTickPrice.Text) Then
            decResult1 = Convert.ToDecimal(txtChiTickPrice.Text) * Convert.ToDecimal(txtChiTickSold.Text)
            lblGrossChiSales.Text = decResult1.ToString("c")
        Else
            MessageBox.Show("Please provide a value")
        End If

        decResult2 = CDec(CSng(txtChiTickPrice.Text) * CSng(txtChiTickSold.Text))
        lblGrossChiSales.Text = FormatCurrency(decResult2)


User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: InvalidCastException Unhandled
3 Mar, 2008 - 08:36 PM
Post #8

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
The example I showed you was for only 1 of your textboxes, you're going to need to check for String.IsNullOrEmpty() on all of them. I was just showing you an example of how to check for an empty value smile.gif

You could eliminate this by adding a RequiredFieldValidator Control to each of your TextBox controls.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:13PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month