2013-07-12 110 views
0

我需要從所選項目的數據源中檢索「id」,但它總是拋出標題中提到的相同錯誤。這裏是我的代碼無法投入'System.Int32'類型的對象來鍵入'System.Data.DataRowView'

 Dim DMV As DataRowView = LbMCat.SelectedValue 'Here occurs the error 
    SelectedMainCat = DMV.Item("id") 

    'Filling the SUB Categories part/same code used to fill Main categories 
    Dim DataAdapterCats As New MySqlDataAdapter("SELECT * From catS where maincat = '" & SelectedMainCat & "';", MySqlConnection) 
    Dim dsSc As New DataSet 
    DataAdapterCats.Fill(dsSc, "SubCategories") 
    Dim SDataTable As DataTable = dsSc.Tables(0) 
    LbSCat.DataSource = SDataTable 
    LbSCat.DisplayMember = "title" 
    LbSCat.ValueMember = "id" 

回答

2

待辦事項如下

Dim DMV As DataRowView = TryCast(LbMCat.SelectedItem, DataRowView) 

If DMV IsNot Nothing Then 
    SelectedMainCat = DMV.Item("id") 
End If 
+0

我不能夠感謝你:) –

0

嘗試直接鑄造的價值:

See it here(DirectCast(LbmCat.SelectedItem,DataRowView的)( 「ID」)的ToString())。它可能有幫助

+0

同樣的錯誤,無法投類型 'System.Int32' 的對象鍵入 'System.Data.DataRowView'。 –

0

如果你檢查的是所選的值是不是整數?

If Not TypeOf (LbMCat.SelectedValue) Is Integer Then 
    Dim DMV As DataRowView = LbMCat.SelectedValue 'Here occurs the error 
    SelectedMainCat = DMV.Item("id")  
End If 
相關問題