2013-10-29 71 views
0

我的代碼:VB.NET公共 '添加' 錯誤

Imports System.Data.OleDb 
Public Class DBase 

    Dim conn As New OleDbConnection 
    Dim myqry As String = Nothing 
    Dim mycmd As New OleDbCommand 
    Dim mydr As OleDbDataReader 
    Dim str As String = Nothing 
    Dim objCmd As New OleDb.OleDbCommand 

    Private Sub DBase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     Call LVsettings() 
     Call ConnToDB() 
     Call FillListview() 
     Call Set1() 
    End Sub 
#Region "FillListView" 

    Sub FillListview() 
     LV.Items.Clear() 
     myqry = "SELECT * from Accounts ORDER BY YrandSec,FirstName Asc" 
     mycmd = New OleDbCommand(myqry, conn) 
     mydr = mycmd.ExecuteReader 

     While mydr.Read 
      With LV 
       .Items.Add(mydr("Number")) 
       With .Items(.Items.Count - 1).SubItems 
        .Add(mydr("StudNo")) 
        .Add(mydr("FirstName")) 
        .Add(mydr("LastName")) 
        .Add(mydr("YrandSec")) 
        .Add(mydr("Exer1")) - Error occurs here 
        .Add(mydr("Exer2")) 
        .Add(mydr("Exer3")) 
        .Add(mydr("Exer4")) 
        .Add(mydr("Exer5")) 
       End With 
      End With 
     End While 
    End Sub 

#End Region 

錯誤:

ERROR - (Overload resolution failed because no Public 'Add' can be called with these arguments: 
    'Public Function Add(text As String) As System.Windows.Forms.ListViewItem.ListViewSubItem': 
     Argument matching parameter 'text' cannot convert from 'DBNull' to 'String'. 
    'Public Function Add(item As System.Windows.Forms.ListViewItem.ListViewSubItem) As System.Windows.Forms.ListViewItem.ListViewSubItem': 
     Argument matching parameter 'item' cannot convert from 'DBNull' to 'ListViewSubItem'.) 

回答

0

Add方法需要一個字符串參數,但單元值是否爲DBNull。 VB.NET無法在運行時將DBNull轉換爲字符串。您可以執行以下操作之一: 1.更改您的SQL語句以明確列出要選擇的列。可能爲NULL的列應該被ISNULL語句包圍,例如, ISNULL('Exer1','')。 2.在While循環中更改您的代碼以處理NULL值(例如,來自:

.Add(mydr("Exer1")) 

要(colIndex是在結果中列的索引,不幸的是你不能使用列名):

If Not mydr.IsDBNull(colIndex) Then .Add(mydr("Exer1")) 

或類似的東西,處理空值。