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!
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
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()
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
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
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?
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
' 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
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