Thursday, March 21, 2013

VB.NET - Function to Create Datatable for Testing

If you want to create a quick datatable without having to connect to any database for testing or proof of concept development, here is a function to create and return a datatable.  If you want more or less rows just add or take them away, also be sure to have your datatype correctly defined if adding or changing the row types.


' For testing
    Function GetDataTable() As DataTable

        ' Create sample data for the DataList control.
        Dim dt As DataTable = New DataTable()
        Dim dr As DataRow

        Try

            ' Define the columns of the table.
            dt.Columns.Add(New DataColumn("tag", GetType(String)))
            dt.Columns.Add(New DataColumn("class", GetType(String)))

            ' Populate the table with sample values.

            dr = dt.NewRow()

            dr(0) = "p"
            dr(1) = "paragraph"

            dt.Rows.Add(dr)

            dr = dt.NewRow()

            dr(0) = "li"
            dr(1) = "listItem"

            dt.Rows.Add(dr)

            dr = dt.NewRow()

            dr(0) = "b"
            dr(1) = "bold"

            dt.Rows.Add(dr)


            Return dt

        Catch ex As Exception
            Throw ex
        End Try

    End Function

No comments:

Post a Comment