Posts

Showing posts from 2009

Removing Duplicates from List Collection

Sounds really simple to pull duplicates out of a list ... MS handled this with a function in VS 3.5 but in earlier versions we had to come up with our own solution.   Dim i As Integer Dim lastapprover As String = String .Empty Dim iIndex As Integer iIndex = bulletApprovers.Items.Count For i = 0 To bulletApprovers.Items.Count - 1 While iIndex > 0           If bulletApprovers.Items(i).Text = lastapprover Then                lastapprover = bulletApprovers.Items(iIndex).Text                bulletApprovers.Items.RemoveAt(iIndex) Else               lastapprover = bulletApprovers.Items(iIndex - 1).Text End If                  iIndex = iIndex - 1 End While Next

Generic Lists to Datasets

Easy way to push a Generic List into a Dataset ** Note 'tasks' is the name of my generic list class Public Function ConvertGenericsListToDataSet( ByVal listvalue As tasks()) As DataSet Dim sb As New StringBuilder() Dim xmlSerializer As New XmlSerializer( GetType (tasks())) Dim sw As New StringWriter(sb) xmlSerializer.Serialize(sw, listvalue) Dim stream As New StringReader(sb.ToString()) Dim ds As New DataSet() ds.ReadXml(stream) Return ds End Function

Sorting Ascending and Descending

Toggling the direction of your datagrid sort is easy using a session to remember which direction you sorted last.   You will want to declare an empty string in the top of your page       Dim SortField As String Then on your sort command add this logic - Example of entire command Private Sub dgTaskExceptions_SortCommand( ByVal source As Object , ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgTaskExceptions.SortCommand     SortField = e.SortExpression If Session( "toggle" ) = True Then SortField += " DESC" Session( "toggle" ) = False Else SortField += " Asc" Session( "toggle" ) = True End If Dim orderstring As String = SortField 'REBIND dgTaskExceptions.DataSource = POTracking_Data.tracking.getSampleTasks(orderstring) dgTaskExceptions.DataBind() End Sub   In my datalayer I add this logic to my query and pass in an OPTIONAL value Dim orderbyString As String = String .Empt

BIND AD to DataGrid

To bind the Active Directory to a datagrid I have built two functions. The first one is called either on pageload or button click or whenever you want it to happen: (Watch for wrapping) Private Function CheckAD() Dim objRootDSE = GetObject( ldap://RootDSE/ ) Dim adPath As String = "LDAP://" & Mid(objRootDSE.Get("DefaultNamingContext"), InStr(objRootDSE.Get("DefaultNamingContext"), "DC=") + 3, InStr(objRootDSE.Get("DefaultNamingContext"), "," ) - 1 - 3) Dim adAuth As LDAPAuthenticate = New LDAPAuthenticate(adPath) dgLdap.DataSource = LDAPAuthenticate.LoadADList(adPath) dgLdap.DataBind() End Function The second one actually connects to the Active Directory and returns a list of the searched properties: Public Shared Function LoadADList(ByVal pathsent As String) As List(Of LDAPAuthenticate) Dim result As SearchResultCollection = Nothing Dim entry As DirectoryEntry = New DirectoryEntry(pathsent, "testuser", "