Friday, December 27, 2013

ASP.NET - Add Validator to Dynamically Created Control

Dim objValidator As New RegularExpressionValidator()
objValidator.ControlToValidate = txtItem.ClientID
objValidator.ValidationExpression = "[0-9]"
objValidator.Text = "*"
objValidator.ErrorMessage = "Must be in correct format"

objValidator.SetFocusOnError = True


phPlaceholder.Controls.Add(objValidator)

Friday, October 4, 2013

SQL - Declare a Table Variable

DECLARE @table_membership TABLE(   seq_id int identity(1,1),
                                            name varchar(1000) NOT NULL,
                                            contact varchar(1000) NOT NULL,
                                            type_sort int NOT NULL,

                                   )

SQL - Update Table From Itself


UPDATE a
SET a.Price=(SELECT MAX(b.PRICE) FROM ITEM AS b WHERE b.Item=a.Item and b.Price is not null )
FROM  Item AS a 


*credit goes to this post: http://stackoverflow.com/questions/12151107/tsql-update-table-from-itself

Monday, June 24, 2013

ASP.NET/VB.NET - Get Attributes of Image File

In order to check attributes such as height, width, and resolution of an image file, go off the System.Drawing.Image namespace:


System.Drawing.Image.FromFile(strFile).Width
System.Drawing.Image.FromFile(strFile).Height
System.Drawing.Image.FromFile(strFile).HorizontalResolution

System.Drawing.Image.FromFile(strFile).VerticalResolution

Monday, April 8, 2013

ASP.NET - Add Class Tags to HTML with XML Node Processing

Here is how you can process html and pick out html tags and add class attributes to them:

Imports System.Xml


    Private Function HandleClassTags(ByVal astrContent As String) As String

        Dim objxmlDoc As New XmlDocument
        Dim objNodeList_Html As XmlNodeList
        Dim strXML As String = ""
        Dim dt_ClassTags As DataTable
        Dim strResult As String = ""

        Try
            dt_ClassTags = GetClassTags() 'return datatable with one column "parent_tag" and one column as "class" ... the value for class will be assigned to the html tag in "parent_tag"

            If dt_ClassTags.Rows.Count > 0 Then

                'replace all & symbols with '---amp---'
                strXML = "<root>" & astrContent.Replace("&", "---amp---") & "</root>"
                objxmlDoc.LoadXml(strXML)

'first go through and remove the existing class set for the html tag if one exists
                For Each row As DataRow In dt_ClassTags.Rows
                    objNodeList_Html = objxmlDoc.GetElementsByTagName(row("parent_tag").ToString)
                    For Each xmlElem As System.Xml.XmlElement In objNodeList_Html
                        If xmlElem.GetAttribute("class") IsNot Nothing OrElse xmlElem.GetAttribute("class") <> "" Then
                            xmlElem.RemoveAttribute("class")
                        End If
                    Next
                Next

'now add the tags from the table as described above
                For Each row As DataRow In dt_ClassTags.Rows
                    objNodeList_Html = objxmlDoc.GetElementsByTagName(row("parent_tag").ToString)
                    For Each xmlElem As System.Xml.XmlElement In objNodeList_Html
                        If xmlElem.GetAttribute("class") Is Nothing OrElse xmlElem.GetAttribute("class") = "" Then
                            xmlElem.SetAttribute("class", row("class").ToString)
                        End If
                    Next
                Next

                strResult = objxmlDoc.OuterXml
                strResult = strResult.Replace("<root>", "").Replace("</root>", "").Replace("---amp---", "&")
            Else
                strResult = astrContent
            End If

            Return strResult

        Catch ex As Exception
            Throw ex
        Finally
            If dt_ClassTags IsNot Nothing Then
                dt_ClassTags.Dispose()
                dt_ClassTags = Nothing
            End If
            If objxmlDoc IsNot Nothing Then
                objxmlDoc = Nothing
            End If
        End Try
    End Function

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