Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sorting a System.Collections.Generic List

Posted on 2014-03-21
4
Medium Priority
?
337 Views
Last Modified: 2014-03-21
I need to be able to sort a list collection based off one of two elements within the list depending on a radio button selection on my page (username& EmpID) prior to loading the list into a drop down list control.  Any assistance would be greatly appreciated!

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Collections.Generic
Imports System.Net
Imports System.Text
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports System.Data.Common
Imports Teradata.Net.Security
Imports Teradata.Client.Provider
Imports Teradata.Client.Provider.TdConnection
Imports System.Data.Common.DbConnectionStringBuilder
Imports System.Data.Common.DbTransaction
Imports System.Security.AccessControl
Imports System.Security.Principal
Imports System.Net.Mail

Partial Class Alter
  Inherits System.Web.UI.Page
  Dim newUser As userInfo
  Dim lstUsers As List(Of userInfo)

#Region " Control Events "
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
      PullUserList()
      Session("page") = 4
    End If

  End Sub
#End Region

#Region " Pull User List "
  Public Sub PullUserList()
    Dim SelStmt, rsCommString As String
    Dim pf As DbProviderFactory = DbProviderFactories.GetFactory("Teradata.Client.Provider")
    Dim con As DbConnection = pf.CreateConnection()
    Dim command As DbCommand = con.CreateCommand
    Dim stringBuilder As New Teradata.Client.Provider.TdConnectionStringBuilder()
    Dim SqlQuery As StringBuilder = New StringBuilder
    Dim arPtr As Integer
    lstUsers = New List(Of userInfo)()

    stringBuilder.DataSource = Session.Contents("DataSource")
    stringBuilder.UserId = Session.Contents("AdminLogin")
    stringBuilder.Password = Session.Contents("AdminPasswd")
    stringBuilder.PersistSecurityInfo = True

    con.ConnectionString = stringBuilder.ConnectionString

    'Get Master List of Users
    SqlQuery.Clear()
    SqlQuery.Append("Sel * From TABLE")
    SelStmt = SqlQuery.ToString()

    command.CommandText = SelStmt
    command.CommandType = CommandType.Text
    command.CommandTimeout = 600

    Try
      con.Open()
      Dim reader As DbDataReader = command.ExecuteReader()


      Do While reader.Read()
        If reader.HasRows Then
          newUser = New userInfo()
          If IsDBNull(reader("commentstring").ToString) = False Then
            rsCommString = reader("commentstring").ToString
          Else
            rsCommString = ""
          End If

          If InStr(rsCommString, "~") <> 0 And _
             InStr(rsCommString, "#") <> 0 And _
             InStr(rsCommString, "$") <> 0 And _
             InStr(rsCommString, "*") <> 0 And _
             InStr(rsCommString, "^") <> 0 And _
             InStr(rsCommString, "+") <> 0 Then

            newUser.username = UCase(reader("username").ToString)
            newUser.comment = reader("commentstring").ToString
            newUser.ownerName = reader("ownername").ToString


            If InStr(UCase(rsCommString), "CONSULTANT") > 0 Then
              newUser.EmpID = ""
            Else
              If InStr(rsCommString, "*") - InStr(rsCommString, "$") > 1 Then
                newUser.EmpID = Mid$(rsCommString, (InStr(rsCommString, "$") + 1), (InStr(rsCommString, "*") - InStr(rsCommString, "$")) - 1)
              Else
                newUser.EmpID = "Not Specified"
              End If
            End If

            If IsDBNull(reader("createtimestamp").ToString) = True Then
              newUser.ctimestamp = ""
            Else
              newUser.ctimestamp = reader("createtimestamp").ToString
            End If

            If IsDBNull(reader("user_status").ToString) = True Then
              newUser.ustatus = ""
            Else
              newUser.ustatus = reader("user_status").ToString
            End If

            If IsDBNull(reader("user_suspend_date").ToString) = True Then
              newUser.susdt = ""
            Else
              newUser.susdt = reader("user_suspend_date").ToString
            End If
            lstUsers.Add(newUser)
          Else
            reader.NextResult()
          End If
        End If
      Loop
      con.Close()
    Catch ex As Exception
      MsgBox("User List data pull has failed:  " & ex.Message, vbExclamation, "ERROR")
      Exit Sub
    End Try

    arPtr = lstUsers.Count
    lstUsers.Sort()

    If rblSort.SelectedValue = 0 Then
      ddUser.Items.Clear()
      ddUser.Items.Add("")
      For arPtr = 0 To lstUsers.Count - 1
        Select Case lstUsers.Item(arPtr).ownerName
          Case "MWDW", "NEDW", "SDW", "WDW", "HQ", "RSL", "SPCL", "DEV"
            If InStr(lstUsers.Item(arPtr).comment, "~") > 0 And InStr(lstUsers.Item(arPtr).comment, "Admin Id") = 0 Then
              ddUser.Items.Add(lstUsers.Item(arPtr).username _
              & " : " & Mid$(lstUsers.Item(arPtr).comment, 1, (InStr(lstUsers.Item(arPtr).comment, "~") - 1)) _
              & " ~ " & Mid$(lstUsers.Item(arPtr).comment, (InStr(lstUsers.Item(arPtr).comment, "$") + 1), (InStr(lstUsers.Item(arPtr).comment, "*") - InStr(lstUsers.Item(arPtr).comment, "$")) - 1))
            End If
        End Select
      Next arPtr
    Else()
      ddUser.Items.Clear()
      ddUser.Items.Add("")
      For arPtr = 0 To lstUsers.Count - 1
        Select Case lstUsers.Item(arPtr).ownerName
          Case "MWDW", "NEDW", "SDW", "WDW", "HQ", "RSL", "SPCL", "DEV"
            If InStr(lstUsers.Item(arPtr).comment, "~") > 0 And InStr(lstUsers.Item(arPtr).comment, "Admin Id") = 0 Then
              ddUser.Items.Add(Mid$(lstUsers.Item(arPtr).comment, 1, (InStr(lstUsers.Item(arPtr).comment, "~") - 1)) _
              & " : " & lstUsers.Item(arPtr).username _
              & " ~ " & Mid$(lstUsers.Item(arPtr).comment, (InStr(lstUsers.Item(arPtr).comment, "$") + 1), (InStr(lstUsers.Item(arPtr).comment, "*") - InStr(lstUsers.Item(arPtr).comment, "$")) - 1))
            End If
        End Select
      Next arPtr
    End If

  End Sub

  Public Class userInfo
    Public username As String
    Public comment As String
    Public ownerName As String
    Public EmpID As String
    Public ctimestamp As String
    Public ustatus As String
    Public susdt As String
  End Class
#End Region

Open in new window

0
Comment
Question by:EDW_Gideon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 39945818
One option would be to use LINQ:
This link describes the sort method and provides an example.
http://msdn.microsoft.com/en-us/library/b0zbh7b6(v=vs.110).aspx
0
 

Author Comment

by:EDW_Gideon
ID: 39946063
I found that earlier but I don't think it's the same method I'm using in my code.  I first add my list items to a "newUser" list and add newUser to the lstUsers.  I got this process from a previously posted question here on EE...  Perhaps I'm using the method wrong?

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28299823.html

Sorry if this is a simple process perhaps I'm just not grasping the MSDN example.
0
 
LVL 27

Accepted Solution

by:
Shaun Kline earned 2000 total points
ID: 39946228
You are building your list of users from a database query. Could you just sort the data in your query, or do you need to sort the list of users somewhere other than the code you provided?
0
 

Author Comment

by:EDW_Gideon
ID: 39946254
I initially thought of that but as the value was only a portion of one of the values I thought it may be easier to sort it after the fact.  I found some SQL that will allow me to pull this sub-string out as a field which will allow me to drive the sort from the query.  Thanks for your assistance.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question