locked
Problem exporting date field in dataset to CSV file RRS feed

  • Question

  • Hi I am using this code I found on the forum to export data to a CSV file. One of the columns is a DATETIME field but for some reason that gets missed out of the export, everything else is copied to the new CSV file, can anyone see a reason why this code is not exporting the DATETIME field? Is there something I can add? Many thanks in advance. Bryan (This is on a mobile)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myTable As DataTable
            Dim myRow As DataRow
            Dim numCols As Integer = 0
            Dim myString As String
            Dim myWriter As New System.IO.StreamWriter("\Program files\Mobile Birder\resources\MobileBird_Export.csv")

            For Each myTable In MobileBirder_Export_DataSet.Tables
                For Each myRow In myTable.Rows
                    myString = ""
                    For numCols = 0 To myTable.Columns.Count - 2
                        If Object.ReferenceEquals(myRow.Item(numCols).GetType(), myString.GetType()) Then
                            myString = myString & """" & myRow.Item(numCols).ToString() & ""","
                        ElseIf Object.ReferenceEquals(myRow.Item(numCols).GetType(), numCols.GetType()) Then
                            myString = myString & myRow.Item(numCols).ToString() & ","
                        End If

                    Next
                    If Object.ReferenceEquals(myRow.Item(numCols).GetType(), myString.GetType()) Then
                        myString = myString & """" & myRow.Item(numCols).ToString() & """"
                    ElseIf Object.ReferenceEquals(myRow.Item(numCols).GetType(), numCols.GetType()) Then
                        myString = myString & myRow.Item(numCols).ToString()
                    End If
                    myWriter.WriteLine(myString)
                Next

            Next
            myWriter.Close()

        End Sub
    Monday, December 21, 2009 7:51 PM

Answers

  • Hi thanks for the reply. The 'Datetime' column was appearing in the datagrid. I removed the column from the dataset and then added it back again, it then started to work!
    I think i must have altered a property in the actual database but not updated the dataset causing it not to read the column correctly, but whatever it was it is now working so thank you for your help.

    Thanks Bryan
    • Marked as answer by warrentang Monday, December 28, 2009 1:35 AM
    Sunday, December 27, 2009 1:37 PM

All replies

  • Hi,

    First I think you need to verify the datetime column is present in the data table and the values are not null. You can use DataTable::WriteXml to do that. If you can make sure the values are there, then you need to debug the code step by step and check out that the values are read and written right.

    Regards,
    Chunsheng Tang


    Please mark the post that helps you, and unmark that does not. This benefits our community.
    Friday, December 25, 2009 9:17 AM
  • Hi thanks for the reply. The 'Datetime' column was appearing in the datagrid. I removed the column from the dataset and then added it back again, it then started to work!
    I think i must have altered a property in the actual database but not updated the dataset causing it not to read the column correctly, but whatever it was it is now working so thank you for your help.

    Thanks Bryan
    • Marked as answer by warrentang Monday, December 28, 2009 1:35 AM
    Sunday, December 27, 2009 1:37 PM