Wednesday, February 6, 2013

VB.NET CSV Export for Custom Class


'WRITE A CSV FOR A LIST OF CUSTOM CLASS

        'Declare your customclass and customclass list and set the value of the list
        Dim reasonowed As ReasonOwedClass = New ReasonOwedClass
        Dim reasonowedlist As List(Of ReasonOwedClass) = New List(Of ReasonOwedClass)
        reasonowedlist = reasonowed.GetAllReasonOwed()

        'Configure the page to export a csv file
        Dim attachment As String = "attachment; filename=MyCsvLol.csv"
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.ContentType = "text/csv"
        HttpContext.Current.Response.AddHeader("Content-Disposition", attachment)

        'Create what you will add your information to
        Dim sb As StringBuilder = New StringBuilder()

        'Optionally declare the property names
        'Since this will be for a mail merge, column names will be customized
        'So this block will likely be useless
        For Each field As System.Reflection.PropertyInfo In reasonowed.GetType.GetProperties()
            sb.Append(field.Name.ToString + ",")
        Next
        sb.Replace(",", vbNewLine, sb.Length - 1, 1)

        'Loop through the properties in the class and add them to the stringbuilder
        'If an item is intentionally left blank, you should make sure not to add to stringbuilder
        For Each reasonowedinlist As ReasonOwedClass In reasonowedlist
            For Each field As System.Reflection.PropertyInfo In reasonowedinlist.GetType.GetProperties()
                sb.Append(field.GetValue(reasonowedinlist).ToString + ",")
            Next
            sb.Replace(",", vbNewLine, sb.Length - 1, 1)
        Next

        'Write the file using the stringbuilder information you have been appending
        HttpContext.Current.Response.Write(sb.ToString())
        HttpContext.Current.Response.End()

Saturday, December 29, 2012

Get all strings between strings of a String VB.NET

Here is a code snippet on how to get all substrings of a string that are inbetween two strings.  It first requires the GetBetween() function to get a string between two other strings from a larger string.

GetBetween()

Public Function GetBetween(ByVal haystack As String, ByVal needle As String, ByVal needle_two As String) As String
        Dim istart As Integer = InStr(haystack, needle)
        If istart > 0 Then
            Dim istop As Integer = InStr(istart, haystack, needle_two)
            If istop > 0 Then
                Dim value As String = haystack.Substring(istart + Len(needle) - 1, istop - istart - Len(needle))
                Return value
            End If
        End If
        Return Nothing
    End Function

GetAllBetween()

This code will allow you to not only get a string between two other strings from within a string, it can return all substrings that you are searching for inbetween at once.  For example, this would be a good way to pull all of the names from an HTML file that fall between <span> and </span>.


Public Function GetAllBetween(ByVal Haystack As String, ByVal Needle As String, ByVal Needle2 As String) As List(Of String)
        'Declare a list of strings to return
        Dim StringList As List(Of String) = New List(Of String)
        'We will set the haystack to 0 when no more objects are found
        While Haystack <> ""
            'Checks to see if an object is found
            If GetBetween(Haystack, Needle, Needle2) Is Nothing Then
                'Since no more strings were found - we set the haystack to blank to end the loop
                Haystack = ""
                'If an string was found
            Else
                'Check to see if the found item already exists
                If StringList.Contains(GetBetween(Haystack, Needle, Needle2)) Then
                    'Remove the part of the haystack before where the already found part is
                    Haystack = Haystack.Substring(Haystack.LastIndexOf(GetBetween(Haystack, Needle, Needle2)))
                    'If the string hasn't already been found
                Else
                    'Add the new found object to the list
                    StringList.Add(GetBetween(Haystack, Needle, Needle2))
                End If
            End If
        End While
        'Send the list of strings back
        Return StringList
    End Function

Friday, December 28, 2012

Validation

Looking at validation for a project at work.  The asp forms in .net offer some really nice tools for data validation, but what really has me concerned is what is necessary with form validation.  Obviously, there are certain fields that are required, and have a required validator attached to them.

Is All Validation Necessary?

Say your database contains a varchar(50) item that accepts null values.  When you are submitting information to that database, should you even check to make sure the data is correct if it is just optional?

When to validate optional data:
  • It will be used somewhere else (ex: reports)
  • It absolutely must have a certain format (ex: letters only)
  • Must have a certain pattern to it
When not to validate optional data:
  • It will not be used somewhere else (ex: reports)
  • Does not need a certain format, or it isn't important to have an acceptable format (ex: phone numbers could include dashes, but since they can be written so many different ways you can leave them unvalidated if they are not used elsewhere.