InvalidCastException

  • 13 years ago

    I am trying to make the following procedure work:

    Private Sub PrintTicket(ByVal strArtist As String, ByVal dteConcertDate As Date, ByVal intSeatNumber As Int32)

    PrintTicket(ArtistTextBox.Text, ConcertDateDTPicker.Value.Date, SeatTextBox.Text)

    <display returned info in a messagebox ( I already have this code written)

    End Sub

    Then as a test, I use the following sample call:

    PrintTicket(ArtistTextBox.Text, System.DateTime.Today, 6)

    Now as I follow it through the debug, everything goes fine until it tries to put 6 into intSeatNumber. I get the following error:

    InvalidCastException: Conversion from string "" to type 'Integer' is not valid.

    Now I think I understand why this is crashing and I have tried using CInt in a couple of different places to make sure the string I take is an integer but with no luck.

    Where am I messing this up? Thanks.

  • 13 years ago
    Your procedure 'PrintTicket' is calling itself.  This is probably a mistake.  In your case if it weren't for the InvalidCastException your procedure would continue to call itself indefinitly causing a StackOverflow error.  The 'PrintTicket' call from within the 'PrintTicket' procedure probably shouldn't exist so you can remove it.  The reason you get the InvalidCastException is because the 'PrintTicket' call inside the procedure tries to pass 'SeatTextBox.Text' into intSeatNumber.  The textbox is obviously empty (the error message tells you what it was trying to cast...in your case "")which causes the cast to fail.
  • 13 years ago
    I think there needs to be some clarification...

    Do you have two overloaded sub routines:

    Private Sub PrintTickets(String, Date, String)
    End Sub

    Private Sub PrintTickets(String, Date, Int32)
    End Sub





    or just one?







Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In theory, theory and practice are the same. In practice, they're not.”