Results 1 to 4 of 4

Thread: Help! Dim RegionID As Integer = Convert.ToInt32

  1. #1

    Thread Starter
    Registered User
    Join Date
    Aug 2018
    Posts
    2

    Lightbulb Help! Dim RegionID As Integer = Convert.ToInt32

    Name:  image1.jpg
Views: 312
Size:  13.8 KB


    If cboRegion.SelectedValue.ToString() <> "" Then
    ' Dim cboRegion As String = String.Empty
    Dim RegionID As Integer = Convert.ToInt32(cboRegion.SelectedValue.ToString())
    LoadProvince(RegionID)
    cboCity.SelectedIndex = 0

    End If

    Need some Help, said, input String was not in a correct Format

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Help! Dim RegionID As Integer = Convert.ToInt32

    Welcome to VBForums

    That code is attempting to convert the value in cboRegion.SelectedValue.ToString() to an Integer, but the error indicates that isn't possible.

    To find out why you should check what the actual value of cboRegion.SelectedValue.ToString() is, as it might be completely different to what you expect... and if you can't work out what is wrong, tell us what the value is and we can work it out.

    Once you know what the issue is you can alter your code to cope with it. One possible solution (which is not appropriate for all situations) is to use TryParse instead of Convert, eg:
    Code:
    Dim RegionID As Integer
    If Int32.TryParse(cboRegion.SelectedValue.ToString(), RegionID) Then
      LoadProvince(RegionID)
      cboCity.SelectedIndex = 0
    End If
    This doesn't make it more likely to convert the value, but it does mean that no error message will be displayed if it can't be converted.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Help! Dim RegionID As Integer = Convert.ToInt32

    This:

    cboRegion.SelectedValue.ToString()

    is not returning what you think it is returning. Depending on how you set up cboRegion, you may be getting back a type name rather than the value you expect. Put a breakpoint on the line that causes the exception (or just let it fail), and look at what that .ToString is returning....you'll be surprised.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help! Dim RegionID As Integer = Convert.ToInt32

    If you're using SelectedValue then you should have assigned the name of a property to the ValueMember. What is the data type of that property?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width