How to search a datatable and assign values from rows in vb.net. Using Dim result() = dt.select([queryhere]) method


Code below is a small snippet from a page code-behind where the aspx front-wend page has six different HyperLink controls. The code sets the visibility of the control by getting results into a datatable. A new datatable is constructed just for the page controls with a column of visibility. This column is set to true or false using a datatable search method or the datatable.select method. 
 
 
 
'get the codes/roles the user has and put them in a table 
        Dim dtRoles As DataTable
        Dim sqlgetRoles As String = ""
        sqlgetRoles = "SELECT Code, [Name] FROM WebPortalUserRole_TN WHERE UserLogin = " & SafeString(login)
        dtRoles = SQL2DataTable(sqlgetRoles)
 
        '##### ROLES/CODES FOR PAGE TOOLS #####
        Dim result() As DataRow
        Dim handbookCodes As New DataTable
        handbookCodes.Columns.Add("HCode")
        handbookCodes.Columns.Add("Visible")
        'add default handbook codes and set visible to false
        handbookCodes.Rows.Add("HBOOK_NUNI", "FALSE")
        handbookCodes.Rows.Add("HBOOK_HHT", "FALSE")
        handbookCodes.Rows.Add("HBOOK_UNIT", "FALSE")
        handbookCodes.Rows.Add("HBOOK_OFFI", "FALSE")
        '##### ROLES/CODES FOR PAGE TOOLS #####
 
 
        'check if login has the codes and if so chage visible column to true
        If dtRoles.Rows.Count > 0 Then
            For Each rw As DataRow In handbookCodes.Rows
                result = dtRoles.Select("Code ='" & rw.Item("HCode") & "'")
                'compare the codes
                If result.Length > 0 AndAlso result(0).Item("Code") = rw.Item("HCode") Then
                    'change the flase value in the row if the code matches
                    rw.Item("Visible") = "TRUE"
                End If
            Next
        End If
 
        'page links to handbooks , should match number of codes in dt handbookCodes
        link_HBookNUNI.Visible = handbookCodes.Rows(0).Item("Visible")
        link_HBookHHT.Visible = handbookCodes.Rows(1).Item("Visible")
        link_HBookUNIT.Visible = handbookCodes.Rows(2).Item("Visible")
        link_HBook_Office.Visible = handbookCodes.Rows(3).Item("Visible")
 
        'TODO build a receipt table --- should only show receipt forms when the user hasn't completed them
        link_HBookNUNI_Receipt.Visible = False
        link_HBook_OfficeReceipt.Visible = False
Last update:
2014-10-01 17:04
Author:
Mark
Revision:
1.0
Average rating:0 (0 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags