1

データセットとCrystalReportsについてサポートをお願いします。

DataSetに2つのDataTable、つまりDeliveredItemsとDeliveryDetailsがあります。私が遭遇した問題は、DeliveredItemsの部分にあり、次の行にエラーがあります。

dataSet.Tables("DeliveredItems").Rows.Add(row)

それは私に与えます:

"The row already belongs to this table."

そして、コードを微調整すると、エラーが返されました。

"Object reference not set to an instance of an object."

私の目的は、ListViewのすべてのアイテムと、データセット内のサブアイテムを取得して、CrystalReportに表示することです。これが私のコードです:

Private Sub CreateReport()

    'Initialize the needed objects for report document.
    Dim myDeliveryReport As New rptDeliveryReceipt
    Dim row As DataRow = Nothing
    Dim row2 As DataRow = Nothing
    Dim dataSet As New DataSet
    Dim counter As Integer = lviDeliveryList.Items.Count
    Dim tempCount As Integer = 0
    Dim listViewItems As New ListViewItem()

    'Create a new DataTable named Delivery Details
    dataSet.Tables.Add("DeliveryDetails")

    'Create columns for the new DataTable named Delivery Details.
    With dataSet.Tables(0).Columns

        .Add("deliveryClientName", Type.GetType("System.String"))
        .Add("deliveryClientStreetAddress", Type.GetType("System.String"))
        .Add("deliveryClientCity", Type.GetType("System.String"))
        .Add("deliveryDRNumber", Type.GetType("System.String"))
        .Add("deliveryDate", Type.GetType("System.String"))
        .Add("deliveryPONumber", Type.GetType("System.String"))

    End With

    'Initialize and insert delivery heading to the DataTable.
    row = dataSet.Tables(0).NewRow
    row(0) = txtDeliveryTargetClient.Text
    row(1) = txtDeliveryClientAddress.Text
    row(2) = txtDeliveryClientCity.Text
    row(3) = txtDRNumber.Text
    row(4) = dtpDeliveryDate.Value.ToString
    row(5) = txtDRPO.Text
    dataSet.Tables(0).Rows.Add(row)

    'Create another DataTable called DeliveredItems
    dataSet.Tables.Add("DeliveredItems")

    'Create columns for the new DataTable named DeliveryItems.
    With dataSet.Tables(1).Columns

        .Add("deliveryCatNumber", Type.GetType("System.String"))
        .Add("deliveryItemDescription", Type.GetType("System.String"))
        .Add("deliveryItemQuantity", Type.GetType("System.String"))
        .Add("deliveryItemUnit", Type.GetType("System.String"))
        .Add("deliveryItemDetails", Type.GetType("System.String"))

    End With

    row = Nothing
    row = dataSet.Tables(1).NewRow

    'Store every data in an array for insertion.
    Dim deliveredItemsCount As Integer = lviDeliveryList.Items.Count
    Dim tempArray() As String
    ReDim tempArray(5)
    Dim deliveryListViewItem As New ListViewItem()

    For counter = 0 To deliveredItemsCount

        deliveryListViewItem = lviDeliveryList.Items.Item(counter)
        tempArray(0) = lviDeliveryList.Items.Item(counter).ToString
        tempArray(1) = deliveryListViewItem.SubItems(1).ToString
        tempArray(2) = deliveryListViewItem.SubItems(2).ToString
        tempArray(3) = deliveryListViewItem.SubItems(3).ToString
        tempArray(4) = deliveryListViewItem.SubItems(4).ToString

        'Insert new records to the DeliveredItems.
        row2(0) = tempArray(0)
        row2(1) = tempArray(1)
        row2(2) = tempArray(2)
        row2(3) = tempArray(3)
        row2(4) = tempArray(4)
        dataSet.Tables("DeliveredItems").Rows.Add(row)

        For counterClear = 0 To (deliveredItemsCount - 1)
            tempArray(counter) = Nothing
        Next counterClear

        row2 = Nothing

    Next counter

    'Set Data Sources for the Crystal Report.
    myDeliveryReport.SetDataSource(dataSet)
    frmDeliveryReceiptReport.crvDeliveryReceipt.ReportSource = myDeliveryReport

    'Dispose after using.
    dataSet.Dispose()
    dataSet = Nothing

End Sub
4

1 に答える 1

0

この行で

 dataSet.Tables("DeliveredItems").Rows.Add(row) 

row2ではなくrowを追加していて、row2が初期化されることはありません。

For counter = 0 To deliveredItemsCount   

    deliveryListViewItem = lviDeliveryList.Items.Item(counter)   
    tempArray(0) = lviDeliveryList.Items.Item(counter).ToString   
    .....

    row2 = dataSet.Tables("DeliveredItems").NewRow
    row2(0) = tempArray(0)   
    ......
    dataSet.Tables("DeliveredItems").Rows.Add(row2)   

最後の注意として。tempArrayを回避できると思います...deliveryListViewItemからrow2の値を直接設定します。(最後に配列のクリアも削除します)

于 2012-09-08T14:31:11.293 に答える