Tips and tricks for .NET using ASP and VB code.

Create an array from a column of data in a table

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim DataTable1 As New Data.DataTable
        DataTable1.ReadXml("C:\Data.xml")
        Dim ColumnValues() As String = DataTableColumnValuesToStringArray(DataTable1, "ColumnName")
    End Sub

    Function DataTableColumnValuesToStringArray(PassedDataTable As Data.DataTable, PassedColumnName As String) As String()
        If (PassedDataTable.Columns.Contains(PassedColumnName) = False) Or (PassedDataTable.Rows.Count < 1) Then
            Return Nothing
            Exit Function
        End If
        Dim ReturnValues(PassedDataTable.Rows.Count - 1) As String
        For i As Integer = 0 To PassedDataTable.Rows.Count - 1
            ReturnValues(i) = Convert.ToString(PassedDataTable.Rows(i).Item(PassedColumnName))
        Next i
        Return ReturnValues
    End Function

No comments:

Post a Comment

Search This Blog