我正在获取错误,因为FormateException未被usercode处理。 错误字符串的格式不正确。 无法解决此错误。有什么建议吗? ?
我在这行中遇到错误
Dim stateid As Integer = Convert.ToInt32(cmbState.SelectedValue.ToString())
这是我的完整代码
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