2012-06-20 59 views
-2

如何解決「對象不能從DBNull轉換爲其他類型」? 我正在使用組合框來檢索服務的價格和折扣價格,如果我在combobox3中選擇一個項目一次,錯誤不會發生,但是如果我在combobox3中再次選擇一個項目,「對象不能從DBNull轉換爲其他類型「。會發生..如何解決「對象不能從DBNull轉換爲其他類型」。

這裏是我的代碼

Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged 

    Dim ds As New DataSet 

    ds = getPrice(arr(ComboBox2.SelectedIndex), ComboBox3.Text) 


    If ds.Tables("getPrice").Rows.Count > 0 Then 
     For i As Integer = 0 To ds.Tables("getPrice").Rows.Count - 1 

      TextBox1.Text = Convert.ToDecimal(ds.Tables("getPrice").Rows(i).Item(0)) 
      TextBox2.Text = Convert.ToDecimal(ds.Tables("getPrice").Rows(i).Item(1)) 

     Next 
    End If 

End Sub 

這是我的查詢

Public Function getPrice(ByVal serviceId As String, ByVal breedSize As String) 

    If breedSize = "Small Breed" Then 
     sqlStr = "Select price_small_breed, discount_small_breed From tblServicePrice Where service_id = " + serviceId + "" 
    ElseIf breedSize = "Medium Breed" Then 
     sqlStr = "Select price_medium_breed, discount_medium_breed From tblServicePrice Where service_id = " + serviceId + "" 
    ElseIf breedSize = "Big Breed" Then 
     sqlStr = "Select price_big_breed, discount_big_breed From tblServicePrice Where service_id = " + serviceId + "" 
    End If 

    ds.Clear() 
    da = New OleDbDataAdapter(sqlStr, con.ConnectionString) 
    da.Fill(ds, "getPrice") 


    Return ds 

End Function 

這裏是圖像

enter image description here

可能是什麼造成具有這個問題?

+1

什麼迭代行的點,如果你分配給一組單獨的文本框? – StingyJack

+0

這甚至沒有編譯。 – StingyJack

回答

1
TextBox1.Text = If(ds.Tables("getPrice").Rows(i).Item(0) Is DBNull.Value, "", Convert.ToDecimal(ds.Tables("getPrice").Rows(i).Item(0)).ToString()) 
TextBox2.Text = If(ds.Tables("getPrice").Rows(i).Item(0) Is DBNull.Value, "", Convert.ToDecimal(ds.Tables("getPrice").Rows(i).Item(1)).ToString()) 
+0

當我選擇一個新的組合框項目時,它將在兩個文本框中都返回空白。 – user188228

+1

是的,如果數據集中沒有數據,則嘗試將其轉換爲無關緊要。 – StingyJack

0

如果數據庫中存在NULL值,會發生什麼?如果可以接受使用某個值,例如零,你可以修改查詢像

sqlStr = "Select COALESCE(price_small_breed, 0), COALESCE(discount_small_breed, 0) From tblServicePrice Where service_id = " & serviceId & "" 
+0

但我的數據庫中沒有空值。 – user188228

+1

null在您的數據集中。 – StingyJack

1

如果您運行的是在用getPrice對數據庫執行特定的查詢語句,你會看到,行中的至少一個具有空值在你的順序位置正在閱讀。

serviceID(注意上限)可能不是您所假設的值。

-4

我的回答是這樣的..我宣佈新的數據集,我沒有從我的代碼

Public Function getPrice(ByVal serviceId As String, ByVal breedSize As String) As DataSet 

     Dim dsa As New DataSet 


     If breedSize = "Small Breed" Then 
      sqlStr = "Select price_small_breed, discount_small_breed From tblServicePrice Where service_id = " + serviceId + "" 
     ElseIf breedSize = "Medium Breed" Then 
      sqlStr = "Select price_medium_breed, discount_medium_breed From tblServicePrice Where service_id = " + serviceId + "" 
     ElseIf breedSize = "Big Breed" Then 
      sqlStr = "Select price_big_breed, discount_big_breed From tblServicePrice Where service_id = " + serviceId + "" 
     End If 

     dsa.Clear() 
     da = New OleDbDataAdapter(sqlStr, con.ConnectionString) 
     da.Fill(dsa, "getPrice") 

     Return dsa 

    End Function 

這是我以前的代碼上使用的數據集..

Public Function getPrice(ByVal serviceId As String, ByVal breedSize As String) 

    If breedSize = "Small Breed" Then 
     sqlStr = "Select price_small_breed, discount_small_breed From tblServicePrice Where service_id = " + serviceId + "" 
    ElseIf breedSize = "Medium Breed" Then 
     sqlStr = "Select price_medium_breed, discount_medium_breed From tblServicePrice Where service_id = " + serviceId + "" 
    ElseIf breedSize = "Big Breed" Then 
     sqlStr = "Select price_big_breed, discount_big_breed From tblServicePrice Where service_id = " + serviceId + "" 
    End If 

    ds.Clear() 
    da = New OleDbDataAdapter(sqlStr, con.ConnectionString) 
    da.Fill(ds, "getPrice") 


    Return ds 

End Function 
+0

這沒有任何意義。請閱讀其他答案。 – StingyJack

+0

它的工作原理,這就是爲什麼我是誰回答我的問題@StingyJack,那裏的答案不工作... – user188228

+0

誰是你對我說這件事?不要對我說這件事,因爲你甚至沒有發佈你的解決方案,我的問題... – user188228

相關問題