1

我想寻求有关数据集和 Crystal Reports 的帮助。

我在数据集中有两个数据表,即 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 的所有项目及其在数据集中的子项目显示在 Crystal Report 上。这是我的代码:

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) 

您添加的是 row 而不是 row2 并且 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 回答