Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting a System.Collections.Generic List

Posted on 2014-03-21
4
Medium Priority
?
340 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
  • 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

876 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