0

I am Getting error like FormateException was unhandled by usercode. Error String is not in a correct format. can't solve this error. Any Suggestion ? ?

I am getting Error In this Line

Dim stateid As Integer = Convert.ToInt32(cmbState.SelectedValue.ToString())

Here's my full code

  Public Class Form1
            Private Sub FillState()
                Dim cn As SqlConnection = New SqlConnection("Data Source=192.168.0.7;Initial Catalog=TraineeDataBaseExamples;Persist Security Info=True;User ID=newtrainee;Password=newtrainee")
                cn.Open()
                Dim da As SqlDataAdapter = New SqlDataAdapter(" select * from KaiyumVbState ", cn)
                Dim ds As New DataSet
                da.Fill(ds)
                With cmbState
                    .DataSource = ds.Tables(0)
                    .DisplayMember = "StateName"
                    .ValueMember = "StateID"
                    .SelectedIndex = -1
                End With

            End Sub

            Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                FillState()
            End Sub

            Private Sub cmbState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbState.SelectedIndexChanged
                If cmbState.SelectedValue.ToString() <> "" Then
                    Dim stateid As Integer = Convert.ToInt32(cmbState.SelectedValue.ToString())
                    FillCities(stateid)
                    cmbCity.SelectedIndex = -1
                End If
            End Sub
            Private Sub FillCities(ByVal stateid As Integer)
                Dim cn As SqlConnection = New SqlConnection("Data Source=192.168.0.7;Initial Catalog=TraineeDataBaseExamples;Persist Security Info=True;User ID=newtrainee;Password=newtrainee")
                Dim cmd As SqlCommand = New SqlCommand("select CityId,CityName from KaiyumVbCity where StateID = @Stateid ", cn)
                cmd.Parameters.AddWithValue("@StateId", stateid)
                Dim da As New SqlDataAdapter()
                da.SelectCommand = cmd
                Dim ds As New DataSet()
                cn.Open()
                da.Fill(ds)
                If ds.Tables(0).Rows.Count > 0 Then
                    With cmbCity
                        .DataSource = ds.Tables(0)
                        .DisplayMember = "CityName"
                        .ValueMember = "CityId"
                    End With
                End If
            End Sub
            Private Sub cmbState_AfterUpdate()

            End Sub
        End Class
kkk
  • 273
  • 1
  • 6
  • 20
  • Are you sure that the StateID column contains only numeric (integer) values? If you set a breakpoint on the failing line, what is the value of SelectedValue property? – – Steve Oct 03 '12 at 10:39
  • solved by this. solved by this. If Integer.TryParse(cmbState.SelectedValue.ToString(), stateid) Then FillCities(stateid) End If – kkk Oct 03 '12 at 11:04

0 Answers0