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

Wednesday, March 13, 2013

ASP.NET/VB.NET - Remove HTML Markup to Display As Plain Text


''' <summary>
    ''' function will remove any text that shows up between the less than and greater than symbols to display HTML as text in a control such as a listbox
    ''' </summary>
    ''' <param name="strText"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function RemoveHTML(strText) As String
        Try
            Return Regex.Replace(strText, "<[^>]*>", "")
        Catch ex As Exception
            Throw ex
        End Try
    End Function

Tuesday, March 12, 2013

VB.NET - Count String Occurrence Within String

Code to look for a count of the number of times a string appears within another string:



Dim intOccurence As Integer


 intOccurence = Regex.Matches(strStringToSearchWithin, Regex.Escape(strStringToSearchFor)).Count